Mercurial Hosting > luan
annotate src/goodjava/lucene/backup/BackupServer.java @ 1674:af18eacf187c
automatic restore
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 29 May 2022 23:38:34 -0600 |
parents | 1b9f9fdb3b41 |
children | 445048f9eca7 |
rev | line source |
---|---|
1499 | 1 package goodjava.lucene.backup; |
2 | |
3 import java.io.File; | |
4 import java.io.IOException; | |
5 import java.util.Map; | |
6 import java.net.Socket; | |
7 import java.net.ServerSocket; | |
8 import java.util.concurrent.Executors; | |
1502 | 9 import java.util.concurrent.ExecutorService; |
1509 | 10 import javax.net.ssl.SSLSocket; |
1499 | 11 import javax.net.ssl.SSLServerSocket; |
12 import goodjava.util.SoftCacheMap; | |
1501 | 13 import goodjava.io.IoUtils; |
1499 | 14 import goodjava.rpc.RpcServer; |
15 import goodjava.rpc.RpcCall; | |
1509 | 16 import goodjava.rpc.RpcClient; |
17 import goodjava.rpc.RpcResult; | |
1512 | 18 import goodjava.rpc.Rpc; |
1509 | 19 import goodjava.rpc.RpcException; |
1499 | 20 import goodjava.logging.Logger; |
21 import goodjava.logging.LoggerFactory; | |
22 | |
23 | |
1672 | 24 public final class BackupServer { |
1499 | 25 private static final Logger logger = LoggerFactory.getLogger(BackupServer.class); |
26 | |
27 public static int port = 9101; | |
28 public static String[] cipherSuites = new String[] { | |
29 "TLS_DH_anon_WITH_AES_128_GCM_SHA256", | |
30 "TLS_DH_anon_WITH_AES_128_CBC_SHA256", | |
31 "TLS_ECDH_anon_WITH_AES_128_CBC_SHA", | |
32 "TLS_DH_anon_WITH_AES_128_CBC_SHA", | |
33 "TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA", | |
34 "SSL_DH_anon_WITH_3DES_EDE_CBC_SHA", | |
35 "TLS_ECDH_anon_WITH_RC4_128_SHA", | |
36 "SSL_DH_anon_WITH_RC4_128_MD5", | |
37 "SSL_DH_anon_WITH_DES_CBC_SHA", | |
38 "SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA", | |
39 "SSL_DH_anon_EXPORT_WITH_RC4_40_MD5", | |
40 }; | |
41 | |
42 private final File backupDir; | |
1502 | 43 private static final ExecutorService threadPool = Executors.newCachedThreadPool(); |
1499 | 44 private static final Map<String,Backup> backups = new SoftCacheMap<String,Backup>(); |
45 | |
1501 | 46 public BackupServer(File backupDir) throws IOException { |
1499 | 47 this.backupDir = backupDir; |
1501 | 48 IoUtils.mkdirs(backupDir); |
1499 | 49 } |
50 | |
51 public synchronized void start() throws IOException { | |
52 final ServerSocket ss; | |
53 if( cipherSuites == null ) { | |
54 ss = new ServerSocket(port); | |
55 } else { | |
1506
d80395468b4e
ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents:
1502
diff
changeset
|
56 ss = IoUtils.getSSLServerSocketFactory().createServerSocket(port); |
1499 | 57 ((SSLServerSocket)ss).setEnabledCipherSuites(cipherSuites); |
58 } | |
59 threadPool.execute(new Runnable(){public void run() { | |
60 try { | |
61 while(!threadPool.isShutdown()) { | |
62 final Socket socket = ss.accept(); | |
63 threadPool.execute(new Runnable(){public void run() { | |
64 handle(socket); | |
65 }}); | |
66 } | |
67 } catch(IOException e) { | |
68 logger.error("",e); | |
69 } | |
70 }}); | |
71 logger.info("started server on port "+port); | |
72 } | |
73 | |
74 private void handle(Socket socket) { | |
75 RpcServer rpc = new RpcServer(socket); | |
1674 | 76 Backup backup = null; |
77 while( !rpc.isClosed() ) { | |
1499 | 78 RpcCall call = rpc.read(); |
1674 | 79 if( call == null ) |
80 break; | |
81 if( call.cmd.equals("exists") ) { | |
82 String name = (String)call.args[0]; | |
83 rpc.write( new RpcResult(new Object[]{new File(backupDir,name).exists()}) ); | |
84 } else if( call.cmd.equals("login") ) { | |
85 String name = (String)call.args[0]; | |
86 synchronized(backups) { | |
87 backup = backups.get(name); | |
88 if( backup == null ) { | |
89 backup = new Backup(new File(backupDir,name)); | |
90 backups.put(name,backup); | |
91 } | |
92 } | |
93 rpc.write(Rpc.OK); | |
94 } else if( backup != null ) { | |
95 backup.handle(rpc,call); | |
96 } else { | |
1512 | 97 rpc.write( new RpcException("login expected") ); |
98 rpc.close(); | |
99 return; | |
100 } | |
1499 | 101 } |
102 } | |
103 | |
1509 | 104 |
105 // for client | |
106 | |
107 public static RpcClient rpcClient(String backupDomain) throws IOException { | |
108 Socket socket; | |
109 if( BackupServer.cipherSuites == null ) { | |
110 socket = new Socket(backupDomain,BackupServer.port); | |
111 } else { | |
112 socket = IoUtils.getSSLSocketFactory().createSocket(backupDomain,BackupServer.port); | |
113 ((SSLSocket)socket).setEnabledCipherSuites(BackupServer.cipherSuites); | |
114 } | |
115 return new RpcClient(socket); | |
116 } | |
117 | |
1499 | 118 } |