Search in sources :

Example 6 with NamingService

use of net.i2p.client.naming.NamingService in project i2p.i2p by i2p.

the class LocalHTTPServer method serveLocalFile.

/**
 *  Very simple web server.
 *
 *  Serve local files in the docs/ directory, for CSS and images in
 *  error pages, using the reserved address proxy.i2p
 *  (similar to p.p in privoxy).
 *  This solves the problems with including links to the router console,
 *  as assuming the router console is at 127.0.0.1 leads to broken
 *  links if it isn't.
 *
 *  Ignore all request headers (If-Modified-Since, etc.)
 *
 *  There is basic protection here -
 *  FileUtil.readFile() prevents traversal above the base directory -
 *  but inproxy/gateway ops would be wise to block proxy.i2p to prevent
 *  exposing the docs/ directory or perhaps other issues through
 *  uncaught vulnerabilities.
 *  Restrict to the /themes/ directory for now.
 *
 *  @param targetRequest decoded path only, non-null
 *  @param query raw (encoded), may be null
 */
public static void serveLocalFile(OutputStream out, String method, String targetRequest, String query, String proxyNonce) throws IOException {
    // a home page message for the curious...
    if (targetRequest.equals("/")) {
        out.write(OK.getBytes("UTF-8"));
        out.flush();
        return;
    }
    if ((method.equals("GET") || method.equals("HEAD")) && targetRequest.startsWith("/themes/") && !targetRequest.contains("..")) {
        String filename = null;
        try {
            // "/themes/".length
            filename = targetRequest.substring(8);
        } catch (IndexOutOfBoundsException ioobe) {
            return;
        }
        // theme hack
        if (filename.startsWith("console/default/"))
            filename = filename.replaceFirst("default", I2PAppContext.getGlobalContext().getProperty("routerconsole.theme", "light"));
        File themesDir = new File(I2PAppContext.getGlobalContext().getBaseDir(), "docs/themes");
        File file = new File(themesDir, filename);
        if (file.exists() && !file.isDirectory()) {
            String type;
            if (filename.endsWith(".css"))
                type = "text/css";
            else if (filename.endsWith(".ico"))
                type = "image/x-icon";
            else if (filename.endsWith(".png"))
                type = "image/png";
            else if (filename.endsWith(".jpg"))
                type = "image/jpeg";
            else
                type = "text/html";
            out.write("HTTP/1.1 200 OK\r\nContent-Type: ".getBytes("UTF-8"));
            out.write(type.getBytes("UTF-8"));
            out.write("\r\nCache-Control: max-age=86400\r\nConnection: close\r\nProxy-Connection: close\r\n\r\n".getBytes("UTF-8"));
            FileUtil.readFile(filename, themesDir.getAbsolutePath(), out);
            return;
        }
    }
    // Do the add and redirect.
    if (targetRequest.equals("/add")) {
        if (query == null) {
            out.write(ERR_ADD.getBytes("UTF-8"));
            return;
        }
        Map<String, String> opts = new HashMap<String, String>(8);
        // this only works if all keys are followed by =value
        StringTokenizer tok = new StringTokenizer(query, "=&;");
        while (tok.hasMoreTokens()) {
            String k = tok.nextToken();
            if (!tok.hasMoreTokens())
                break;
            String v = tok.nextToken();
            opts.put(decode(k), decode(v));
        }
        String url = opts.get("url");
        String host = opts.get("host");
        String b64Dest = opts.get("dest");
        String nonce = opts.get("nonce");
        String referer = opts.get("referer");
        String book = "privatehosts.txt";
        if (opts.get("master") != null)
            book = "userhosts.txt";
        else if (opts.get("router") != null)
            book = "hosts.txt";
        Destination dest = null;
        if (b64Dest != null) {
            try {
                dest = new Destination(b64Dest);
            } catch (DataFormatException dfe) {
                System.err.println("Bad dest to save?" + b64Dest);
            }
        }
        // System.err.println("nonce        : \"" + nonce         + "\"");
        if (proxyNonce.equals(nonce) && url != null && host != null && dest != null) {
            NamingService ns = I2PAppContext.getGlobalContext().namingService();
            Properties nsOptions = new Properties();
            nsOptions.setProperty("list", book);
            if (referer != null && referer.startsWith("http")) {
                String ref = DataHelper.escapeHTML(referer);
                String from = "<a href=\"" + ref + "\">" + ref + "</a>";
                nsOptions.setProperty("s", _t("Added via address helper from {0}", from));
            } else {
                nsOptions.setProperty("s", _t("Added via address helper"));
            }
            boolean success = ns.put(host, dest, nsOptions);
            writeRedirectPage(out, success, host, book, url);
            return;
        }
        out.write(ERR_ADD.getBytes("UTF-8"));
    } else {
        out.write(ERR_404.getBytes("UTF-8"));
    }
    out.flush();
}
Also used : Destination(net.i2p.data.Destination) StringTokenizer(java.util.StringTokenizer) DataFormatException(net.i2p.data.DataFormatException) HashMap(java.util.HashMap) NamingService(net.i2p.client.naming.NamingService) Properties(java.util.Properties) File(java.io.File)

