/* Copyright 1999-2011 (c) My-Channels Copyright (c) 2012-2014 Software AG, Darmstadt, Germany and/or Software AG USA Inc., Reston, VA, USA, and/or its subsidiaries and/or its affiliates and/or their licensors. Use, reproduction, transfer, publication or disclosure is prohibited except as specifically provided for in your License Agreement with Software AG. */ package com.pcbsys.nirvana.apps; import com.pcbsys.foundation.utils.fEnvironment; import com.pcbsys.nirvana.client.*; import com.pcbsys.nirvana.client.p2p.*; import java.util.StringTokenizer; import java.util.Enumeration; import java.util.Vector; import java.io.*; /** * This class allows you to create a remote shell onto a machine where a nirvana server is running */ public class nP2PShell { private static nP2PShell mySelf=null; private static int bufferSize = 1000; private static void processArgs(String[] args){ switch (args.length){ case 1: if (args[0].equals("-?")) UsageEnv(); break; } } private static void processEnvironmentVariable(String variable){ String laxVAR=System.getProperty("lax.nl.env."+variable); if (laxVAR!=null) System.setProperty(variable,laxVAR); } public static void main( String[] args ) { //Process command line arguments processArgs(args); //Process Environment Variables processEnvironmentVariable("RNAME"); processEnvironmentVariable("LOGLEVEL"); processEnvironmentVariable("HPROXY"); processEnvironmentVariable("HAUTH"); processEnvironmentVariable("CKEYSTORE"); processEnvironmentVariable("CKEYSTOREPASSWD"); processEnvironmentVariable("CAKEYSTORE"); processEnvironmentVariable("CAKEYSTOREPASSWD"); System.out.println("***********************************************************"); System.out.println("* *"); System.out.println("* WARNING WARNING WARNING WARNING WARNING WARNING WARNING *"); System.out.println("* This is a sample application demonstrating how to write *"); System.out.println("* a simple nirvana P2P service. This sample is extremely *"); System.out.println("* insecure and should NEVER be run on any production or *"); System.out.println("* secure hosts. The author accepts no liability for any *"); System.out.println("* damage caused *"); System.out.println("* *"); System.out.println("***********************************************************"); // Adjust the HTTP proxy settings // Install any proxy server settings fEnvironment.setProxyEnvironments(); // Install any ssl settings fEnvironment.setSSLEnvironments(); // Get the realm details String[] rproperties = new String[ 4 ]; int idx = 0; String RNAME = System.getProperty( "RNAME" ); if (RNAME == null) { Usage(); System.exit(1); } StringTokenizer st = new StringTokenizer( RNAME, "," ); while ( st.hasMoreTokens() ) { String someRNAME = (String)st.nextToken(); rproperties[ idx ] = someRNAME; idx++; } // Trim the array String[] rpropertiesTrimmed = new String[ idx ]; System.arraycopy( rproperties, 0, rpropertiesTrimmed, 0, idx ); if ( rpropertiesTrimmed.length == 0 ) { System.out.println( "No realm information supplied" ); System.exit( 1 ); } mySelf = new nP2PShell( rpropertiesTrimmed ); } public nP2PShell( String[] rprops ) { if (rprops == null) return; try { nSessionAttributes nsa = null; try { nsa = new nSessionAttributes( rprops ); } catch ( Exception ex ) { System.out.println( "Error creating Session Attributes. Please check your RNAME" ); System.exit( 1 ); } System.out.println( "Creating factory" ); nServiceFactory factory = new nServiceFactory( nsa ); System.out.println( "Getting list of services" ); Enumeration enum1 = factory.getAllServices(); nServiceInfo si = null; Vector res = new Vector(); while ( enum1.hasMoreElements() ) { nServiceInfo info = (nServiceInfo)enum1.nextElement(); if ( info.getName().indexOf( "shell" ) != -1 ) { res.addElement( info ); } } if ( res.size() > 1 ) { System.out.println( "List of available Shell Servers" ); for ( int x = 0; x < res.size(); x++ ) { si = (nServiceInfo)res.elementAt( x ); System.out.println( "" + x + ") " + si.getName() + " : " + si.getDescription() ); } System.out.print( "Enter the number you wish to connect to : " ); si = (nServiceInfo)res.elementAt( 0 ); System.out.flush(); } else if ( res.size() == 1 ) { si = (nServiceInfo)res.elementAt( 0 ); System.out.println( "Service : " + si.getName() + " : " + si.getDescription() ); } else { System.out.println( "No servers are currently active" ); System.exit( 0 ); } if ( si != null ) { nStreamService service = (nStreamService)factory.connectToService( si ); new dumpStream( service ); BufferedInputStream bis = new BufferedInputStream( System.in ); OutputStream os = service.getOutputStream(); byte val; int pos = 0; byte[] command = new byte[ 256 ]; try { boolean run = true; while ( run ) { bis.read( command, pos, 1 ); if ( command[ pos ] == '\n' ) { if ( pos > 0 ) { pos++; byte[] tmp = new byte[ pos ]; System.arraycopy( command, 0, tmp, 0, pos ); os.write( tmp ); os.flush(); pos = 0; } else { run = false; } } else { pos++; } } } catch ( Exception read ) { read.printStackTrace(); } } factory.close(); } catch ( Exception ex ) { ex.printStackTrace(); } } class dumpStream extends Thread { private InputStream myIs; private nStreamService myService; public dumpStream( nStreamService service ) { myService = service; myIs = myService.getInputStream(); start(); } public void run() { byte[] buffer = new byte[ bufferSize ]; try { int size = 0; while ( true ) { size = myIs.read(buffer); if ( size != -1 ) { System.out.write( buffer, 0, size ); System.out.flush(); } } } catch ( Exception ex ) { ex.printStackTrace(); System.out.println( "Shell has terminated" ); System.exit( 0 ); } } } // End of dumpStream Inner Class /** * Prints the usage string for this class */ private static void Usage() { System.out.println( "Usage ...\n" ); System.out.println("np2pshell \n"); System.out.println( "\n\nNote: -? provides help on environment variables \n"); } private static void UsageEnv() { Usage(); //Special case, show the usage string when -? is passed as no arguments are otherwise required System.out.println( "\n\n(Environment Variables) \n"); System.out.println( "(RNAME) - One or more RNAME entries in the form protocol://host:port" ); System.out.println( " protocol - Can be one of nsp, nhp, nsps, or nhps, where:" ); System.out.println( " nsp - Specifies Nirvana Socket Protocol (nsp)" ); System.out.println( " nhp - Specifies Nirvana HTTP Protocol (nhp)" ); System.out.println( " nsps - Specifies Nirvana Socket Protocol Secure (nsps), i.e. using SSL/TLS" ); System.out.println( " nhps - Specifies Nirvana HTTP Protocol Secure (nhps), i.e. using SSL/TLS" ); System.out.println( " port - The port number of the server" ); System.out.println( "\nHint: - For multiple RNAME entries, use comma separated values which will be attempted in connection weight order\n" ); System.out.println( "(LOGLEVEL) - This determines how much information the nirvana api will output 0 = verbose 7 = quiet\n" ); System.out.println( "(CKEYSTORE) - If using SSL, the location of the keystore containing the client cert\n"); System.out.println( "(CKEYSTOREPASSWD) - If using SSL, the password for the keystore containing the client cert\n"); System.out.println( "(CAKEYSTORE) - If using SSL, the location of the ca truststore\n"); System.out.println( "(CAKEYSTOREPASSWD) - If using SSL, the password for the ca truststore\n"); System.out.println( "(HPROXY) - HTTP Proxy details in the form proxyhost:proxyport, where:" ); System.out.println( " proxyhost - The HTTP proxy host" ); System.out.println( " proxyport - The HTTP proxy port\n" ); System.out.println( "(HAUTH) - HTTP Proxy authentication details in the form user:pass, where:" ); System.out.println( " user - The HTTP proxy authentication username" ); System.out.println( " pass - The HTTP proxy authentication password\n" ); System.exit(1); } } // End of nP2PShell Class