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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
package com.juick.components.s2s;
import com.juick.components.XMPPServer;
import com.juick.xmpp.extensions.StreamError;
import com.juick.xmpp.utils.XmlUtils;
import org.apache.commons.lang3.StringUtils;
import org.xmlpull.v1.XmlPullParser;
import rocks.xmpp.addr.Jid;
import rocks.xmpp.core.session.XmppSessionConfiguration;
import rocks.xmpp.core.stanza.model.Stanza;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLSocket;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.EOFException;
import java.io.IOException;
import java.io.StringReader;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* @author ugnich
*/
public class ConnectionIn extends Connection implements Runnable {
final public List<String> from = new ArrayList<>();
public long tsRemoteData = 0;
public long packetsRemote = 0;
XmppSessionConfiguration configuration;
public ConnectionIn(XMPPServer xmpp, Socket socket) throws Exception {
super(xmpp);
this.socket = socket;
restartParser();
}
protected Stanza parse(String xml) {
try {
Unmarshaller unmarshaller = xmpp.getSession().createUnmarshaller();
return (Stanza)unmarshaller.unmarshal(new StringReader(xml));
} catch (JAXBException e) {
logger.error("JAXB exception", e);
}
return null;
}
@Override
public void run() {
try {
parser.next(); // stream:stream
updateTsRemoteData();
if (!parser.getName().equals("stream")
|| !parser.getNamespace("stream").equals(NS_STREAM)) {
// || !parser.getAttributeValue(null, "version").equals("1.0")
// || !parser.getAttributeValue(null, "to").equals(Main.HOSTNAME)) {
throw new Exception(String.format("stream from %s invalid", socket.getRemoteSocketAddress()));
}
streamID = parser.getAttributeValue(null, "id");
if (streamID == null) {
streamID = UUID.randomUUID().toString();
}
boolean xmppversionnew = parser.getAttributeValue(null, "version") != null;
String from = parser.getAttributeValue(null, "from");
if (xmpp.bannedHosts.contains(from)) {
closeConnection();
return;
}
sendOpenStream(from, xmppversionnew);
while (parser.next() != XmlPullParser.END_DOCUMENT) {
updateTsRemoteData();
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
logParser();
packetsRemote++;
String tag = parser.getName();
if (tag.equals("result") && parser.getNamespace().equals(NS_DB)) {
String dfrom = parser.getAttributeValue(null, "from");
String to = parser.getAttributeValue(null, "to");
logger.info("stream from {} to {} {} asking for dialback", dfrom, to, streamID);
if (dfrom.endsWith(xmpp.HOSTNAME) && (dfrom.equals(xmpp.HOSTNAME) || dfrom.endsWith("." + xmpp.HOSTNAME))) {
logger.warn("stream from {} is invalid", dfrom);
break;
}
if (to != null && to.equals(xmpp.HOSTNAME)) {
String dbKey = XmlUtils.getTagText(parser);
updateTsRemoteData();
xmpp.startDialback(dfrom, streamID, dbKey);
} else {
logger.warn("stream from " + dfrom + " " + streamID + " invalid to " + to);
break;
}
} else if (tag.equals("verify") && parser.getNamespace().equals(NS_DB)) {
String vfrom = parser.getAttributeValue(null, "from");
String vto = parser.getAttributeValue(null, "to");
String vid = parser.getAttributeValue(null, "id");
String vkey = XmlUtils.getTagText(parser);
updateTsRemoteData();
boolean valid = false;
if (vfrom != null && vto != null && vid != null && vkey != null) {
ConnectionOut c = xmpp.getConnectionOut(vfrom, false);
if (c == null) {
logger.warn("outgoing connection to {} not found", vfrom);
} else {
String dialbackKey = c.dbKey;
valid = vkey.equals(dialbackKey);
}
}
if (valid) {
sendStanza("<db:verify from='" + vto + "' to='" + vfrom + "' id='" + vid + "' type='valid'/>");
logger.info("stream from {} {} dialback verify valid", vfrom, streamID);
} else {
sendStanza("<db:verify from='" + vto + "' to='" + vfrom + "' id='" + vid + "' type='invalid'/>");
logger.warn("stream from {} {} dialback verify invalid", vfrom, streamID);
}
} else if (tag.equals("presence") && checkFromTo(parser)) {
String xml = XmlUtils.parseToString(parser, false);
logger.info("stream {} presence: {}", streamID, xml);
xmpp.onStanzaReceived(parse(xml));
} else if (tag.equals("message") && checkFromTo(parser)) {
updateTsRemoteData();
String xml = XmlUtils.parseToString(parser, false);
logger.info("stream {} message: {}", streamID, xml);
xmpp.onStanzaReceived(parse(xml));
} else if (tag.equals("iq") && checkFromTo(parser)) {
updateTsRemoteData();
String type = parser.getAttributeValue(null, "type");
String xml = XmlUtils.parseToString(parser, false);
if (type == null || !type.equals("error")) {
logger.info("stream {} iq: {}", streamID, xml);
xmpp.getRouter().sendStanza(parse(xml));
}
} else if (sc != null && !isSecured() && tag.equals("starttls")) {
logger.info("stream {} securing", streamID);
sendStanza("<proceed xmlns=\"" + NS_TLS + "\" />");
try {
socket = sc.getSocketFactory().createSocket(socket, socket.getInetAddress().getHostAddress(),
socket.getPort(), true);
((SSLSocket) socket).setUseClientMode(false);
((SSLSocket) socket).startHandshake();
setSecured(true);
logger.info("stream {} secured", streamID);
restartParser();
} catch (SSLException sex) {
logger.warn("stream {} ssl error {}", streamID, sex);
sendStanza("<failed xmlns\"" + NS_TLS + "\" />");
xmpp.removeConnectionIn(this);
closeConnection();
}
} else if (isSecured() && tag.equals("stream") && parser.getNamespace().equals(NS_STREAM)) {
sendOpenStream(null, true);
} else if (tag.equals("error")) {
StreamError streamError = StreamError.parse(parser);
logger.warn("Stream error from {}: {}", streamID, streamError.getCondition());
xmpp.removeConnectionIn(this);
closeConnection();
} else {
String unhandledStanza = XmlUtils.parseToString(parser, true);
logger.warn("Unhandled stanza from {}: {}", streamID, unhandledStanza);
}
}
logger.warn("stream {} finished", streamID);
xmpp.removeConnectionIn(this);
closeConnection();
} catch (EOFException | SocketException ex) {
logger.info("stream {} closed (dirty)", streamID);
xmpp.removeConnectionIn(this);
closeConnection();
} catch (Exception e) {
logger.warn("stream {} error {}", streamID, e);
xmpp.removeConnectionIn(this);
closeConnection();
}
}
void updateTsRemoteData() {
tsRemoteData = System.currentTimeMillis();
}
void sendOpenStream(String from, boolean xmppversionnew) throws IOException {
String openStream = "<?xml version='1.0'?><stream:stream xmlns='jabber:server' " +
"xmlns:stream='http://etherx.jabber.org/streams' xmlns:db='jabber:server:dialback' from='" +
xmpp.HOSTNAME + "' id='" + streamID + "' version='1.0'>";
if (xmppversionnew) {
openStream += "<stream:features>";
if (sc != null && !isSecured() && !xmpp.brokenSSLhosts.contains(from)) {
openStream += "<starttls xmlns=\"" + NS_TLS + "\"><optional/></starttls>";
}
openStream += "</stream:features>";
}
sendStanza(openStream);
}
public void sendDialbackResult(String sfrom, String type) {
sendStanza("<db:result from='" + xmpp.HOSTNAME + "' to='" + sfrom + "' type='" + type + "'/>");
if (type.equals("valid")) {
from.add(sfrom);
logger.info("stream from {} {} ready", sfrom, streamID);
}
}
boolean checkFromTo(XmlPullParser parser) throws Exception {
String cfrom = parser.getAttributeValue(null, "from");
String cto = parser.getAttributeValue(null, "to");
if (StringUtils.isNotEmpty(cfrom) && StringUtils.isNotEmpty(cto)) {
Jid jidto = Jid.of(cto);
if (jidto.getDomain().equals(xmpp.HOSTNAME)) {
Jid jidfrom = Jid.of(cfrom);
int size = from.size();
for (int i = 0; i < size; i++) {
if (from.get(i).equals(jidfrom.getDomain())) {
return true;
}
}
}
}
return false;
}
}
|