Search in sources :

Example 6 with Getopt

use of gnu.getopt.Getopt in project i2p.i2p by i2p.

the class Storage method main.

/**
 *  Create a metainfo.
 *  Used in the installer build process; do not comment out.
 *  @since 0.9.4
 */
public static void main(String[] args) {
    boolean error = false;
    String created_by = null;
    String announce = null;
    Getopt g = new Getopt("Storage", args, "a:c:");
    try {
        int c;
        while ((c = g.getopt()) != -1) {
            switch(c) {
                case 'a':
                    announce = g.getOptarg();
                    break;
                case 'c':
                    created_by = g.getOptarg();
                    break;
                case '?':
                case ':':
                default:
                    error = true;
                    break;
            }
        // switch
        }
    // while
    } catch (RuntimeException e) {
        e.printStackTrace();
        error = true;
    }
    if (error || args.length - g.getOptind() != 1) {
        System.err.println("Usage: Storage [-a announceURL] [-c created-by] file-or-dir");
        System.exit(1);
    }
    File base = new File(args[g.getOptind()]);
    I2PAppContext ctx = I2PAppContext.getGlobalContext();
    I2PSnarkUtil util = new I2PSnarkUtil(ctx);
    File file = null;
    FileOutputStream out = null;
    try {
        Storage storage = new Storage(util, base, announce, null, created_by, false, null);
        MetaInfo meta = storage.getMetaInfo();
        file = new File(storage.getBaseName() + ".torrent");
        out = new FileOutputStream(file);
        out.write(meta.getTorrentData());
        String hex = DataHelper.toString(meta.getInfoHash());
        System.out.println("Created:     " + file);
        System.out.println("InfoHash:    " + hex);
        String basename = base.getName().replace(" ", "%20");
        String magnet = MagnetURI.MAGNET_FULL + hex + "&dn=" + basename;
        if (announce != null)
            magnet += "&tr=" + announce;
        System.out.println("Magnet:      " + magnet);
    } catch (IOException ioe) {
        if (file != null)
            file.delete();
        ioe.printStackTrace();
        System.exit(1);
    } finally {
        try {
            if (out != null)
                out.close();
        } catch (IOException ioe) {
        }
    }
}
Also used : Getopt(gnu.getopt.Getopt) I2PAppContext(net.i2p.I2PAppContext) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) SecureFile(net.i2p.util.SecureFile) File(java.io.File)

Example 7 with Getopt

use of gnu.getopt.Getopt in project i2p.i2p by i2p.

the class EepGet method main.

/**
 * EepGet [-p 127.0.0.1:4444] [-n #retries] [-e etag] [-o outputFile] [-m markSize lineLen] url
 */
