|
1 import javax.net.ssl.*; 2 import java.security.KeyStore; 3 import javax.security.cert.X509Certificate; 4 import java.io.*; 5 import java.net.SocketAddress; 6 import java.net.InetSocketAddress; 7 8 /** 9 * Rudimentary command-line epp-client. 10 */ 11 12 public class EppClient { 13 14 private static final String SERVER = "epp.registry.tryout.be"; 15 private static final int PORT = 33123; 16 17 private static final int TIME_OUT = 3000; 18 19 private static final String KEYSTORE_FILE = "epp-tryout.ks"; 20 private static final String KEYSTORE_PWD = "test123"; 21 22 private static final String logout = 23 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 24 "<epp xmlns=\"urn:ietf:params:xml:ns:epp-1.0\">\n" + 25 " <command> <logout/> </command>\n" + 26 "</epp>\n"; 27 28 private BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); 29 private SSLSocket socket; 30 private DataInputStream dis; 31 private DataOutputStream dos; 32 33 public static void main(String[] args) throws Exception { 34 EppClient client = new EppClient(); 35 client.run(); 36 } 37 38 private String readOption (String message, String defaultvalue) throws Exception { 39 System.out.print(message + " [" + defaultvalue + "] : "); 40 String value = stdin.readLine(); 41 if (value == null || value.length() == 0) { 42 return defaultvalue; 43 } 44 return value; 45 } 46 47 private int readOption (String message, int defaultvalue) throws Exception { 48 System.out.print(message + " [" + defaultvalue + "] : "); 49 String value = stdin.readLine(); 50 if (value == null || value.length() == 0) { 51 return defaultvalue; 52 } 53 return Integer.parseInt(value); 54 } 55 56 private void run() throws Exception { 57 String server = readOption("Enter name of the server",SERVER); 58 int port = readOption("Enter remote port", PORT); 59 SSLSocketFactory factory = null; 60 SSLContext ctx = SSLContext.getInstance("TLS"); 61 // add KeyManagers if server checks client certificates 62 ctx.init(null, getTrustManagers(), null); 63 factory = ctx.getSocketFactory(); 64 socket = (SSLSocket)factory.createSocket(); 65 int timeout = readOption ("Enter socket time-out", TIME_OUT); 66 socket.setSoTimeout(timeout); 67 SocketAddress addr = new InetSocketAddress(server,port); 68 socket.connect(addr,timeout); 69 System.out.println("Connected to " + socket.getRemoteSocketAddress()); 70 dis = new DataInputStream(socket.getInputStream()); 71 dos = new DataOutputStream(socket.getOutputStream()); 72 X509Certificate chain[] = socket.getSession().getPeerCertificateChain(); 73 for (int i = 0; i < chain.length; i++) { 74 System.out.println("peer-certificate " + i); 75 System.out.println(" Subject : " + chain[i].getSubjectDN().getName()); 76 System.out.println(" Issuer : " + chain[i].getIssuerDN().getName()); 77 } 78 pause("press <Enter> to read the initial greeting"); 79 readEppString(); 80 sendCommands(); 81 System.out.println("closing the connection"); 82 socket.close(); 83 } 84 85 TrustManager[] getTrustManagers() throws Exception { 86 String filename = readOption("Enter path of the keystore file", KEYSTORE_FILE); 87 String password = readOption("Enter path of the keystore file", KEYSTORE_PWD); 88 KeyStore keyStore = KeyStore.getInstance("JKS"); 89 keyStore.load ( 90 new FileInputStream(filename), 91 password.toCharArray() 92 ); 93 TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); 94 tmf.init (keyStore); 95 return tmf.getTrustManagers(); 96 } 97 98 private void sendCommands() throws Exception { 99 while (true) { 100 System.out.println("Please, enter one of the following:"); 101 System.out.println(" STOP : to quit immediately"); 102 System.out.println(" LOGOUT : to log out and quit"); 103 System.out.println(" the name of an UTF-encoded file to send to the server"); 104 String input = stdin.readLine(); 105 if ("STOP".equals(input)) { 106 break; 107 } else 108 if ("LOGOUT".equals(input)) { 109 writeEppString(logout); 110 pause("press <Enter> to see the response"); 111 readEppString(); 112 break; 113 } 114 else { 115 try { 116 String eppCommand = readFile(input); 117 writeEppString(eppCommand); 118 pause("press <Enter> to see the response"); 119 readEppString(); 120 } catch (FileNotFoundException e) { 121 System.out.println(e); 122 System.out.println("============================"); 123 } 124 } 125 } 126 } 127 128 private String readFile (String filename) throws Exception { 129 StringBuffer buffer = new StringBuffer(); 130 BufferedReader file = new BufferedReader(new FileReader(filename)); 131 try { 132 String line; 133 while ((line = file.readLine()) != null) { 134 buffer.append(line); 135 buffer.append("\n"); 136 } 137 return buffer.toString(); 138 } finally { 139 file.close(); 140 } 141 } 142 143 private String readEppString () throws IOException { 144 int len = dis.readInt(); 145 if (len > 4000) { 146 throw new IOException ("Indicated length is unlikely long: " + len); 147 } 148 System.out.println("length of input: " + len + " bytes"); 149 len = len - 4; 150 byte bytes[] = new byte[len]; 151 dis.readFully(bytes,0,len); 152 String input = new String(bytes,"UTF-8"); 153 System.out.println("========================================="); 154 System.out.print (input); 155 System.out.println("========================================="); 156 return input; 157 } 158 159 private void writeEppString (String output) throws IOException { 160 byte[] bytes = output.getBytes("UTF-8"); 161 int len = bytes.length + 4; 162 System.out.println("sending " + len + " bytes"); 163 System.out.println("==================================="); 164 System.out.print (output); 165 System.out.println("==================================="); 166 dos.writeInt(len); 167 dos.write(bytes); 168 } 169 170 private void pause(String msg) throws Exception { 171 System.out.print(msg); 172 System.in.read(); 173 } 174 175 } 176