Search in sources :

Example 1 with I2PAppContext

use of net.i2p.I2PAppContext in project i2p.i2p by i2p.

the class SAMStreamSend method main.

public static void main(String[] args) {
    Getopt g = new Getopt("SAM", args, "sxhb:m:o:p:u:v:w:");
    boolean isSSL = false;
    boolean isMaster = false;
    int mode = STREAM;
    String version = "3.3";
    String host = "127.0.0.1";
    String port = "7656";
    String user = null;
    String password = null;
    String opts = "inbound.length=0 outbound.length=0";
    int c;
    while ((c = g.getopt()) != -1) {
        switch(c) {
            case 's':
                isSSL = true;
                break;
            case 'x':
                isMaster = true;
                break;
            case 'm':
                mode = Integer.parseInt(g.getOptarg());
                if (mode < 0 || mode > V1RAW) {
                    System.err.println(USAGE);
                    return;
                }
                break;
            case 'v':
                version = g.getOptarg();
                break;
            case 'b':
                host = g.getOptarg();
                break;
            case 'o':
                opts = opts + ' ' + g.getOptarg();
                break;
            case 'p':
                port = g.getOptarg();
                break;
            case 'u':
                user = g.getOptarg();
                break;
            case 'w':
                password = g.getOptarg();
                break;
            case 'h':
            case '?':
            case ':':
            default:
                System.err.println(USAGE);
                return;
        }
    // switch
    }
    // while
    int startArgs = g.getOptind();
    if (args.length - startArgs != 2) {
        System.err.println(USAGE);
        return;
    }
    if (isMaster) {
        mode += MASTER;
        version = "3.3";
    }
    if ((user == null && password != null) || (user != null && password == null)) {
        System.err.println("both user and password or neither");
        return;
    }
    if (user != null && password != null && VersionComparator.comp(version, "3.2") < 0) {
        System.err.println("user/password require 3.2");
        return;
    }
    I2PAppContext ctx = I2PAppContext.getGlobalContext();
    SAMStreamSend sender = new SAMStreamSend(ctx, host, port, args[startArgs], args[startArgs + 1]);
    sender.startup(version, isSSL, mode, user, password, opts);
}
Also used : Getopt(gnu.getopt.Getopt) I2PAppContext(net.i2p.I2PAppContext)

Example 2 with I2PAppContext

use of net.i2p.I2PAppContext in project i2p.i2p by i2p.

the class SAMStreamSink method main.

public static void main(String[] args) {
    Getopt g = new Getopt("SAM", args, "sxhb:m:p:u:v:w:");
    boolean isSSL = false;
    boolean isMaster = false;
    int mode = STREAM;
    String version = "3.3";
    String host = "127.0.0.1";
    String port = "7656";
    String user = null;
    String password = null;
    String opts = "inbound.length=0 outbound.length=0";
    int c;
    while ((c = g.getopt()) != -1) {
        switch(c) {
            case 's':
                isSSL = true;
                break;
            case 'x':
                isMaster = true;
                break;
            case 'm':
                mode = Integer.parseInt(g.getOptarg());
                if (mode < 0 || mode > FORWARDSSL) {
                    System.err.println(USAGE);
                    return;
                }
                break;
            case 'v':
                version = g.getOptarg();
                break;
            case 'b':
                host = g.getOptarg();
                break;
            case 'o':
                opts = opts + ' ' + g.getOptarg();
                break;
            case 'p':
                port = g.getOptarg();
                break;
            case 'u':
                user = g.getOptarg();
                break;
            case 'w':
                password = g.getOptarg();
                break;
            case 'h':
            case '?':
            case ':':
            default:
                System.err.println(USAGE);
                return;
        }
    // switch
    }
    // while
    int startArgs = g.getOptind();
    if (args.length - startArgs != 2) {
        System.err.println(USAGE);
        return;
    }
    if (isMaster) {
        mode += MASTER;
        version = "3.3";
    }
    if ((user == null && password != null) || (user != null && password == null)) {
        System.err.println("both user and password or neither");
        return;
    }
    if (user != null && password != null && VersionComparator.comp(version, "3.2") < 0) {
        System.err.println("user/password require 3.2");
        return;
    }
    I2PAppContext ctx = I2PAppContext.getGlobalContext();
    SAMStreamSink sink = new SAMStreamSink(ctx, host, port, args[startArgs], args[startArgs + 1]);
    sink.startup(version, isSSL, mode, user, password, opts);
}
Also used : Getopt(gnu.getopt.Getopt) I2PAppContext(net.i2p.I2PAppContext)