public static void main(String[] args) {
    String proxyHost = "127.0.0.1";
    int proxyPort = 4444;
    int numRetries = 0;
    int markSize = 1024;
    int lineLen = 40;
    long inactivityTimeout = INACTIVITY_TIMEOUT;
    String etag = null;
    String saveAs = null;
    List<String> extra = null;
    String username = null;
    String password = null;
    boolean error = false;
    // 
    // note: if you add options, please update installer/resources/man/eepget.1
    // 
    Getopt g = new Getopt("eepget", args, "p:cn:t:e:o:m:l:h:u:x:");
    try {
        int c;
        while ((c = g.getopt()) != -1) {
            switch(c) {
                case 'p':
                    String s = g.getOptarg();
                    int colon = s.indexOf(':');
                    if (colon >= 0) {
                        // Todo IPv6 [a:b:c]:4444
                        proxyHost = s.substring(0, colon);
                        String port = s.substring(colon + 1);
                        proxyPort = Integer.parseInt(port);
                    } else {
                        proxyHost = s;
                    // proxyPort remains default
                    }
                    break;
                case 'c':
                    // no proxy, same as -p :0
                    proxyHost = "";
                    proxyPort = 0;
                    break;
                case 'n':
                    numRetries = Integer.parseInt(g.getOptarg());
                    break;
                case 't':
                    inactivityTimeout = 1000 * Integer.parseInt(g.getOptarg());
                    break;
                case 'e':
                    etag = "\"" + g.getOptarg() + "\"";
                    break;
                case 'o':
                    saveAs = g.getOptarg();
                    break;
                case 'm':
                    markSize = Integer.parseInt(g.getOptarg());
                    break;
                case 'l':
                    lineLen = Integer.parseInt(g.getOptarg());
                    break;
                case 'h':
                    String a = g.getOptarg();
                    int eq = a.indexOf('=');
                    if (eq > 0) {
                        if (extra == null)
                            extra = new ArrayList<String>(2);
                        String key = a.substring(0, eq);
                        String val = a.substring(eq + 1);
                        extra.add(key);
                        extra.add(val);
                    } else {
                        error = true;
                    }
                    break;
                case 'u':
                    username = g.getOptarg();
                    break;
                case 'x':
                    password = g.getOptarg();
                    break;
                case '?':
                case ':':
                default:
                    error = true;
                    break;
            }
        // switch
        }
    // while
    } catch (RuntimeException e) {
        e.printStackTrace();
        error = true;
    }
    if (error || args.length - g.getOptind() != 1) {
        usage();
        System.exit(1);
    }
    String url = args[g.getOptind()];
    if (saveAs == null)
        saveAs = suggestName(url);
    EepGet get = new EepGet(I2PAppContext.getGlobalContext(), true, proxyHost, proxyPort, numRetries, saveAs, url, true, etag);
    if (extra != null) {
        for (int i = 0; i < extra.size(); i += 2) {
            get.addHeader(extra.get(i), extra.get(i + 1));
        }
    }
    if (username != null) {
        if (password == null) {
            try {
                BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
                do {
                    System.err.print("Proxy password: ");
                    password = r.readLine();
                    if (password == null)
                        throw new IOException();
                    password = password.trim();
                } while (password.length() <= 0);
            } catch (IOException ioe) {
                System.exit(1);
            }
        }
        get.addAuthorization(username, password);
    }
    get.addStatusListener(get.new CLIStatusListener(markSize, lineLen));
    if (!get.fetch(CONNECT_TIMEOUT, -1, inactivityTimeout))
        System.exit(1);
}
Also used : InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Getopt(gnu.getopt.Getopt) BufferedReader(java.io.BufferedReader)

Example 8 with Getopt

use of gnu.getopt.Getopt in project i2p.i2p by i2p.

the class PartialEepGet method main.

/**
 * PartialEepGet [-p 127.0.0.1:4444] [-l #bytes] url
 */
