Mercurial Hosting > luan
comparison src/goodjava/lucene/backup/BackupServer.java @ 1690:973d3039c421
backup server checks client domain
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Sun, 12 Jun 2022 20:13:24 -0600 |
| parents | 445048f9eca7 |
| children | aff2309ae510 |
comparison
equal
deleted
inserted
replaced
| 1689:af22d6c120e4 | 1690:973d3039c421 |
|---|---|
| 1 package goodjava.lucene.backup; | 1 package goodjava.lucene.backup; |
| 2 | 2 |
| 3 import java.io.File; | 3 import java.io.File; |
| 4 import java.io.IOException; | 4 import java.io.IOException; |
| 5 import java.util.Map; | 5 import java.util.Map; |
| 6 import java.net.InetAddress; | |
| 6 import java.net.Socket; | 7 import java.net.Socket; |
| 7 import java.net.ServerSocket; | 8 import java.net.ServerSocket; |
| 9 import java.net.UnknownHostException; | |
| 8 import java.util.concurrent.Executors; | 10 import java.util.concurrent.Executors; |
| 9 import java.util.concurrent.ExecutorService; | 11 import java.util.concurrent.ExecutorService; |
| 10 import javax.net.ssl.SSLSocket; | 12 import javax.net.ssl.SSLSocket; |
| 11 import javax.net.ssl.SSLServerSocket; | 13 import javax.net.ssl.SSLServerSocket; |
| 12 import goodjava.util.SoftCacheMap; | 14 import goodjava.util.SoftCacheMap; |
| 69 } | 71 } |
| 70 }}); | 72 }}); |
| 71 logger.info("started server on port "+port); | 73 logger.info("started server on port "+port); |
| 72 } | 74 } |
| 73 | 75 |
| 76 private static String getName(RpcServer rpc,Object[] args) { | |
| 77 String domain = (String)args[0]; | |
| 78 InetAddress addr; | |
| 79 try { | |
| 80 addr = InetAddress.getByName(domain); | |
| 81 } catch(UnknownHostException e) { | |
| 82 rpc.write( new RpcException("domain lookup failed") ); | |
| 83 rpc.close(); | |
| 84 return null; | |
| 85 } | |
| 86 if( !rpc.socket.getInetAddress().equals(addr) ) { | |
| 87 rpc.write( new RpcException("domain doesn't match client") ); | |
| 88 rpc.close(); | |
| 89 return null; | |
| 90 } | |
| 91 String name = (String)args[1]; | |
| 92 return name==null ? domain : domain + "~" + name; | |
| 93 } | |
| 94 | |
| 74 private void handle(Socket socket) { | 95 private void handle(Socket socket) { |
| 75 RpcServer rpc = new RpcServer(socket); | 96 RpcServer rpc = new RpcServer(socket); |
| 76 Backup backup = null; | 97 Backup backup = null; |
| 77 while( !rpc.isClosed() ) { | 98 while( !rpc.isClosed() ) { |
| 78 RpcCall call = rpc.read(); | 99 RpcCall call = rpc.read(); |
| 79 if( call == null ) | 100 if( call == null ) |
| 80 break; | 101 break; |
| 81 if( call.cmd.equals("exists") ) { | 102 if( call.cmd.equals("exists") ) { |
| 82 String name = (String)call.args[0]; | 103 String name = getName(rpc,call.args); |
| 104 if( name==null ) return; | |
| 83 rpc.write( new RpcResult(new Object[]{new File(backupDir,name).exists()}) ); | 105 rpc.write( new RpcResult(new Object[]{new File(backupDir,name).exists()}) ); |
| 84 } else if( call.cmd.equals("login") ) { | 106 } else if( call.cmd.equals("login") ) { |
| 85 String name = (String)call.args[0]; | 107 String name = getName(rpc,call.args); |
| 108 if( name==null ) return; | |
| 86 synchronized(backups) { | 109 synchronized(backups) { |
| 87 backup = backups.get(name); | 110 backup = backups.get(name); |
| 88 if( backup == null ) { | 111 if( backup == null ) { |
| 89 backup = new Backup(new File(backupDir,name)); | 112 backup = new Backup(new File(backupDir,name)); |
| 90 backups.put(name,backup); | 113 backups.put(name,backup); |