Example 3 with I2PAppContext

use of net.i2p.I2PAppContext 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 4 with I2PAppContext

use of net.i2p.I2PAppContext in project i2p.i2p by i2p.

the class BlocklistEntries method main.

/**
 *  BlocklistEntries [-p keystorepw] input.txt keystore.ks you@mail.i2p
 *  File format: One entry per line, # starts a comment, ! starts an unblock entry.
 *  Single IPv4 or IPv6 address only (no mask allowed), or 44-char base 64 router hash.
 *  See MAX_ENTRIES above.
 */
public static void main(String[] args) {
    if (args.length < 3) {
        System.err.println("Usage: BlocklistEntries [-p keystorepw] input.txt keystore.ks you@mail.i2p");
        System.exit(1);
    }
    int st;
    String kspass;
    if (args[0].equals("-p")) {
        kspass = args[1];
        st = 2;
    } else {
        kspass = KeyStoreUtil.DEFAULT_KEYSTORE_PASSWORD;
        st = 0;
    }
    String inputFile = args[st++];
    String privateKeyFile = args[st++];
    String signerName = args[st];
    I2PAppContext ctx = new I2PAppContext();
    List<String> elist = new ArrayList<String>(16);
    List<String> rlist = new ArrayList<String>(4);
    StringBuilder buf = new StringBuilder();
    long now = System.currentTimeMillis();
    String date = RFC3339Date.to3339Date(now);
    buf.append(date).append('\n');
    ;
    BufferedReader br = null;
    try {
        br = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile), "UTF-8"));
        String s = null;
        while ((s = br.readLine()) != null) {
            int index = s.indexOf('#');
            if (index == 0)
                // comment
                continue;
            if (index > 0)
                s = s.substring(0, index);
            s = s.trim();
            if (s.length() < 7) {
                if (s.length() > 0)
                    System.err.println("Bad line: " + s);
                continue;
            }
            if (s.startsWith("!")) {
                rlist.add(s.substring(1));
            } else {
                elist.add(s);
                buf.append(s).append('\n');
                ;
            }
        }
    } catch (IOException ioe) {
        System.err.println("load error from " + args[0]);
        ioe.printStackTrace();
        System.exit(1);
    } finally {
        if (br != null)
            try {
                br.close();
            } catch (IOException ioe) {
            }
    }
    if (elist.isEmpty() && rlist.isEmpty()) {
        System.err.println("nothing to sign");
        System.exit(1);
    }
    if (elist.size() > MAX_ENTRIES) {
        System.err.println("too many blocks, max is " + MAX_ENTRIES);
        System.exit(1);
    }
    for (String s : rlist) {
        buf.append('!').append(s).append('\n');
    }
    SigningPrivateKey spk = null;
    try {
        String keypw = "";
        while (keypw.length() < 6) {
            System.err.print("Enter password for key \"" + signerName + "\": ");
            keypw = DataHelper.readLine(System.in);
            if (keypw == null) {
                System.out.println("\nEOF reading password");
                System.exit(1);
            }
            keypw = keypw.trim();
            if (keypw.length() > 0 && keypw.length() < 6)
                System.out.println("Key password must be at least 6 characters");
        }
        File pkfile = new File(privateKeyFile);
        PrivateKey pk = KeyStoreUtil.getPrivateKey(pkfile, kspass, signerName, keypw);
        if (pk == null) {
            System.out.println("Private key for " + signerName + " not found in keystore " + privateKeyFile);
            System.exit(1);
        }
        spk = SigUtil.fromJavaKey(pk);
    } catch (GeneralSecurityException gse) {
        System.out.println("Error signing input file '" + inputFile + "'");
        gse.printStackTrace();
        System.exit(1);
    } catch (IOException ioe) {
        System.out.println("Error signing input file '" + inputFile + "'");
        ioe.printStackTrace();
        System.exit(1);
    }
    SigType type = spk.getType();
    byte[] data = DataHelper.getUTF8(buf.toString());
    Signature ssig = ctx.dsa().sign(data, spk);
    if (ssig == null) {
        System.err.println("sign failed");
        System.exit(1);
    }
    String bsig = Base64.encode(ssig.getData());
    // verify
    BlocklistEntries ble = new BlocklistEntries(elist.size());
    ble.entries.addAll(elist);
    ble.removes.addAll(rlist);
    ble.supdated = date;
    ble.signer = signerName;
    ble.sig = type.getCode() + ":" + bsig;
    boolean ok = ble.verify(ctx);
    if (!ok) {
        System.err.println("verify failed");
        System.exit(1);
    }
    System.out.println("  <i2p:blocklist signer=\"" + signerName + "\" sig=\"" + type.getCode() + ':' + bsig + "\">");
    System.out.println("    <updated>" + date + "</updated>");
    for (String e : elist) {
        System.out.println("    <i2p:block>" + e + "</i2p:block>");
    }
    for (String e : rlist) {
        System.out.println("    <i2p:unblock>" + e + "</i2p:unblock>");
    }
    System.out.println("  </i2p:blocklist>");
}
Also used : SigningPrivateKey(net.i2p.data.SigningPrivateKey) PrivateKey(java.security.PrivateKey) InputStreamReader(java.io.InputStreamReader) I2PAppContext(net.i2p.I2PAppContext) GeneralSecurityException(java.security.GeneralSecurityException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) SigType(net.i2p.crypto.SigType) SigningPrivateKey(net.i2p.data.SigningPrivateKey) Signature(net.i2p.data.Signature) BufferedReader(java.io.BufferedReader) File(java.io.File)

