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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
package com.juick.xmpp.s2s;
import com.juick.xmpp.extensions.StreamFeatures;
import com.juick.xmpp.utils.XmlUtils;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLSocket;
import java.io.EOFException;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.util.UUID;
import java.util.logging.Level;
/**
* @author ugnich
*/
public class ConnectionOut extends Connection implements Runnable {
public boolean streamReady = false;
public String to;
XMPPComponent xmpp;
String checkSID = null;
String dbKey = null;
public ConnectionOut(XMPPComponent xmpp, String hostname) throws CertificateException, UnrecoverableKeyException, NoSuchAlgorithmException, XmlPullParserException, KeyManagementException, KeyStoreException, IOException {
super(xmpp);
this.xmpp = xmpp;
to = hostname;
}
public ConnectionOut(XMPPComponent xmpp, String hostname, String checkSID, String dbKey) throws Exception {
super(xmpp);
to = hostname;
this.checkSID = checkSID;
this.dbKey = dbKey;
streamID = UUID.randomUUID().toString();
}
void sendOpenStream() throws IOException {
sendStanza("<?xml version='1.0'?><stream:stream xmlns='jabber:server' id='" + streamID +
"' xmlns:stream='http://etherx.jabber.org/streams' xmlns:db='jabber:server:dialback' from='" +
xmpp.HOSTNAME + "' to='" + to + "' version='1.0'>");
}
void processDialback() throws Exception {
if (checkSID != null) {
sendDialbackVerify(checkSID, dbKey);
}
sendStanza("<db:result from='" + xmpp.HOSTNAME + "' to='" + to + "'>" +
generateDialbackKey(to, xmpp.HOSTNAME, streamID) + "</db:result>");
}
@Override
public void run() {
LOGGER.info("STREAM TO " + to + " START");
try {
HostnamePort addr = DNSQueries.getServerAddress(to);
try {
socket = new Socket(InetAddress.getByName(addr.hostname), addr.port);
} catch (UnknownHostException e) {
socket = new Socket(InetAddress.getByName("talk.google.com"), 5269);
}
restartParser();
sendOpenStream();
parser.next(); // stream:stream
streamID = parser.getAttributeValue(null, "id");
if (streamID == null || streamID.isEmpty()) {
throw new Exception("STREAM TO " + to + " INVALID FIRST PACKET");
}
LOGGER.info("STREAM TO " + to + " " + streamID + " OPEN");
xmpp.addConnectionOut(ConnectionOut.this);
boolean xmppversionnew = parser.getAttributeValue(null, "version") != null;
if (!xmppversionnew) {
processDialback();
}
while (parser.next() != XmlPullParser.END_DOCUMENT) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
logParser();
String tag = parser.getName();
if (tag.equals("result") && parser.getNamespace().equals(NS_DB)) {
String type = parser.getAttributeValue(null, "type");
if (type != null && type.equals("valid")) {
streamReady = true;
LOGGER.info("STREAM TO " + to + " " + streamID + " READY");
String cache = xmpp.getFromCache(to);
if (cache != null) {
LOGGER.info("STREAM TO " + to + " " + streamID + " SENDING CACHE");
sendStanza(cache);
}
} else {
LOGGER.info("STREAM TO " + to + " " + streamID + " DIALBACK FAIL");
}
XmlUtils.skip(parser);
} else if (tag.equals("verify") && parser.getNamespace().equals(NS_DB)) {
String from = parser.getAttributeValue(null, "from");
String type = parser.getAttributeValue(null, "type");
String sid = parser.getAttributeValue(null, "id");
if (from != null && from.equals(to) && sid != null && !sid.isEmpty() && type != null) {
ConnectionIn c = xmpp.getConnectionIn(sid);
if (c != null) {
c.sendDialbackResult(from, type);
}
}
XmlUtils.skip(parser);
} else if (tag.equals("features") && parser.getNamespace().equals(NS_STREAM)) {
StreamFeatures features = StreamFeatures.parse(parser);
if (!isSecured() && features.STARTTLS >= 0 && !xmpp.brokenSSLhosts.contains(to)) {
System.out.println("STREAM TO " + to + " " + streamID + " SECURING");
sendStanza("<starttls xmlns=\"" + NS_TLS + "\" />");
} else {
processDialback();
}
} else if (tag.equals("proceed") && parser.getNamespace().equals(NS_TLS)) {
try {
socket = sc.getSocketFactory().createSocket(socket, socket.getInetAddress().getHostAddress(),
socket.getPort(), true);
((SSLSocket) socket).startHandshake();
setSecured(true);
System.out.println("STREAM " + streamID + " SECURED");
restartParser();
sendOpenStream();
} catch (SSLException sex) {
LOGGER.log(Level.SEVERE, String.format("s2s ssl error: %s %s", to, streamID), sex);
sendStanza("<failed xmlns\"" + NS_TLS + "\" />");
xmpp.removeConnectionOut(this);
closeConnection();
}
} else if (isSecured() && tag.equals("stream") && parser.getNamespace().equals(NS_STREAM)) {
streamID = parser.getAttributeValue(null, "id");
} else {
LOGGER.info("STREAM TO " + to + " " + streamID + ": " + XmlUtils.parseToString(parser, true));
}
}
LOGGER.warning("STREAM TO " + to + " " + streamID + " FINISHED");
xmpp.removeConnectionOut(ConnectionOut.this);
closeConnection();
} catch (EOFException eofex) {
LOGGER.info(String.format("STREAM %s %s CLOSED (dirty)", to, streamID));
xmpp.removeConnectionOut(ConnectionOut.this);
closeConnection();
} catch (Exception e) {
LOGGER.log(Level.SEVERE, String.format("s2s out exception: %s %s", to, streamID), e);
xmpp.removeConnectionOut(ConnectionOut.this);
closeConnection();
}
}
public void sendDialbackVerify(String sid, String key) {
try {
sendStanza("<db:verify from='" + xmpp.HOSTNAME + "' to='" + to + "' id='" + sid + "'>" + key + "</db:verify>");
} catch (IOException e) {
LOGGER.log(Level.WARNING, "STREAM TO " + to + " " + streamID + " ERROR", e);
}
}
}
|