Mercurial Hosting > luan
annotate src/goodjava/lucene/backup/BackupServer.java @ 1593:ea62c2316b2e
docs
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sat, 20 Mar 2021 22:29:01 -0600 |
parents | 31b543826ca9 |
children | 8dd8c556c449 |
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; | |
1512 | 7 import java.io.Writer; |
8 import java.io.FileWriter; | |
9 import java.io.Reader; | |
10 import java.io.FileReader; | |
1499 | 11 import java.io.IOException; |
12 import java.util.Map; | |
13 import java.net.Socket; | |
14 import java.net.ServerSocket; | |
15 import java.util.concurrent.Executors; | |
1502 | 16 import java.util.concurrent.ExecutorService; |
1509 | 17 import javax.net.ssl.SSLSocket; |
1499 | 18 import javax.net.ssl.SSLServerSocket; |
19 import goodjava.util.SoftCacheMap; | |
1501 | 20 import goodjava.io.IoUtils; |
1499 | 21 import goodjava.rpc.RpcServer; |
22 import goodjava.rpc.RpcCall; | |
1509 | 23 import goodjava.rpc.RpcClient; |
24 import goodjava.rpc.RpcResult; | |
1512 | 25 import goodjava.rpc.Rpc; |
1509 | 26 import goodjava.rpc.RpcException; |
1499 | 27 import goodjava.logging.Logger; |
28 import goodjava.logging.LoggerFactory; | |
29 | |
30 | |
31 public class BackupServer { | |
32 private static final Logger logger = LoggerFactory.getLogger(BackupServer.class); | |
33 | |
34 public static int port = 9101; | |
35 public static String[] cipherSuites = new String[] { | |
36 "TLS_DH_anon_WITH_AES_128_GCM_SHA256", | |
37 "TLS_DH_anon_WITH_AES_128_CBC_SHA256", | |
38 "TLS_ECDH_anon_WITH_AES_128_CBC_SHA", | |
39 "TLS_DH_anon_WITH_AES_128_CBC_SHA", | |
40 "TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA", | |
41 "SSL_DH_anon_WITH_3DES_EDE_CBC_SHA", | |
42 "TLS_ECDH_anon_WITH_RC4_128_SHA", | |
43 "SSL_DH_anon_WITH_RC4_128_MD5", | |
44 "SSL_DH_anon_WITH_DES_CBC_SHA", | |
45 "SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA", | |
46 "SSL_DH_anon_EXPORT_WITH_RC4_40_MD5", | |
47 }; | |
48 | |
49 private final File backupDir; | |
1502 | 50 private static final ExecutorService threadPool = Executors.newCachedThreadPool(); |
1499 | 51 private static final Map<String,Backup> backups = new SoftCacheMap<String,Backup>(); |
52 | |
1501 | 53 public BackupServer(File backupDir) throws IOException { |
1499 | 54 this.backupDir = backupDir; |
1501 | 55 IoUtils.mkdirs(backupDir); |
1499 | 56 } |
57 | |
58 public synchronized void start() throws IOException { | |
59 final ServerSocket ss; | |
60 if( cipherSuites == null ) { | |
61 ss = new ServerSocket(port); | |
62 } else { | |
1506
d80395468b4e
ssl security in code
Franklin Schmidt <fschmidt@gmail.com>
parents:
1502
diff
changeset
|
63 ss = IoUtils.getSSLServerSocketFactory().createServerSocket(port); |
1499 | 64 ((SSLServerSocket)ss).setEnabledCipherSuites(cipherSuites); |
65 } | |
66 threadPool.execute(new Runnable(){public void run() { | |
67 try { | |
68 while(!threadPool.isShutdown()) { | |
69 final Socket socket = ss.accept(); | |
70 threadPool.execute(new Runnable(){public void run() { | |
71 handle(socket); | |
72 }}); | |
73 } | |
74 } catch(IOException e) { | |
75 logger.error("",e); | |
76 } | |
77 }}); | |
78 logger.info("started server on port "+port); | |
79 } | |
80 | |
81 private void handle(Socket socket) { | |
82 RpcServer rpc = new RpcServer(socket); | |
1512 | 83 Backup backup; |
84 { | |
1499 | 85 RpcCall call = rpc.read(); |
1512 | 86 if( !call.cmd.equals("login") ) { |
87 rpc.write( new RpcException("login expected") ); | |
88 rpc.close(); | |
89 return; | |
90 } | |
1499 | 91 String name = (String)call.args[0]; |
1512 | 92 String password = (String)call.args[1]; |
1499 | 93 synchronized(backups) { |
94 backup = backups.get(name); | |
95 if( backup == null ) { | |
96 backup = new Backup(new File(backupDir,name)); | |
97 backups.put(name,backup); | |
98 } | |
99 } | |
1512 | 100 File pwd = new File(backupDir,name+".pwd"); |
101 try { | |
102 if( !pwd.exists() ) { | |
103 Writer out = new FileWriter(pwd); | |
104 out.write(password); | |
105 out.close(); | |
106 } else { | |
107 Reader in = new FileReader(pwd); | |
108 if( !IoUtils.readAll(in).equals(password) ) { | |
109 rpc.write( new RpcException("wrong password") ); | |
110 rpc.close(); | |
111 return; | |
112 } | |
113 } | |
114 } catch(IOException e) { | |
115 throw new RuntimeException(e); | |
116 } | |
117 rpc.write(Rpc.OK); | |
118 } | |
119 while( !rpc.isClosed() ) { | |
120 RpcCall call = rpc.read(); | |
121 if( call == null ) | |
122 break; | |
1499 | 123 backup.handle(rpc,call); |
124 } | |
125 } | |
126 | |
1509 | 127 |
128 // for client | |
129 | |
130 public static RpcClient rpcClient(String backupDomain) throws IOException { | |
131 Socket socket; | |
132 if( BackupServer.cipherSuites == null ) { | |
133 socket = new Socket(backupDomain,BackupServer.port); | |
134 } else { | |
135 socket = IoUtils.getSSLSocketFactory().createSocket(backupDomain,BackupServer.port); | |
136 ((SSLSocket)socket).setEnabledCipherSuites(BackupServer.cipherSuites); | |
137 } | |
138 return new RpcClient(socket); | |
139 } | |
140 | |
141 public static void getBackup(String backupDomain,String name,File zip) throws IOException, RpcException { | |
142 RpcClient rpc = BackupServer.rpcClient(backupDomain); | |
143 RpcCall call = new RpcCall("zip",name); | |
144 rpc.write(call); | |
145 RpcResult result = rpc.read(); | |
146 OutputStream out = new BufferedOutputStream(new FileOutputStream(zip)); | |
147 IoUtils.copyAll(result.in,out); | |
148 out.close(); | |
149 rpc.close(); | |
150 } | |
151 | |
1499 | 152 } |