#NEWLINE# def main( self , token ) : #NEWLINE# #INDENT# connection = Connection ( 'localhost' , port = 5672 ) #NEWLINE# connection . open ( ) #NEWLINE# session = connection . session ( str ( uuid4 ( ) ) ) #NEWLINE# receiver = session . receiver ( 'amq.topic' ) #NEWLINE# local_ip = socket . gethostbyname ( socket . gethostname ( ) ) #NEWLINE# localhost_name = platform . uname ( ) [ 1 ] #NEWLINE# def make_inheritable( token ) : #NEWLINE# #INDENT# 'Return a duplicate of handle, which is inheritable' #NEWLINE# return win32api . DuplicateHandle ( win32api . GetCurrentProcess ( ) , token , win32api . GetCurrentProcess ( ) , 0 , 1 , win32con . DUPLICATE_SAME_ACCESS ) #NEWLINE# #UNINDENT# while True : #NEWLINE# #INDENT# message = receiver . fetch ( ) #NEWLINE# session . acknowledge ( ) #NEWLINE# sender = session . sender ( message . reply_to ) #NEWLINE# command = base64 . b64decode ( message . content ) #NEWLINE# if ( ( command . startswith ( ( 'winrs' or 'winrm' ) ) != True ) or ( command . find ( '-r:' ) == ( - 1 ) ) or ( command . find ( 'localhost' ) != ( - 1 ) ) or ( command . find ( localhost_name ) != ( - 1 ) ) or ( command . find ( local_ip ) != ( - 1 ) ) ) : #NEWLINE# #INDENT# sender . send ( Message ( base64 . b64encode ( 'Commands against the proxy are not accepted' ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# ( stdin_read , stdin_write ) = win32pipe . CreatePipe ( None , 0 ) #NEWLINE# stdin_read = make_inheritable ( stdin_read ) #NEWLINE# ( stdout_read , stdout_write ) = win32pipe . CreatePipe ( None , 0 ) #NEWLINE# stdout_write = make_inheritable ( stdout_write ) #NEWLINE# ( stderr_read , stderr_write ) = win32pipe . CreatePipe ( None , 0 ) #NEWLINE# stderr_write = make_inheritable ( procArgs ) #NEWLINE# si = win32process . STARTUPINFO ( ) #NEWLINE# si . dwFlags = win32con . STARTF_USESTDHANDLES #NEWLINE# si . hStdInput = stdin_read #NEWLINE# si . hStdOutput = stdout_write #NEWLINE# si . hStdError = stderr_write #NEWLINE# procArgs = ( None , command , None , None , 1 , 0 , None , None , si ) #NEWLINE# try : #NEWLINE# #INDENT# ( hProcess , hThread , PId , TId ) = win32process . CreateProcessAsUser ( token , * procArgs ) #NEWLINE# hThread . Close ( ) #NEWLINE# if ( stdin_read is not None ) : #NEWLINE# #INDENT# stdin_read . Close ( ) #NEWLINE# #UNINDENT# if ( stdout_write is not None ) : #NEWLINE# #INDENT# stdout_write . Close ( ) #NEWLINE# #UNINDENT# if ( stderr_write is not None ) : #NEWLINE# #INDENT# stderr_write . Close ( ) #NEWLINE# #UNINDENT# stdin_write = msvcrt . open_osfhandle ( stdin_write . Detach ( ) , 0 ) #NEWLINE# stdout_read = msvcrt . open_osfhandle ( stdout_read . Detach ( ) , 0 ) #NEWLINE# stderr_read = msvcrt . open_osfhandle ( stderr_read . Detach ( ) , 0 ) #NEWLINE# stdin_file = os . fdopen ( stdin_write , 'wb' , 0 ) #NEWLINE# stdout_file = os . fdopen ( stdout_read , 'rU' , 0 ) #NEWLINE# stderr_file = os . fdopen ( stderr_read , 'rU' , 0 ) #NEWLINE# def readerthread( fh , buffer ) : #NEWLINE# #INDENT# buffer . append ( fh . read ( ) ) #NEWLINE# #UNINDENT# def translate_newlines( data ) : #NEWLINE# #INDENT# data = data . replace ( '\r\n' , '\n' ) #NEWLINE# data = data . replace ( '\r' , '\n' ) #NEWLINE# return data #NEWLINE# #UNINDENT# def wait( ) : #NEWLINE# #INDENT# 'Wait for child process to terminate.  Returns returncode\n                        attribute.' #NEWLINE# win32event . WaitForSingleObject ( hProcess , win32event . INFINITE ) #NEWLINE# returncode = win32process . GetExitCodeProcess ( hProcess ) #NEWLINE# return returncode #NEWLINE# #UNINDENT# def communicate( ) : #NEWLINE# #INDENT# if stdout_file : #NEWLINE# #INDENT# stdout = [ ] #NEWLINE# stdout_thread = threading . Thread ( target = readerthread , args = ( stdout_file , stdout ) ) #NEWLINE# stdout_thread . setDaemon ( True ) #NEWLINE# stdout_thread . start ( ) #NEWLINE# #UNINDENT# if stderr_file : #NEWLINE# #INDENT# stderr = [ ] #NEWLINE# stderr_thread = threading . Thread ( target = readerthread , args = ( stderr_file , stderr ) ) #NEWLINE# stderr_thread . setDaemon ( True ) #NEWLINE# stderr_thread . start ( ) #NEWLINE# #UNINDENT# stdin_file . close ( ) #NEWLINE# if stdout_file : #NEWLINE# #INDENT# stdout_thread . join ( ) #NEWLINE# #UNINDENT# if stderr_file : #NEWLINE# #INDENT# stderr_thread . join ( ) #NEWLINE# #UNINDENT# if ( stdout is not None ) : #NEWLINE# #INDENT# stdout = stdout [ 0 ] #NEWLINE# #UNINDENT# if ( stderr is not None ) : #NEWLINE# #INDENT# stderr = stderr [ 0 ] #NEWLINE# #UNINDENT# if stdout : #NEWLINE# #INDENT# stdout = translate_newlines ( stdout ) #NEWLINE# #UNINDENT# if stderr : #NEWLINE# #INDENT# stderr = translate_newlines ( stderr ) #NEWLINE# #UNINDENT# return_code = wait ( ) #NEWLINE# return ( stdout , stderr , return_code ) #NEWLINE# #UNINDENT# ( ret_stdout , ret_stderr , retcode ) = communicate ( ) #NEWLINE# result = Message ( base64 . b64encode ( str ( ret_stdout ) ) ) #NEWLINE# result . properties [ 'retcode' ] = base64 . b64encode ( str ( retcode ) ) #NEWLINE# if ret_stderr : #NEWLINE# #INDENT# result . properties [ 'stderr' ] = base64 . b64encode ( str ( ret_stderr ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# result . properties [ 'stderr' ] = base64 . b64encode ( '' ) #NEWLINE# #UNINDENT# sender . send ( result ) #NEWLINE# #UNINDENT# except Exception as exception_message : #NEWLINE# #INDENT# result = Message ( base64 . b64encode ( '' ) ) #NEWLINE# result . properties [ 'retcode' ] = base64 . b64encode ( str ( exception_message [ 0 ] ) ) #NEWLINE# result . properties [ 'stderr' ] = base64 . b64encode ( str ( exception_message [ 2 ] ) ) #NEWLINE# sender . send ( result )