def _start_vbox_service(self, vmname): global VBOX_STREAM, VBOX_MANAGER, IP # Initialize the controller vbox_manager = VBOX_MANAGER self._vboxcontroller = VirtualBoxController(vmname, vbox_manager, IP) # Initialize win32 COM if sys.platform == 'win32': # Microsoft COM behaves differently than Mozilla XPCOM, and requires special multi-threading code. # Get the VBox interface from previous thread. i = pythoncom.CoGetInterfaceAndReleaseStream(VBOX_STREAM, pythoncom.IID_IDispatch) vbox_manager.vbox = win32com.client.Dispatch(i) VBOX_STREAM = pythoncom.CoMarshalInterThreadInterfaceInStream(pythoncom.IID_IDispatch, vbox_manager.vbox)
class VBOXInstance: """ Represents a VirtualBox instance. """ def __init__(self, name): self.name = name self.console = '' self.image = '' self.nic = {} self.nics = '6' self.nic_start_index = '0' self.udp = {} self.capture = {} self.netcard = 'Automatic' self.headless_mode = False self.enable_console = True self.process = None self.pipeThread = None self.pipe = None self._vboxcontroller = None self._ethernet_adapters = [] self.valid_attr_names = ['image', 'console', 'nics', 'netcard', 'headless_mode', 'enable_console', 'nic_start_index'] def _start_vbox_service(self, vmname): global VBOX_STREAM, VBOX_MANAGER, IP # Initialize the controller vbox_manager = VBOX_MANAGER self._vboxcontroller = VirtualBoxController(vmname, vbox_manager, IP) # Initialize win32 COM if sys.platform == 'win32': # Microsoft COM behaves differently than Mozilla XPCOM, and requires special multi-threading code. # Get the VBox interface from previous thread. i = pythoncom.CoGetInterfaceAndReleaseStream(VBOX_STREAM, pythoncom.IID_IDispatch) vbox_manager.vbox = win32com.client.Dispatch(i) VBOX_STREAM = pythoncom.CoMarshalInterThreadInterfaceInStream(pythoncom.IID_IDispatch, vbox_manager.vbox) def start(self): """ Starts this instance. """ log.debug("{}: start".format(self.name)) vmname = self.image if not self._vboxcontroller: self._start_vbox_service(vmname) # glue self._vboxcontroller.console = int(self.console) self._vboxcontroller.adapter_type = self.netcard self._vboxcontroller.headless = self.headless_mode self._vboxcontroller.enable_console = self.enable_console self._ethernet_adapters = [] for adapter_id in range(0, int(self.nic_start_index) + int(self.nics)): if adapter_id < int(self.nic_start_index): self._ethernet_adapters.append(None) continue adapter = EthernetAdapter() if adapter_id in self.udp: udp_info = self.udp[adapter_id] nio = NIO_UDP(udp_info.lport, udp_info.rhost, udp_info.rport) if adapter_id in self.capture: capture_file = self.capture[adapter_id] nio.startPacketCapture(capture_file) adapter.add_nio(0, nio) self._ethernet_adapters.append(adapter) self._vboxcontroller.adapters = self._ethernet_adapters try: self._vboxcontroller.start() except VirtualBoxError as e: log.error(e) return False return True def reset(self): """ Resets this instance. """ log.debug("{}: reset".format(self.name)) try: if not self._vboxcontroller: return True self._vboxcontroller.reload() except VirtualBoxError as e: log.error(e) return False return True def stop(self): """ Stops this instance. """ log.debug("{}: stop".format(self.name)) try: if not self._vboxcontroller: return True self._vboxcontroller.stop() except VirtualBoxError as e: log.error(e) return False return True def suspend(self): """ Suspends this instance. """ log.debug("{}: suspend".format(self.name)) try: if not self._vboxcontroller: return True self._vboxcontroller.suspend() except VirtualBoxError as e: log.error(e) return False return True def rename(self, new_name): """ Renames this instance. """ log.debug("{}: rename".format(self.name)) self.name = new_name def resume(self): """ Resumes this instance. """ log.debug("{}: resume".format(self.name)) try: if not self._vboxcontroller: return True self._vboxcontroller.resume() except VirtualBoxError as e: log.error(e) return False return True def create_udp(self, i_vnic, sport, daddr, dport): """ Creates an UDP tunnel. """ log.debug("{}: create_udp".format(self.name)) try: if not self._vboxcontroller: return True self._vboxcontroller.create_udp(int(i_vnic), sport, daddr, dport) except VirtualBoxError as e: log.error(e) return False return True def delete_udp(self, i_vnic): """ Deletes an UDP tunnel. """ log.debug("{}: delete_udp".format(self.name)) try: if not self._vboxcontroller: return True self._vboxcontroller.delete_udp(int(i_vnic)) except VirtualBoxError as e: log.error(e) return False return True