annotate src/goodjava/lucene/backup/BackupServer.java @ 1506:d80395468b4e

ssl security in code
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 15 May 2020 18:29:47 -0600
parents 8a7b6b32c691
children 0ba144491a42
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 package goodjava.lucene.backup;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 import java.io.File;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 import java.io.IOException;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 import java.util.Map;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 import java.net.Socket;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 import java.net.ServerSocket;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 import java.util.concurrent.Executors;
1502
8a7b6b32c691 minor threads
Franklin Schmidt <fschmidt@gmail.com>
parents: 1501
diff changeset
9 import java.util.concurrent.ExecutorService;
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 import javax.net.ssl.SSLServerSocket;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 import goodjava.util.SoftCacheMap;
1501
Franklin Schmidt <fschmidt@gmail.com>
parents: 1499
diff changeset
12 import goodjava.io.IoUtils;
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 import goodjava.rpc.RpcServer;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 import goodjava.rpc.RpcCall;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 import goodjava.logging.Logger;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 import goodjava.logging.LoggerFactory;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 public class BackupServer {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 private static final Logger logger = LoggerFactory.getLogger(BackupServer.class);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 public static int port = 9101;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 public static String[] cipherSuites = new String[] {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 "TLS_DH_anon_WITH_AES_128_GCM_SHA256",
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 "TLS_DH_anon_WITH_AES_128_CBC_SHA256",
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 "TLS_ECDH_anon_WITH_AES_128_CBC_SHA",
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 "TLS_DH_anon_WITH_AES_128_CBC_SHA",
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 "TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA",
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 "SSL_DH_anon_WITH_3DES_EDE_CBC_SHA",
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 "TLS_ECDH_anon_WITH_RC4_128_SHA",
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 "SSL_DH_anon_WITH_RC4_128_MD5",
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 "SSL_DH_anon_WITH_DES_CBC_SHA",
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 "SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA",
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 "SSL_DH_anon_EXPORT_WITH_RC4_40_MD5",
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 };
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 private final File backupDir;
1502
8a7b6b32c691 minor threads
Franklin Schmidt <fschmidt@gmail.com>
parents: 1501
diff changeset
38 private static final ExecutorService threadPool = Executors.newCachedThreadPool();
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 private static final Map<String,Backup> backups = new SoftCacheMap<String,Backup>();
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40
1501
Franklin Schmidt <fschmidt@gmail.com>
parents: 1499
diff changeset
41 public BackupServer(File backupDir) throws IOException {
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 this.backupDir = backupDir;
1501
Franklin Schmidt <fschmidt@gmail.com>
parents: 1499
diff changeset
43 IoUtils.mkdirs(backupDir);
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 public synchronized void start() throws IOException {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 final ServerSocket ss;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 if( cipherSuites == null ) {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 ss = new ServerSocket(port);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 } else {
1506
d80395468b4e ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents: 1502
diff changeset
51 ss = IoUtils.getSSLServerSocketFactory().createServerSocket(port);
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 ((SSLServerSocket)ss).setEnabledCipherSuites(cipherSuites);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 threadPool.execute(new Runnable(){public void run() {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 try {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 while(!threadPool.isShutdown()) {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 final Socket socket = ss.accept();
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 threadPool.execute(new Runnable(){public void run() {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 handle(socket);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 }});
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 } catch(IOException e) {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 logger.error("",e);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65 }});
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 logger.info("started server on port "+port);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
69 private void handle(Socket socket) {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 RpcServer rpc = new RpcServer(socket);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71 while( !rpc.isClosed() ) {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 RpcCall call = rpc.read();
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73 if( call == null )
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
74 break;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 String name = (String)call.args[0];
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76 Backup backup;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
77 synchronized(backups) {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
78 backup = backups.get(name);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79 if( backup == null ) {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
80 backup = new Backup(new File(backupDir,name));
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
81 backups.put(name,backup);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84 backup.handle(rpc,call);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
85 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
86 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
87
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88 }