Example 5 with I2PAppContext

use of net.i2p.I2PAppContext in project i2p.i2p by i2p.

the class I2PSocketManagerFactory method createManager.

private static I2PSocketManager createManager(I2PSession session, Properties opts, String name) {
    I2PAppContext context = I2PAppContext.getGlobalContext();
    // As of 0.9.12, ignore this setting, as jwebcache and i2phex set it to the old value.
    // There is no other valid manager.
    // String classname = opts.getProperty(PROP_MANAGER, DEFAULT_MANAGER);
    String classname = DEFAULT_MANAGER;
    try {
        Class<?> cls = Class.forName(classname);
        if (!I2PSocketManager.class.isAssignableFrom(cls))
            throw new IllegalArgumentException(classname + " is not an I2PSocketManager");
        Constructor<?> con = cls.getConstructor(I2PAppContext.class, I2PSession.class, Properties.class, String.class);
        I2PSocketManager mgr = (I2PSocketManager) con.newInstance(new Object[] { context, session, opts, name });
        return mgr;
    } catch (Throwable t) {
        getLog().log(Log.CRIT, "Error loading " + classname, t);
        throw new IllegalStateException(t);
    }
}
Also used : I2PAppContext(net.i2p.I2PAppContext)

Aggregations

I2PAppContext (net.i2p.I2PAppContext)55 SessionKey (net.i2p.data.SessionKey)13 File (java.io.File)11 IOException (java.io.IOException)11 Properties (java.util.Properties)9 Test (org.junit.Test)7 Getopt (gnu.getopt.Getopt)5 ArrayList (java.util.ArrayList)5 FileInputStream (java.io.FileInputStream)3 FileOutputStream (java.io.FileOutputStream)3 InputStream (java.io.InputStream)3 DataFormatException (net.i2p.data.DataFormatException)3 Log (net.i2p.util.Log)3 SecureFileOutputStream (net.i2p.util.SecureFileOutputStream)3 Encoding (i2p.susi.webmail.encoding.Encoding)2 OutputStream (java.io.OutputStream)2 GeneralSecurityException (java.security.GeneralSecurityException)2 HashSet (java.util.HashSet)2 I2PSession (net.i2p.client.I2PSession)2 NamingService (net.i2p.client.naming.NamingService)2