Mercurial Hosting > luan
annotate 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 |
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; | |
1690
973d3039c421
backup server checks client domain
Franklin Schmidt <fschmidt@gmail.com>
parents:
1675
diff
changeset
|
6 import java.net.InetAddress; |
1499 | 7 import java.net.Socket; |
8 import java.net.ServerSocket; | |
1690
973d3039c421
backup server checks client domain
Franklin Schmidt <fschmidt@gmail.com>
parents:
1675
diff
changeset
|
9 import java.net.UnknownHostException; |
1499 | 10 import java.util.concurrent.Executors; |
1502 | 11 import java.util.concurrent.ExecutorService; |
1509 | 12 import javax.net.ssl.SSLSocket; |
1499 | 13 import javax.net.ssl.SSLServerSocket; |
14 import goodjava.util.SoftCacheMap; | |
1501 | 15 import goodjava.io.IoUtils; |
1499 | 16 import goodjava.rpc.RpcServer; |
17 import goodjava.rpc.RpcCall; | |
1509 | 18 import goodjava.rpc.RpcClient; |
19 import goodjava.rpc.RpcResult; | |
1512 | 20 import goodjava.rpc.Rpc; |
1509 | 21 import goodjava.rpc.RpcException; |
1499 | 22 import goodjava.logging.Logger; |
23 import goodjava.logging.LoggerFactory; | |
24 | |
25 | |
1672 | 26 public final class BackupServer { |
1499 | 27 private static final Logger logger = LoggerFactory.getLogger(BackupServer.class); |
28 | |
1675 | 29 public static int port = 9102; |
1499 | 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 | |
1690
973d3039c421
backup server checks client domain
Franklin Schmidt <fschmidt@gmail.com>
parents:
1675
diff
changeset
|
76 private static String getName(RpcServer rpc,Object[] args) { |
973d3039c421
backup server checks client domain
Franklin Schmidt <fschmidt@gmail.com>
parents:
1675
diff
changeset
|
77 String domain = (String)args[0]; |
973d3039c421
backup server checks client domain
Franklin Schmidt <fschmidt@gmail.com>
parents:
1675
diff
changeset
|
78 InetAddress addr; |
973d3039c421
backup server checks client domain
Franklin Schmidt <fschmidt@gmail.com>
parents:
1675
diff
changeset
|
79 try { |
973d3039c421
backup server checks client domain
Franklin Schmidt <fschmidt@gmail.com>
parents:
1675
diff
changeset
|
80 addr = InetAddress.getByName(domain); |
973d3039c421
backup server checks client domain
Franklin Schmidt <fschmidt@gmail.com>
parents:
1675
diff
changeset
|
81 } catch(UnknownHostException e) { |
973d3039c421
backup server checks client domain
Franklin Schmidt <fschmidt@gmail.com>
parents:
1675
diff
changeset
|
82 rpc.write( new RpcException("domain lookup failed") ); |
973d3039c421
backup server checks client domain
Franklin Schmidt <fschmidt@gmail.com>
parents:
1675
diff
changeset
|
83 rpc.close(); |
973d3039c421
backup server checks client domain
Franklin Schmidt <fschmidt@gmail.com>
parents:
1675
diff
changeset
|
84 return null; |
973d3039c421
backup server checks client domain
Franklin Schmidt <fschmidt@gmail.com>
parents:
1675
diff
changeset
|
85 } |
973d3039c421
backup server checks client domain
Franklin Schmidt <fschmidt@gmail.com>
parents:
1675
diff
changeset
|
86 if( !rpc.socket.getInetAddress().equals(addr) ) { |
973d3039c421
backup server checks client domain
Franklin Schmidt <fschmidt@gmail.com>
parents:
1675
diff
changeset
|
87 rpc.write( new RpcException("domain doesn't match client") ); |
973d3039c421
backup server checks client domain
Franklin Schmidt <fschmidt@gmail.com>
parents:
1675
diff
changeset
|
88 rpc.close(); |
973d3039c421
backup server checks client domain
Franklin Schmidt <fschmidt@gmail.com>
parents:
1675
diff
changeset
|
89 return null; |
973d3039c421
backup server checks client domain
Franklin Schmidt <fschmidt@gmail.com>
parents:
1675
diff
changeset
|
90 } |
973d3039c421
backup server checks client domain
Franklin Schmidt <fschmidt@gmail.com>
parents:
1675
diff
changeset
|
91 String name = (String)args[1]; |
973d3039c421
backup server checks client domain
Franklin Schmidt <fschmidt@gmail.com>
parents:
1675
diff
changeset
|
92 return name==null ? domain : domain + "~" + name; |
973d3039c421
backup server checks client domain
Franklin Schmidt <fschmidt@gmail.com>
parents:
1675
diff
changeset
|
93 } |
973d3039c421
backup server checks client domain
Franklin Schmidt <fschmidt@gmail.com>
parents:
1675
diff
changeset
|
94 |
1499 | 95 private void handle(Socket socket) { |
96 RpcServer rpc = new RpcServer(socket); | |
1674 | 97 Backup backup = null; |
98 while( !rpc.isClosed() ) { | |
1499 | 99 RpcCall call = rpc.read(); |
1674 | 100 if( call == null ) |
101 break; | |
102 if( call.cmd.equals("exists") ) { | |
1690
973d3039c421
backup server checks client domain
Franklin Schmidt <fschmidt@gmail.com>
parents:
1675
diff
changeset
|
103 String name = getName(rpc,call.args); |
973d3039c421
backup server checks client domain
Franklin Schmidt <fschmidt@gmail.com>
parents:
1675
diff
changeset
|
104 if( name==null ) return; |
1674 | 105 rpc.write( new RpcResult(new Object[]{new File(backupDir,name).exists()}) ); |
106 } else if( call.cmd.equals("login") ) { | |
1690
973d3039c421
backup server checks client domain
Franklin Schmidt <fschmidt@gmail.com>
parents:
1675
diff
changeset
|
107 String name = getName(rpc,call.args); |
973d3039c421
backup server checks client domain
Franklin Schmidt <fschmidt@gmail.com>
parents:
1675
diff
changeset
|
108 if( name==null ) return; |
1674 | 109 synchronized(backups) { |
110 backup = backups.get(name); | |
111 if( backup == null ) { | |
112 backup = new Backup(new File(backupDir,name)); | |
113 backups.put(name,backup); | |
114 } | |
115 } | |
116 rpc.write(Rpc.OK); | |
117 } else if( backup != null ) { | |
118 backup.handle(rpc,call); | |
119 } else { | |
1512 | 120 rpc.write( new RpcException("login expected") ); |
121 rpc.close(); | |
122 return; | |
123 } | |
1499 | 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 | |
1499 | 141 } |