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