aboutsummaryrefslogtreecommitdiff
path: root/juick-xmpp/src/main/java/com/juick/components/s2s/ConnectionOut.java
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2017-10-09 15:27:48 +0300
committerGravatar Vitaly Takmazov2017-10-10 09:37:31 +0300
commit448fc7e84732422011186a9a4633c345e9c6208e (patch)
tree7ec23a9f29939fecf6456956ead6cebd1338871d /juick-xmpp/src/main/java/com/juick/components/s2s/ConnectionOut.java
parent443daa747298de315840cdd6ee6992519707e61a (diff)
xmpp:ConnectionOut -> StreamServerDialback
Diffstat (limited to 'juick-xmpp/src/main/java/com/juick/components/s2s/ConnectionOut.java')
-rw-r--r--juick-xmpp/src/main/java/com/juick/components/s2s/ConnectionOut.java191
1 files changed, 0 insertions, 191 deletions
diff --git a/juick-xmpp/src/main/java/com/juick/components/s2s/ConnectionOut.java b/juick-xmpp/src/main/java/com/juick/components/s2s/ConnectionOut.java
deleted file mode 100644
index 589ed18a..00000000
--- a/juick-xmpp/src/main/java/com/juick/components/s2s/ConnectionOut.java
+++ /dev/null
@@ -1,191 +0,0 @@
-/*
- * Copyright (C) 2008-2017, Juick
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-package com.juick.components.s2s;
-
-import com.juick.components.XMPPServer;
-import com.juick.components.s2s.util.DialbackUtils;
-import com.juick.xmpp.extensions.StreamError;
-import com.juick.xmpp.extensions.StreamFeatures;
-import com.juick.xmpp.utils.XmlUtils;
-import org.apache.commons.text.RandomStringGenerator;
-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.InetSocketAddress;
-import java.net.Socket;
-import java.net.SocketException;
-import java.security.KeyManagementException;
-import java.security.KeyStoreException;
-import java.security.NoSuchAlgorithmException;
-import java.security.UnrecoverableKeyException;
-import java.security.cert.CertificateException;
-import java.util.Arrays;
-import java.util.UUID;
-
-/**
- * @author ugnich
- */
-public class ConnectionOut extends Connection implements Runnable {
-
- public boolean streamReady = false;
- public String to;
- String checkSID = null;
- String dbKey = null;
- RandomStringGenerator generator = new RandomStringGenerator.Builder().withinRange('a', 'z').build();
-
- public ConnectionOut(XMPPServer xmpp, String hostname) throws CertificateException, UnrecoverableKeyException, NoSuchAlgorithmException, XmlPullParserException, KeyManagementException, KeyStoreException, IOException {
- super(xmpp);
- to = hostname;
- dbKey = DialbackUtils.generateDialbackKey(generator.generate(15), to, xmpp.HOSTNAME, streamID);
- }
-
- public ConnectionOut(XMPPServer 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 + "'>" +
- dbKey + "</db:result>");
- }
-
- @Override
- public void run() {
- logger.info("stream to {} start", to);
- try {
- socket = new Socket();
- InetSocketAddress address = DNSQueries.getServerAddress(to);
- socket.connect(address);
- 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 {} {} open", to, streamID);
- 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 {} {} ready", to, streamID);
-
- String cache = xmpp.getFromCache(to);
- if (cache != null) {
- logger.info("stream to {} {} sending cache", to, streamID);
- sendStanza(cache);
- }
-
- } else {
- logger.info("stream to {} {} dialback fail", to, streamID);
- }
- 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) {
- xmpp.getConnectionIn(sid).ifPresent(c -> c.sendDialbackResult(from, type));
- }
- XmlUtils.skip(parser);
- } else if (tag.equals("features") && parser.getNamespace().equals(NS_STREAM)) {
- StreamFeatures features = StreamFeatures.parse(parser);
- if (sc != null && !isSecured() && features.STARTTLS >= 0 && !Arrays.asList(xmpp.brokenSSLhosts).contains(to)) {
- logger.info("stream to {} {} securing", to, streamID);
- 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);
- logger.info("stream {} secured", streamID);
- restartParser();
- sendOpenStream();
- } catch (SSLException sex) {
- logger.error("s2s ssl error: {} {}, error {}", 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 if (tag.equals("error")) {
- StreamError streamError = StreamError.parse(parser);
- logger.warn("Stream error from {}: {}", streamID, streamError.getCondition());
- xmpp.removeConnectionOut(this);
- closeConnection();
- } else {
- String unhandledStanza = XmlUtils.parseToString(parser, true);
- logger.warn("Unhandled stanza from {} {} : {}", to, streamID, unhandledStanza);
- }
- }
-
- logger.warn("stream to {} {} finished", to, streamID);
- xmpp.removeConnectionOut(ConnectionOut.this);
- closeConnection();
- } catch (EOFException | SocketException eofex) {
- logger.info("stream {} {} closed (dirty)", to, streamID);
- xmpp.removeConnectionOut(ConnectionOut.this);
- closeConnection();
- } catch (Exception e) {
- logger.error("s2s out exception: {} {}, exception {}", to, streamID, e);
- xmpp.removeConnectionOut(ConnectionOut.this);
- closeConnection();
- }
- }
-
- public void sendDialbackVerify(String sid, String key) {
- sendStanza("<db:verify from='" + xmpp.HOSTNAME + "' to='" + to + "' id='" + sid + "'>" +
- key + "</db:verify>");
- }
-}