diff --git a/core/src/main/groovy/com/muwire/core/MuWireSettings.groovy b/core/src/main/groovy/com/muwire/core/MuWireSettings.groovy index 43509aa3..c7980b8a 100644 --- a/core/src/main/groovy/com/muwire/core/MuWireSettings.groovy +++ b/core/src/main/groovy/com/muwire/core/MuWireSettings.groovy @@ -11,6 +11,7 @@ class MuWireSettings { final boolean isLeaf boolean allowUntrusted + boolean allowTrustLists int downloadRetryInterval int updateCheckInterval boolean autoDownloadUpdate @@ -33,6 +34,7 @@ class MuWireSettings { MuWireSettings(Properties props) { isLeaf = Boolean.valueOf(props.get("leaf","false")) allowUntrusted = Boolean.valueOf(props.get("allowUntrusted","true")) + allowTrustLists = Boolean.valueOf(props.get("allowTrustLists","true")) crawlerResponse = CrawlerResponse.valueOf(props.get("crawlerResponse","REGISTERED")) nickname = props.getProperty("nickname","MuWireUser") downloadLocation = new File((String)props.getProperty("downloadLocation", @@ -61,6 +63,7 @@ class MuWireSettings { Properties props = new Properties() props.setProperty("leaf", isLeaf.toString()) props.setProperty("allowUntrusted", allowUntrusted.toString()) + props.setProperty("allowTrustLists", String.valueOf(allowTrustLists)) props.setProperty("crawlerResponse", crawlerResponse.toString()) props.setProperty("nickname", nickname) props.setProperty("downloadLocation", downloadLocation.getAbsolutePath()) diff --git a/core/src/main/groovy/com/muwire/core/connection/ConnectionAcceptor.groovy b/core/src/main/groovy/com/muwire/core/connection/ConnectionAcceptor.groovy index 0b686af9..7b2f0746 100644 --- a/core/src/main/groovy/com/muwire/core/connection/ConnectionAcceptor.groovy +++ b/core/src/main/groovy/com/muwire/core/connection/ConnectionAcceptor.groovy @@ -14,6 +14,7 @@ import com.muwire.core.hostcache.HostCache import com.muwire.core.trust.TrustLevel import com.muwire.core.trust.TrustService import com.muwire.core.upload.UploadManager +import com.muwire.core.util.DataUtil import com.muwire.core.search.InvalidSearchResultException import com.muwire.core.search.ResultsParser import com.muwire.core.search.SearchManager @@ -124,6 +125,9 @@ class ConnectionAcceptor { break case (byte)'P': processPOST(e) + break + case (byte)'T': + processTRUST(e) break default: throw new Exception("Invalid read $read") @@ -242,5 +246,44 @@ class ConnectionAcceptor { e.close() } } + + private void processTRUST(Endpoint e) { + byte[] RUST = new byte[6] + DataInputStream dis = new DataInputStream(e.getInputStream()) + dis.readFully(RUST) + if (RUST != "RUST\r\n".getBytes(StandardCharsets.US_ASCII)) + throw new IOException("Invalid TRUST connection") + String header + while ((header = DataUtil.readTillRN(dis)) != ""); // ignore headers for now + + OutputStream os = e.getOutputStream() + if (!settings.allowTrustLists) { + os.write("403 Not Allowed\r\n\r\n".getBytes(StandardCharsets.US_ASCII)) + os.flush() + e.close() + return + } + + os.write("200 OK\r\n\r\n".getBytes(StandardCharsets.US_ASCII)) + List good = new ArrayList<>(trustService.good.values()) + int size = Math.min(Short.MAX_VALUE * 2, good.size()) + good = good.subList(0, size) + DataOutputStream dos = new DataOutputStream(os) + dos.writeShort(size) + good.each { + it.write(dos) + } + + List bad = new ArrayList<>(trustService.bad.values()) + size = Math.min(Short.MAX_VALUE * 2, bad.size()) + bad = bad.subList(0, size) + dos.writeShort(size) + bad.each { + it.write(dos) + } + + dos.flush() + e.close() + } }