changeset 1690:973d3039c421

backup server checks client domain
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 12 Jun 2022 20:13:24 -0600
parents af22d6c120e4
children 8d4152398825
files src/goodjava/lucene/backup/BackupIndexWriter.java src/goodjava/lucene/backup/BackupServer.java src/goodjava/rpc/RpcCon.java src/luan/modules/lucene/LuceneIndex.java
diffstat 4 files changed, 36 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
diff -r af22d6c120e4 -r 973d3039c421 src/goodjava/lucene/backup/BackupIndexWriter.java
--- a/src/goodjava/lucene/backup/BackupIndexWriter.java	Sun Jun 12 18:48:01 2022 -0600
+++ b/src/goodjava/lucene/backup/BackupIndexWriter.java	Sun Jun 12 20:13:24 2022 -0600
@@ -32,20 +32,22 @@
 public final class BackupIndexWriter extends LoggingIndexWriter {
 	private static final Logger logger = LoggerFactory.getLogger(BackupIndexWriter.class);
 	public static String[] backupDomains;
+	private final String domain;
 	private final String name;
 	private final File dir;
 	private boolean isSyncPending = false;
 	private final ExecutorService exec = Executors.newSingleThreadExecutor();
 
-	public BackupIndexWriter(LuceneIndexWriter indexWriter,File logDir,long logTime,String name)
+	public BackupIndexWriter(LuceneIndexWriter indexWriter,File logDir,long logTime,String domain,String name)
 		throws IOException
 	{
 		super(indexWriter,logDir,logTime);
 		if( backupDomains == null )
 			throw new RuntimeException("must set backupDomains");
+		this.domain = domain;
 		this.name = name;
 		File f = new File(System.getProperty("java.io.tmpdir"));
-		dir = new File(f,"goodjava.lucene/"+name);
+		dir = new File(f,"goodjava.lucene/"+domain+"~"+name);
 		IoUtils.mkdirs(dir);
 	}
 
@@ -140,7 +142,7 @@
 		for( String backupDomain : backupDomains ) {
 			RpcClient rpc = BackupServer.rpcClient(backupDomain);
 			try {
-				RpcCall call = new RpcCall("login",name);
+				RpcCall call = new RpcCall("login",domain,name);
 				rpc.write(call);
 				rpc.read();
 				call = new RpcCall("check",logInfo);
@@ -186,7 +188,7 @@
 		}
 	}
 
-	public static BackupIndexWriter newWithRestore(LuceneIndexWriter indexWriter,File logDir,long logTime,String name)
+	public static BackupIndexWriter newWithRestore(LuceneIndexWriter indexWriter,File logDir,long logTime,String domain,String name)
 		throws IOException
 	{
 		if( !logDir.exists() ) {
@@ -194,7 +196,7 @@
 			try {
 				RpcCall call;
 				RpcResult result;
-				call = new RpcCall("exists",name);
+				call = new RpcCall("exists",domain,name);
 				rpc.write(call);
 				result = rpc.read();
 				boolean exists = (Boolean)result.returnValues[0];
@@ -202,7 +204,7 @@
 					logger.error("restoring "+logDir+" from backup");
 					File zip = File.createTempFile("luan_",".zip");
 					IoUtils.delete(zip);
-					call = new RpcCall("login",name);
+					call = new RpcCall("login",domain,name);
 					rpc.write(call);
 					rpc.read();
 					call = new RpcCall("zip");
@@ -222,7 +224,7 @@
 			}
 			rpc.close();
 		}
-		return new BackupIndexWriter(indexWriter,logDir,logTime,name);
+		return new BackupIndexWriter(indexWriter,logDir,logTime,domain,name);
 	}
 
 }
diff -r af22d6c120e4 -r 973d3039c421 src/goodjava/lucene/backup/BackupServer.java
--- a/src/goodjava/lucene/backup/BackupServer.java	Sun Jun 12 18:48:01 2022 -0600
+++ b/src/goodjava/lucene/backup/BackupServer.java	Sun Jun 12 20:13:24 2022 -0600
@@ -3,8 +3,10 @@
 import java.io.File;
 import java.io.IOException;
 import java.util.Map;
+import java.net.InetAddress;
 import java.net.Socket;
 import java.net.ServerSocket;
+import java.net.UnknownHostException;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ExecutorService;
 import javax.net.ssl.SSLSocket;
@@ -71,6 +73,25 @@
 		logger.info("started server on port "+port);
 	}
 
+	private static String getName(RpcServer rpc,Object[] args) {
+		String domain = (String)args[0];
+		InetAddress addr;
+		try {
+			addr = InetAddress.getByName(domain);
+		} catch(UnknownHostException e) {
+			rpc.write( new RpcException("domain lookup failed") );
+			rpc.close();
+			return null;
+		}
+		if( !rpc.socket.getInetAddress().equals(addr) ) {
+			rpc.write( new RpcException("domain doesn't match client") );
+			rpc.close();
+			return null;
+		}
+		String name = (String)args[1];
+		return name==null ? domain : domain + "~" + name;
+	}
+
 	private void handle(Socket socket) {
 		RpcServer rpc = new RpcServer(socket);
 		Backup backup = null;
@@ -79,10 +100,12 @@
 			if( call == null )
 				break;
 			if( call.cmd.equals("exists") ) {
-				String name = (String)call.args[0];
+				String name = getName(rpc,call.args);
+				if( name==null )  return;
 				rpc.write( new RpcResult(new Object[]{new File(backupDir,name).exists()}) );
 			} else if( call.cmd.equals("login") ) {
-				String name = (String)call.args[0];
+				String name = getName(rpc,call.args);
+				if( name==null )  return;
 				synchronized(backups) {
 					backup = backups.get(name);
 					if( backup == null ) {
diff -r af22d6c120e4 -r 973d3039c421 src/goodjava/rpc/RpcCon.java
--- a/src/goodjava/rpc/RpcCon.java	Sun Jun 12 18:48:01 2022 -0600
+++ b/src/goodjava/rpc/RpcCon.java	Sun Jun 12 20:13:24 2022 -0600
@@ -16,7 +16,7 @@
 
 
 public class RpcCon {
-	private final Socket socket;
+	public final Socket socket;
 	private final DataInputStream in;
 	private final DataOutputStream out;
 	InputStream inBinary = null;
diff -r af22d6c120e4 -r 973d3039c421 src/luan/modules/lucene/LuceneIndex.java
--- a/src/luan/modules/lucene/LuceneIndex.java	Sun Jun 12 18:48:01 2022 -0600
+++ b/src/luan/modules/lucene/LuceneIndex.java	Sun Jun 12 20:13:24 2022 -0600
@@ -209,10 +209,7 @@
 			if( BackupIndexWriter.backupDomains == null ) {
 				writer = new LoggingIndexWriter((LuceneIndexWriter)writer,logDir,logTime);
 			} else {
-				String name = this.domain;
-				if( this.name != null )
-					name += "~" + this.name;
-				writer = BackupIndexWriter.newWithRestore((LuceneIndexWriter)writer,logDir,logTime,name);
+				writer = BackupIndexWriter.newWithRestore((LuceneIndexWriter)writer,logDir,logTime,domain,name);
 			}
 		}
 		reader = DirectoryReader.open(fsDir);