Example 7 with NamingService

use of net.i2p.client.naming.NamingService in project i2p.i2p by i2p.

the class NamingServiceBean method searchNamingService.

/**
 * depth-first search
 */
private static NamingService searchNamingService(NamingService ns, String srch) {
    String name = ns.getName();
    if (name.equals(srch) || basename(name).equals(srch) || name.equals(DEFAULT_NS))
        return ns;
    List<NamingService> list = ns.getNamingServices();
    if (list != null) {
        for (NamingService nss : list) {
            NamingService rv = searchNamingService(nss, srch);
            if (rv != null)
                return rv;
        }
    }
    return null;
}
Also used : NamingService(net.i2p.client.naming.NamingService)

Example 8 with NamingService

use of net.i2p.client.naming.NamingService in project i2p.i2p by i2p.

the class NamingServiceBean method getNamingService.

/**
 * @return the NamingService for the current file name, or the root NamingService
 */
private NamingService getNamingService() {
    NamingService root = _context.namingService();
    NamingService rv = searchNamingService(root, getFileName());
    return rv != null ? rv : root;
}
Also used : NamingService(net.i2p.client.naming.NamingService)

Example 9 with NamingService

use of net.i2p.client.naming.NamingService in project i2p.i2p by i2p.

the class I2PTunnel method destFromName.

/**
 *  @param i2cpHost may be null
 *  @param i2cpPort may be null
 *  @param user may be null
 *  @param pw may be null
 *  @since 0.9.11
 */
