/** * CrawlStartScanner_p * Copyright 2010 by Michael Peter Christen, mc@yacy.net, Frankfurt am Main, Germany * First released 12.12.2010 at http://yacy.net * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program in the file lgpl21.txt * If not, see . */ import java.io.IOException; import java.net.InetAddress; import java.net.MalformedURLException; import java.util.ConcurrentModificationException; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeMap; import java.util.regex.Pattern; import net.yacy.cora.federate.solr.connector.AbstractSolrConnector; import net.yacy.cora.order.Base64Order; import net.yacy.cora.protocol.Domains; import net.yacy.cora.protocol.RequestHeader; import net.yacy.cora.protocol.Scanner; import net.yacy.cora.protocol.Scanner.Access; import net.yacy.cora.sorting.ReversibleScoreMap; import net.yacy.data.WorkTables; import net.yacy.kelondro.data.meta.DigestURI; import net.yacy.kelondro.logging.Log; import net.yacy.search.Switchboard; import net.yacy.search.SwitchboardConstants; import net.yacy.search.query.SearchEventCache; import net.yacy.search.schema.CollectionSchema; import net.yacy.server.serverObjects; import net.yacy.server.serverSwitch; public class CrawlStartScanner_p { private final static int CONCURRENT_RUNNER = 200; public static serverObjects respond( @SuppressWarnings("unused") final RequestHeader header, final serverObjects post, final serverSwitch env) { final serverObjects prop = new serverObjects(); final Switchboard sb = (Switchboard) env; // clean up all search events SearchEventCache.cleanupEvents(true); prop.put("noserverdetected", 0); prop.put("hosts", ""); prop.put("intranet.checked", sb.isIntranetMode() ? 1 : 0); int timeout = sb.isIntranetMode() ? 200 : 3000; timeout = post == null ? timeout : post.getInt("timeout", timeout); // make a scanhosts entry String hostt = post == null ? "" : post.get("scanhosts", "").trim(); boolean listall = false; if (hostt.equals("*")) { hostt = ""; listall = true; } String[] hosts0 = hostt.indexOf('\n') > 0 || hostt.indexOf('\r') > 0 ? hostt.split("[\\r\\n]+") : hostt.split(Pattern.quote(",")); Set hostSet = new LinkedHashSet(); for (String s: hosts0) if (s != null && s.length() > 0) hostSet.add(s); final Set ips = Domains.myIntranetIPs(); prop.put("intranethosts", ips.toString()); prop.put("intranetHint", sb.isIntranetMode() ? 0 : 1); if ( hostSet.isEmpty() ) { InetAddress ip; if ( sb.isIntranetMode() ) { if ( !ips.isEmpty() ) { ip = ips.iterator().next(); } else { ip = Domains.dnsResolve("192.168.0.1"); } } else { ip = Domains.myPublicLocalIP(); if ( Domains.isThisHostIP(ip) ) { ip = sb.peers.mySeed().getInetAddress(); } } if ( ip != null ) { hostSet.add(ip.getHostAddress()); } } String hos = ""; for (String s: hostSet) hos += s + "\n"; prop.put("scanhosts", hos.trim()); if (listall) { // get a list of all hosts in the index ReversibleScoreMap hostscore = null; try { hostscore = sb.index.fulltext().getDefaultConnector().getFacets(AbstractSolrConnector.CATCHALL_TERM, 1000, CollectionSchema.host_s.getSolrFieldName()).get(CollectionSchema.host_s.getSolrFieldName()); } catch (IOException e) {} if (hostscore != null) { for (String s: hostscore) hostSet.add(s); } } // parse post requests if ( post != null ) { int repeat_time = 0; String repeat_unit = "seldays"; // check scheduler if ( post.get("rescan", "").equals("scheduler") ) { repeat_time = post.getInt("repeat_time", -1); repeat_unit = post.get("repeat_unit", "selminutes"); // selminutes, selhours, seldays } final int subnet = post.getInt("subnet", 24); // scan a range of ips if (post.containsKey("scan")) { boolean scanftp = "on".equals(post.get("scanftp", "")); boolean scanhttp = "on".equals(post.get("scanhttp", "")); boolean scanhttps = "on".equals(post.get("scanhttps", "")); boolean scansmb = "on".equals(post.get("scansmb", "")); final Set scanbase = new HashSet(); // select host base to scan if ("hosts".equals(post.get("source", ""))) { for (String host: hostSet) { if (host.startsWith("http://")) host = host.substring(7); if (host.startsWith("https://")) host = host.substring(8); if (host.startsWith("ftp://")) host = host.substring(6); if (host.startsWith("smb://")) host = host.substring(6); final int p = host.indexOf('/', 0); if (p >= 0) host = host.substring(0, p); InetAddress ip; if (host.length() > 0) { ip = Domains.dnsResolve(host); if (ip != null) scanbase.add(ip); if (scanftp && !hostSet.contains("ftp." + host)) { ip = Domains.dnsResolve("ftp." + host); if (ip != null) scanbase.add(ip); } if ((scanhttp || scanhttps) && !hostSet.contains("www." + host)) { ip = Domains.dnsResolve("www." + host); if (ip != null) scanbase.add(ip); } } } } if ("intranet".equals(post.get("source", ""))) { scanbase.addAll(Domains.myIntranetIPs()); } // start a scanner final Scanner scanner = new Scanner(CONCURRENT_RUNNER, timeout); List addresses = Scanner.genlist(scanbase, subnet); if (scanftp) scanner.addFTP(addresses); if (scanhttp) scanner.addHTTP(addresses); if (scanhttps) scanner.addHTTPS(addresses); if (scansmb) scanner.addSMB(addresses); scanner.start(); scanner.terminate(); if ("on".equals(post.get("accumulatescancache", "")) && !"scheduler".equals(post.get("rescan", ""))) { Scanner.scancacheExtend(scanner); } else { Scanner.scancacheReplace(scanner); } } // check crawl request if ( post.containsKey("crawl") ) { // make a pk/url mapping final Iterator> se = Scanner.scancacheEntries(); final Map pkmap = new TreeMap(Base64Order.enhancedCoder); while (se.hasNext()) { final Scanner.Service u = se.next().getKey(); DigestURI uu; try { uu = u.url(); pkmap.put(uu.hash(), uu); } catch ( final MalformedURLException e ) { Log.logException(e); } } // search for crawl start requests in this mapping for ( final Map.Entry entry : post.entrySet() ) { if ( entry.getValue().startsWith("mark_") ) { final byte[] pk = entry.getValue().substring(5).getBytes(); final DigestURI url = pkmap.get(pk); if ( url != null ) { String path = "/Crawler_p.html?createBookmark=off&xsstopw=off&crawlingDomMaxPages=10000&intention=&range=domain&indexMedia=on&recrawl=nodoubles&xdstopw=off&storeHTCache=on&sitemapURL=&repeat_time=7&crawlingQ=on&cachePolicy=iffresh&indexText=on&crawlingMode=url&mustnotmatch=&crawlingDomFilterDepth=1&crawlingDomFilterCheck=off&crawlingstart=Start%20New%20Crawl&xpstopw=off&repeat_unit=seldays&crawlingDepth=99&directDocByURL=off"; path += "&crawlingURL=" + url.toNormalform(true); WorkTables.execAPICall( Domains.LOCALHOST, (int) sb.getConfigLong("port", 8090), sb.getConfig(SwitchboardConstants.ADMIN_ACCOUNT_B64MD5, ""), path, pk); } } } } // check scheduler if ( "scheduler".equals(post.get("rescan", "")) ) { // store this call as api call if ( repeat_time > 0 ) { // store as scheduled api call sb.tables.recordAPICall( post, "CrawlStartScanner_p.html", WorkTables.TABLE_API_TYPE_CRAWLER, "network scanner for hosts: " + hostSet.toString(), repeat_time, repeat_unit.substring(3)); } // execute the scan results if ( Scanner.scancacheSize() > 0 ) { // make a comment cache final Map apiCommentCache = WorkTables.commentCache(sb); String urlString; DigestURI u; try { final Iterator> se = Scanner.scancacheEntries(); Map.Entry host; while ( se.hasNext() ) { host = se.next(); try { u = host.getKey().url(); urlString = u.toNormalform(true); if (host.getValue() == Access.granted && Scanner.inIndex(apiCommentCache, urlString) == null ) { String path = "/Crawler_p.html?createBookmark=off&xsstopw=off&crawlingDomMaxPages=10000&intention=&range=domain&indexMedia=on&recrawl=nodoubles&xdstopw=off&storeHTCache=on&sitemapURL=&repeat_time=7&crawlingQ=on&cachePolicy=iffresh&indexText=on&crawlingMode=url&mustnotmatch=&crawlingDomFilterDepth=1&crawlingDomFilterCheck=off&crawlingstart=Start%20New%20Crawl&xpstopw=off&repeat_unit=seldays&crawlingDepth=99"; path += "&crawlingURL=" + urlString; WorkTables.execAPICall( Domains.LOCALHOST, (int) sb.getConfigLong("port", 8090), sb.getConfig(SwitchboardConstants.ADMIN_ACCOUNT_B64MD5, ""), path, u.hash()); } } catch ( final MalformedURLException e ) { Log.logException(e); } } } catch ( final ConcurrentModificationException e ) { } } } } return prop; } }