-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetupConnection.java
More file actions
109 lines (95 loc) · 4.32 KB
/
Copy pathsetupConnection.java
File metadata and controls
109 lines (95 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package clientCommunications;
import clientFeatures.MyException;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import serverCommunications.CreateCertificates;
import javax.net.ssl.*;
import java.io.*;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URL;
import java.net.UnknownHostException;
import java.security.KeyStore;
import java.security.Security;
/**
* Created by ryan on 11/30/14.
* Created with specified port, host, timeout
* listens for feedback from server for command, and executes a new thread of the specified process/feature
* a NULL host string will result in connecting to localhost
*/
public class setupConnection implements Runnable{
String host, temp, certifcatePassword, password;
int port, timeout;
URL hostURL;
SSLSocket connection;
//Socket connection;
// DataInputStream br;
//DataOutputStream wr;
private PrintWriter wr;
private BufferedReader br, stdin, ioKeystore;
private OutputStream os;
private InputStream is ;
String pickedCipher[] ={"TLS_RSA_WITH_AES_256_CBC_SHA"};
KeyStore ts;
TrustManagerFactory tmf;
SSLContext sslContext;
File trustStore;
public setupConnection(String host, String password, int port, int timeout, CreateCertificates c) {
this.password=password;
this.certifcatePassword=c.getPassword();
this.host=host;
this.port=port;
this.timeout=timeout;
try {
hostURL = new URL("http://"+host);
// connection=new Socket(host, port);
ioKeystore= new BufferedReader(new FileReader(trustStore=c.getCertificate()));
ts=KeyStore.getInstance(KeyStore.getDefaultType());
ts.load(new FileInputStream(c.getKeyStore()), certifcatePassword.toCharArray());
tmf=TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(ts);
sslContext=SSLContext.getInstance("SSL");
sslContext.init(null, tmf.getTrustManagers(), null);
SSLSocketFactory sslFact= (SSLSocketFactory) sslContext.getSocketFactory();
connection=(SSLSocket) sslFact.createSocket(host, port);
Security.addProvider(
new com.sun.net.ssl.internal.ssl.Provider());
} catch(MalformedURLException e) {
new Thread(new MyException(e, "MalformedURLException: Server host url is malformed, or doesn't exist")).start();
} catch (UnknownHostException e) {
new Thread(new MyException(e, "UnknownHostException: Client connection socket can't connect")).start();
} catch (IOException e) {
new Thread(new MyException(e, "IOException: client socket has io error")).start();
} catch (Exception e) {
new Thread(new MyException(e, "Exception: Error in ssl and keystore initialization")).start();
}
}
public void run() {
byte[] buf;
try {
if(connection==null) {
SSLSocketFactory sslFact= (SSLSocketFactory) SSLSocketFactory.getDefault();
connection=(SSLSocket) sslFact.createSocket(host, port);
}
connection.setEnabledCipherSuites(pickedCipher);
is = connection.getInputStream();
os=connection.getOutputStream();
wr=new PrintWriter(os, true);
br=new BufferedReader(new InputStreamReader(is));
// stdin=new BufferedReader(new InputStreamReader(System.in));
// connection.setKeepAlive(true);
System.out.println("setup data streams");
wr.println(password);
temp=br.readLine();
System.out.println(temp+" Server message");
// System.out.println((temp=stdin.readLine())+"Server message stdin");
// System.out.println((temp=stdin.readLine())+"Server message stdin");
wr.close();
br.close();
// if(stdin.ready())
// stdin.close();
// }
} catch (IOException e) {
new Thread(new MyException(e, "Exception in creating io streams to server")).start();
}
}
}