package org.openslx.satellitedaemon;
import java.math.BigInteger;
import java.net.Authenticator;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.openslx.network.StaticProxyAuthenticator;
import org.openslx.network.StaticProxySelector;
import org.openslx.satellitedaemon.filetransfer.FileDownloadWorker;
import org.openslx.satellitedaemon.filetransfer.FileUploadWorker;
import com.btr.proxy.search.wpad.WpadProxySearchStrategy;
import com.btr.proxy.util.ProxyException;
/***********************************************************************************************/
/**
* Main class for uploading images from the HS-Server to the Satellite Server.
*
*/
public class App
{
private static Logger log = Logger.getLogger( App.class );
public static void main( String[] args ) throws NoSuchAlgorithmException
{
BasicConfigurator.configure();
int i = 0;
String arg;
String organizationName;
BigInteger modulus, privExp, pubExp;
String ipAddress;
// Check if there are arguments available and if they start with "--".
if ( i < args.length && args[i].startsWith( "--" ) ) {
// Arguments available, take the first one.
arg = args[i++];
if ( arg.equals( "--checkconfig" ) ) {
if ( checkConfig() ) {
System.exit( 0 );
}
log.error( "Config not valid: existing modulus, private and public exponent no valid key pair." );
System.exit( 2 );
} else if ( arg.equals( "--genid" ) ) {
if ( i < args.length ) {
organizationName = args[i++];
if ( genId( organizationName ) ) {
System.exit( 0 );
}
else
System.exit( 2 );
} else {
log.error( "Illegal option: --genid requires <organizationName>" );
System.exit( 2 );
}
} else if ( arg.equals( "--import" ) ) {
if ( args.length != 5 ) {
log.error( "Illegal option: '--import' requires 4 arguments, <OrgName> <Modulus> <priv_Exponent> <pub_Exponent>" );
System.exit( 2 );
} else {
organizationName = args[i++];
modulus = new BigInteger( args[i++] );
privExp = new BigInteger( args[i++] );
pubExp = new BigInteger( args[i++] );
if ( importId( organizationName, modulus, privExp, pubExp ) ) {
System.exit( 0 );
} else
System.exit( 2 );
}
} else if ( arg.equals( "--submitkey" ) ) {
if ( i < args.length ) {
ipAddress = args[i++];
if ( submitKey( ipAddress ) )
System.exit( 0 );
else
System.exit( 2 );
} else {
log.error( "Illegal option: --submitkey requires <IPADDRESS>" );
System.exit( 2 );
}
} else if ( arg.equals( "--updateaddress" ) ) {
if ( i < args.length ) {
ipAddress = args[i++];
if ( updateAddress( ipAddress ) ) {
System.exit( 0 );
} else
System.exit( 2 );
} else {
log.error( "Illegal option: --updateaddress requires <IPADDRESS>" );
System.exit( 2 );
}
} else {
// no correct option was selected.
log.error( "Illegal option: given argument(s) not valid." );
log.info( "\nUsage: java -jar target/satellite-daemon-1.0-SNAPSHOT-jar-with-dependencies.jar [options]\n"
+ " options:\n"
+ " [--checkconfig] or\n"
+ " [--genid <ORGANIZANTIONNAME>] or\n"
+ " [--import <ORGANIZATIONNAME> <MODULUS> <PRIV_EXP> <PUB_EXP>] or\n"
+ " [--submitkey <IPADDRESS>] or\n"
+ " [--updateaddress <IPADDRESS>]" );
System.exit( 2 );
}
} else if ( args.length == 0 ) {
// No Option choosed, try to load existing identity.
if ( !tryLoadIdentity() ) {
System.exit( 2 );
}
}
if ( !Globals.masterServerSslContextInit() ) {
log.error( "Problem with initializing the SSLContext" );
System.exit( 1 );
}
log.info( "Configure proxy settings ..." );
// Configuring proxy settings. First read options from config file.
String proxyConfiguration = Globals.getProxyConf();
if ( ( proxyConfiguration.equals( "AUTO" ) ) || ( proxyConfiguration.equals( "" ) ) ) {
log.info( "Configuring proxy settings automatically..." );
// Configuring proxy settings automatically.
WpadProxySearchStrategy wPSS = new WpadProxySearchStrategy();
try {
ProxySelector pS = wPSS.getProxySelector();
ProxySelector.setDefault( pS );
} catch ( ProxyException e ) {
log.error( "Setting proxy configuration automatically failed.", e );
}
} else if ( proxyConfiguration.equals( "YES" ) ) {
// Take the proxy settings from config file.
// First check if one of the following necessary options might not be set.
if ( Globals.checkProxySettings() ) {
String proxyAddress = Globals.getProxyAddress();
int proxyPort = Globals.getProxyPort();
// Configure proxy.
Proxy proxy = new Proxy( Proxy.Type.SOCKS, new InetSocketAddress( proxyAddress, proxyPort ) );
StaticProxySelector sPS = new StaticProxySelector( proxy );
ProxySelector.setDefault( sPS );
if ( ! ( Globals.getProxyUsername().equals( "" ) ) && ! ( Globals.getProxyPassword().equals( "" ) ) ) {
log.info( "Configuring proxy settings manually WITH authentication..." );
// use Proxy with authentication.
String proxyUname = Globals.getProxyUsername();
String proxyPass = Globals.getProxyPassword();
// Set authentication.
StaticProxyAuthenticator sPA = new StaticProxyAuthenticator( proxyUname, proxyPass );
Authenticator.setDefault( sPA );
}
}
}
log.info( "... proxy settings are done." );
// Start Up- and Download.
Thread uploadWorker = new Thread( new FileUploadWorker() );
uploadWorker.start();
Thread downloadWorker = new Thread( new FileDownloadWorker() );
downloadWorker.start();
}
private static boolean checkConfig()
{
if ( Identity.getOrganizationName() == null ) {
log.error( "Checking config failed: no existing organization name." );
return false;
}
RSAPublicKey pub = (RSAPublicKey)Identity.getPublicKey();
RSAPrivateKey priv = (RSAPrivateKey)Identity.getPrivateKey();
assert ( pub.getModulus() == priv.getModulus() );
BigInteger modulus = pub.getModulus();
return Identity.isValidKeyPair(
modulus,
priv.getPrivateExponent(),
pub.getPublicExponent() );
}
private static boolean genId( String organizationName )
{
return Identity.generateIdentity( organizationName );
}
private static boolean importId( String organizationName, BigInteger modulus, BigInteger privExp, BigInteger pubExp )
{
if ( !Identity.isValidKeyPair( modulus, privExp, pubExp ) ) {
log.error( "Given arguments not valid: given modulus, private and public exponent no valid key pair." );
return false;
}
return Identity.importIdentity( organizationName, modulus, privExp, pubExp );
}
private static boolean submitKey( String ipAddress )
{
return Identity.submitKey( ipAddress );
}
private static boolean updateAddress( String ipAddress )
{
return Identity.updateAddress( ipAddress );
}
private static boolean tryLoadIdentity()
{
return checkConfig();
}
}