private static Destination destFromName(String name, String i2cpHost, String i2cpPort, boolean isSSL, String user, String pw) throws DataFormatException {
    if ((name == null) || (name.trim().length() <= 0))
        throw new DataFormatException("Empty destination provided");
    I2PAppContext ctx = I2PAppContext.getGlobalContext();
    Log log = ctx.logManager().getLog(I2PTunnel.class);
    if (name.startsWith("file:")) {
        Destination result = new Destination();
        byte[] content = null;
        FileInputStream in = null;
        try {
            in = new FileInputStream(name.substring("file:".length()));
            byte[] buf = new byte[1024];
            int read = DataHelper.read(in, buf);
            content = new byte[read];
            System.arraycopy(buf, 0, content, 0, read);
        } catch (IOException ioe) {
            System.out.println(ioe.getMessage());
            return null;
        } finally {
            if (in != null)
                try {
                    in.close();
                } catch (IOException io) {
                }
        }
        try {
            result.fromByteArray(content);
            return result;
        } catch (RuntimeException ex) {
            if (log.shouldLog(Log.INFO))
                log.info("File is not a binary destination - trying base64");
            try {
                byte[] decoded = Base64.decode(new String(content));
                result.fromByteArray(decoded);
                return result;
            } catch (DataFormatException dfe) {
                if (log.shouldLog(Log.WARN))
                    log.warn("File is not a base64 destination either - failing!");
                return null;
            }
        }
    } else {
        // ask naming service
        name = name.trim();
        NamingService inst = ctx.namingService();
        boolean b32 = name.length() == 60 && name.toLowerCase(Locale.US).endsWith(".b32.i2p");
        Destination d = null;
        if (ctx.isRouterContext() || !b32) {
            // Local lookup.
            // Even though we could do b32 outside router ctx here,
            // we do it below instead so we can set the host and port,
            // which we can't do with lookup()
            d = inst.lookup(name);
            if (d != null || ctx.isRouterContext() || name.length() >= 516)
                return d;
        }
        // Outside router context only,
        // try simple session to ask the router.
        I2PClient client = new I2PSimpleClient();
        Properties opts = new Properties();
        if (i2cpHost != null)
            opts.put(I2PClient.PROP_TCP_HOST, i2cpHost);
        if (i2cpPort != null)
            opts.put(I2PClient.PROP_TCP_PORT, i2cpPort);
        opts.put("i2cp.SSL", Boolean.toString(isSSL));
        if (user != null)
            opts.put("i2cp.username", user);
        if (pw != null)
            opts.put("i2cp.password", pw);
        I2PSession session = null;
        try {
            session = client.createSession(null, opts);
            session.connect();
            d = session.lookupDest(name);
        } catch (I2PSessionException ise) {
            if (log.shouldLog(Log.WARN))
                log.warn("Lookup via router failed", ise);
        } finally {
            if (session != null) {
                try {
                    session.destroySession();
                } catch (I2PSessionException ise) {
                }
            }
        }
        return d;
    }
}
Also used : Destination(net.i2p.data.Destination) I2PAppContext(net.i2p.I2PAppContext) Log(net.i2p.util.Log) IOException(java.io.IOException) OrderedProperties(net.i2p.util.OrderedProperties) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) DataFormatException(net.i2p.data.DataFormatException) NamingService(net.i2p.client.naming.NamingService) I2PSimpleClient(net.i2p.client.I2PSimpleClient) I2PSession(net.i2p.client.I2PSession) I2PSessionException(net.i2p.client.I2PSessionException) I2PClient(net.i2p.client.I2PClient)

Example 10 with NamingService

use of net.i2p.client.naming.NamingService in project i2p.i2p by i2p.

the class Daemon method test.

/**
 * @since 0.9.26
 */
public static void test(String[] args) {
    Properties ctxProps = new Properties();
    String PROP_FORCE = "i2p.naming.blockfile.writeInAppContext";
    ctxProps.setProperty(PROP_FORCE, "true");
    I2PAppContext ctx = new I2PAppContext(ctxProps);
    NamingService ns = getNamingService("hosts.txt");
    File published = new File("test-published.txt");
    Log log = new Log(new File("test-log.txt"));
    SubscriptionList subscriptions = new SubscriptionList("test-sub.txt");
    update(ns, published, subscriptions, log);
    ctx.logManager().flush();
}
Also used : I2PAppContext(net.i2p.I2PAppContext) NamingService(net.i2p.client.naming.NamingService) SingleFileNamingService(net.i2p.client.naming.SingleFileNamingService) Properties(java.util.Properties) OrderedProperties(net.i2p.util.OrderedProperties) File(java.io.File)

Aggregations

NamingService (net.i2p.client.naming.NamingService)10 Properties (java.util.Properties)5 SingleFileNamingService (net.i2p.client.naming.SingleFileNamingService)4 Destination (net.i2p.data.Destination)4 OrderedProperties (net.i2p.util.OrderedProperties)3 File (java.io.File)2 I2PAppContext (net.i2p.I2PAppContext)2 DataFormatException (net.i2p.data.DataFormatException)2 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 SortedMap (java.util.SortedMap)1 StringTokenizer (java.util.StringTokenizer)1 I2PClient (net.i2p.client.I2PClient)1 I2PSession (net.i2p.client.I2PSession)1 I2PSessionException (net.i2p.client.I2PSessionException)1 I2PSimpleClient (net.i2p.client.I2PSimpleClient)1 HostTxtEntry (net.i2p.client.naming.HostTxtEntry)1