public static void main(String[] args) {
    String proxyHost = "127.0.0.1";
    int proxyPort = 4444;
    // 40 sig + 16 version for .suds
    long size = 56;
    String saveAs = null;
    String username = null;
    String password = null;
    boolean error = false;
    Getopt g = new Getopt("partialeepget", args, "p:cl:o:u:x:");
    try {
        int c;
        while ((c = g.getopt()) != -1) {
            switch(c) {
                case 'p':
                    String s = g.getOptarg();
                    int colon = s.indexOf(':');
                    if (colon >= 0) {
                        // Todo IPv6 [a:b:c]:4444
                        proxyHost = s.substring(0, colon);
                        String port = s.substring(colon + 1);
                        proxyPort = Integer.parseInt(port);
                    } else {
                        proxyHost = s;
                    // proxyPort remains default
                    }
                    break;
                case 'c':
                    // no proxy, same as -p :0
                    proxyHost = "";
                    proxyPort = 0;
                    break;
                case 'l':
                    size = Long.parseLong(g.getOptarg());
                    break;
                case 'o':
                    saveAs = g.getOptarg();
                    break;
                case 'u':
                    username = g.getOptarg();
                    break;
                case 'x':
                    password = g.getOptarg();
                    break;
                case '?':
                case ':':
                default:
                    error = true;
                    break;
            }
        // switch
        }
    // while
    } catch (RuntimeException e) {
        e.printStackTrace();
        error = true;
    }
    if (error || args.length - g.getOptind() != 1) {
        usage();
        System.exit(1);
    }
    String url = args[g.getOptind()];
    if (saveAs == null)
        saveAs = suggestName(url);
    OutputStream out;
    try {
        // resume from a previous eepget won't work right doing it this way
        out = new FileOutputStream(saveAs);
    } catch (IOException ioe) {
        System.err.println("Failed to create output file " + saveAs);
        // dummy for compiler
        out = null;
        System.exit(1);
    }
    EepGet get = new PartialEepGet(I2PAppContext.getGlobalContext(), proxyHost, proxyPort, out, url, size);
    if (username != null) {
        if (password == null) {
            try {
                BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
                do {
                    System.err.print("Proxy password: ");
                    password = r.readLine();
                    if (password == null)
                        throw new IOException();
                    password = password.trim();
                } while (password.length() <= 0);
            } catch (IOException ioe) {
                System.exit(1);
            }
        }
        get.addAuthorization(username, password);
    }
    get.addStatusListener(get.new CLIStatusListener(1024, 40));
    if (get.fetch(45 * 1000, -1, 60 * 1000)) {
        System.err.println("Last-Modified: " + get.getLastModified());
        System.err.println("Etag: " + get.getETag());
    } else {
        System.err.println("Failed " + url);
        System.exit(1);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Getopt(gnu.getopt.Getopt) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader)

Example 9 with Getopt

use of gnu.getopt.Getopt in project i2p.i2p by i2p.

the class LocalClientManager method main.

public static void main(String[] args) {
    int dropX1000 = 0, jitter = 0, latency = 0;
    boolean error = false;
    Getopt g = new Getopt("router", args, "d:j:l:");
    try {
        int c;
        while ((c = g.getopt()) != -1) {
            switch(c) {
                case 'd':
                    dropX1000 = (int) (1000 * Double.parseDouble(g.getOptarg()));
                    if (dropX1000 < 0 || dropX1000 >= 100 * 1000)
                        error = true;
                    break;
                case 'j':
                    jitter = Integer.parseInt(g.getOptarg());
                    if (jitter < 0)
                        error = true;
                    break;
                case 'l':
                    latency = Integer.parseInt(g.getOptarg());
                    if (latency < 0)
                        error = true;
                    break;
                default:
                    error = true;
            }
        }
    } catch (RuntimeException e) {
        e.printStackTrace();
        error = true;
    }
    if (error || args.length - g.getOptind() > 0) {
        usage();
        System.exit(1);
    }
    RouterContext ctx = new RouterContext(null);
    int port = ClientManagerFacadeImpl.DEFAULT_PORT;
    LocalClientManager mgr = new LocalClientManager(ctx, port);
    mgr.dropX1000 = dropX1000;
    mgr.jitter = jitter;
    mgr.latency = latency;
    mgr.start();
    System.out.println("Listening on port " + port);
    try {
        Thread.sleep(60 * 60 * 1000);
    } catch (InterruptedException ie) {
    }
    System.out.println("Done listening on port " + port);
}
Also used : Getopt(gnu.getopt.Getopt) RouterContext(net.i2p.router.RouterContext)

Example 10 with Getopt

use of gnu.getopt.Getopt in project narchy by automenta.

the class RDPClientChooser method RunMacRemoteDesktopConnection.

// 
// Private Class method to run the Microsoft MAC OS X Remote Desktop
// Connection (RDP) Client
// 
/**
 * Private method to run the Mac OS RDP client provided by Microsoft
 *
 * @param args Arguments to provide to native client
 */
private static boolean RunMacRemoteDesktopConnection(String[] args) throws IOException {
    logger.info("RunMacRemoteDesktopConnection()");
    LongOpt[] alo = new LongOpt[4];
    int c;
    String arg;
    Options.windowTitle = "Remote Desktop Connection";
    // Process arguments (there are more than we need now - need to reduce -
    // also need to check for correct args)
    Getopt g = new Getopt("properJavaRDP", args, "bc:d:f::g:k:l:n:p:s:t:T:u:", alo);
    while ((c = g.getopt()) != -1) {
        switch(c) {
            case 'd':
                Options.domain = g.getOptarg();
                break;
            case 'n':
                Options.hostname = g.getOptarg();
                break;
            case 'p':
                Options.password = g.getOptarg();
                break;
            case 't':
                arg = g.getOptarg();
                try {
                    Options.port = Integer.parseInt(arg);
                } catch (Exception e) {
                }
                break;
            case 'T':
                Options.windowTitle = g.getOptarg().replace('_', ' ');
                break;
            case 'u':
                Options.username = g.getOptarg();
                break;
            case '?':
            default:
                break;
        }
    }
    // Obtain Server name and possibly port from command args
    String server = null;
    if (g.getOptind() < args.length) {
        int colonat = args[args.length - 1].indexOf(':', 0);
        if (colonat == -1) {
            server = args[args.length - 1];
        } else {
            server = args[args.length - 1].substring(0, colonat);
            Options.port = Integer.parseInt(args[args.length - 1].substring(colonat + 1));
        }
    } else {
        logger.warn("Server name required");
        return false;
    }
    // Create a temporary directory from which to run RDC - we do this so
    // that
    // we can run multiple instances
    String rdproot = "/var/tmp/RDP-" + Options.hostname + '-' + Options.port;
    try {
        new File(rdproot).mkdir();
    } catch (Exception e) {
        logger.warn("Failed to create directory {}", rdproot);
        return false;
    }
    // Dynamically create the RDP config file based on args passed.
    logger.info("Creating RDP Config in {}", rdproot);
    FileWriter rdpConfigFile = new FileWriter(rdproot + "/Default.rdp");
    rdpConfigFile.write("screen mode id:i:0\n");
    rdpConfigFile.write("startdisplay:i:0\n");
    // full screen - this
    rdpConfigFile.write("desktop size id:i:6\n");
    // needs to be mapped
    // from geometry param
    // passed in TBD
    rdpConfigFile.write("desktopwidth:i:1280\n");
    rdpConfigFile.write("desktopheight:i:854\n");
    rdpConfigFile.write("autoshowmenu:i:1\n");
    rdpConfigFile.write("desktopallowresize:i:1\n");
    // 256 colors
    rdpConfigFile.write("session bpp:i:8\n");
    rdpConfigFile.write("winposstr:s:0,3,0,0,800,600\n");
    rdpConfigFile.write("auto connect:i:1\n");
    rdpConfigFile.write("full address:s:" + server + ':' + Options.port + '\n');
    rdpConfigFile.write("compression:i:1\n");
    rdpConfigFile.write("rightclickmodifiers:i:4608\n");
    rdpConfigFile.write("altkeyreplacement:i:0\n");
    rdpConfigFile.write("audiomode:i:1\n");
    rdpConfigFile.write("redirectdrives:i:1\n");
    rdpConfigFile.write("redirectprinters:i:1\n");
    rdpConfigFile.write("username:s:" + Options.username + '\n');
    rdpConfigFile.write("clear password:s:" + Options.password + '\n');
    rdpConfigFile.write("domain:s:" + Options.domain + '\n');
    rdpConfigFile.write("alternate shell:s:\n");
    rdpConfigFile.write("shell working directory:s:\n");
    rdpConfigFile.write("preference flag id:i:2\n");
    rdpConfigFile.write("disable wallpaper:i:1\n");
    rdpConfigFile.write("disable full window drag:i:0\n");
    rdpConfigFile.write("disable menu anims:i:0\n");
    rdpConfigFile.write("disable themes:i:0\n");
    rdpConfigFile.write("disable cursor setting:i:0\n");
    rdpConfigFile.write("bitmapcachepersistenable:i:1\n");
    rdpConfigFile.write("Min Send Interval:i:5\n");
    rdpConfigFile.write("Order Draw Threshold:i:5\n");
    rdpConfigFile.write("Max Event Count:i:150\n");
    rdpConfigFile.write("Normal Event Count:i:150\n");
    rdpConfigFile.write("BitMapCacheSize:i:3500\n");
    rdpConfigFile.write("Keyboard Layout:i:en-uk\n");
    rdpConfigFile.close();
    if (new File(System.getProperty("user.home") + "/Library/Preferences/Microsoft/RDC Client").exists()) {
        FileWriter recentServersFile = new FileWriter(System.getProperty("user.home") + "/Library/Preferences/Microsoft/RDC Client/Recent Servers");
        recentServersFile.write(server + "\r1\r");
        recentServersFile.close();
    }
    // Copy the RDP Client application to a temporary directory to allow
    // multiple copies to run. Note here that we use the MAC OS X ditto
    // command because
    // a normal copy of the executable would not copy the advanced OS X
    // attributes which (among other things) denote the file as an
    // "application".
    String[] appcopycmd = { "/bin/sh", "-c", "ditto -rsrc /Applications/Remote\\ Desktop\\ Connection/Remote\\ Desktop\\ Connection " + rdproot + "/ >/dev/null 2>/dev/null" };
    try {
        Runtime.getRuntime().exec(appcopycmd);
    } catch (IOException e) {
        logger.warn("Unable to copy application to temporary directory");
        return false;
    }
    try {
        Process p = Runtime.getRuntime().exec(appcopycmd);
        logger.warn("RDP Client copied to {}", rdproot);
        try {
            // Wait for the command to complete
            p.waitFor();
        } catch (InterruptedException e) {
            logger.warn("Unable to wait for application to copy");
            return false;
        }
    } catch (IOException e) {
        logger.warn("Unable to copy application to temporary directory");
        return false;
    }
    // Move the application to the name of the title so that the running
    // application shows when using ALT-TAB etc.
    String[] mvcmd = { "/bin/sh", "-c", "mv " + rdproot + "/Remote\\ Desktop\\ Connection '" + rdproot + '/' + Options.windowTitle + "' >/dev/null 2>/dev/null" };
    try {
        Process p = Runtime.getRuntime().exec(mvcmd);
        try {
            // Wait for the mv command to complete
            p.waitFor();
        } catch (InterruptedException e) {
            logger.warn("Unable to wait for application to run");
            return false;
        }
    } catch (IOException e) {
        logger.warn("Unable to move application");
        return false;
    }
    // Run an instance of the RDP Client using the Mac OS X "open" command
    String[] rdpcmd = { "/bin/sh", "-c", "open -a '" + rdproot + '/' + Options.windowTitle + "' " + rdproot + "/Default.rdp >/dev/null 2>/dev/null" };
    try {
        Process p = Runtime.getRuntime().exec(rdpcmd);
        logger.info("RDP Client Launched from {}", rdproot);
        try {
            // Wait for the open command to complete
            p.waitFor();
        } catch (InterruptedException e) {
            logger.warn("Unable to wait for application to run");
            return false;
        }
    } catch (IOException e) {
        logger.warn("Unable to open (run) application");
        return false;
    }
    try {
        Thread.sleep(10000);
    } catch (Exception e) {
        logger.info("Unable to wait for 10 seconds");
        return false;
    }
    // Remove the tempory directory
    String[] rmcmd = { "/bin/sh", "-c", "rm -r " + rdproot + " >/dev/null 2>/dev/null" };
    try {
        Runtime.getRuntime().exec(rmcmd);
    } catch (IOException e) {
        logger.warn("Unable to remove temporary directory {}", rdproot);
        return true;
    }
    logger.warn("RDP Client Completed");
    return true;
}
Also used : Getopt(gnu.getopt.Getopt) LongOpt(gnu.getopt.LongOpt) FileWriter(java.io.FileWriter) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException)

Aggregations

Getopt (gnu.getopt.Getopt)26 File (java.io.File)11 IOException (java.io.IOException)10 LongOpt (gnu.getopt.LongOpt)9 I2PAppContext (net.i2p.I2PAppContext)5 BufferedReader (java.io.BufferedReader)4 InputStream (java.io.InputStream)4 InputStreamReader (java.io.InputStreamReader)4 ArrayList (java.util.ArrayList)4 FileInputStream (java.io.FileInputStream)3 RandomAccessFile (java.io.RandomAccessFile)3 Properties (java.util.Properties)3 FileOutputStream (java.io.FileOutputStream)2 FileWriter (java.io.FileWriter)2 SocketException (java.net.SocketException)2 UnknownHostException (java.net.UnknownHostException)2 HashMap (java.util.HashMap)2 OrderedProperties (net.i2p.util.OrderedProperties)2 KeyCode_FileBased (automenta.rdp.keymapping.KeyCode_FileBased)1 KeyCode_FileBased_Localised (automenta.rdp.rdp.KeyCode_FileBased_Localised)1