导航菜单

页面标题

页面副标题

95爱播免登录版 v3.1.9 - XMPPConnection.java 源代码

正在查看: 95爱播免登录版 v3.1.9 应用的 XMPPConnection.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package org.jivesoftware.smack;

import com.xiaomi.mipush.sdk.Constants;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Objects;
import org.apache.commons.httpclient.cookie.CookieSpec;
import org.apache.harmony.javax.security.auth.callback.CallbackHandler;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.compression.XMPPInputOutputStream;
import org.jivesoftware.smack.debugger.SmackDebugger;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.dns.HostAddress;

public class XMPPConnection extends Connection {
    private boolean anonymous;
    private boolean authenticated;
    private Collection<String> compressionMethods;
    private boolean connected;
    String connectionID;
    PacketReader packetReader;
    PacketWriter packetWriter;
    Roster roster;
    private boolean serverAckdCompression;
    Socket socket;
    private volatile boolean socketClosed;
    private String user;
    private boolean usingTLS;
    private boolean wasAuthenticated;

    public XMPPConnection(String str, CallbackHandler callbackHandler) {
        super(new ConnectionConfiguration(str));
        this.connectionID = null;
        this.user = null;
        this.connected = false;
        this.socketClosed = false;
        this.authenticated = false;
        this.wasAuthenticated = false;
        this.anonymous = false;
        this.usingTLS = false;
        this.roster = null;
        this.serverAckdCompression = false;
        this.config.setCompressionEnabled(false);
        this.config.setSASLAuthenticationEnabled(true);
        this.config.setDebuggerEnabled(Connection.DEBUG_ENABLED);
        this.config.setCallbackHandler(callbackHandler);
    }

    private void connectUsingConfiguration(ConnectionConfiguration connectionConfiguration) throws XMPPException {
        Iterator<HostAddress> it = connectionConfiguration.getHostAddresses().iterator();
        LinkedList linkedList = new LinkedList();
        boolean z = false;
        do {
            if (it.hasNext()) {
                XMPPException xMPPException = null;
                HostAddress next = it.next();
                String fqdn = next.getFQDN();
                int port = next.getPort();
                try {
                    if (connectionConfiguration.getSocketFactory() == null) {
                        this.socket = new Socket(fqdn, port);
                    } else {
                        this.socket = connectionConfiguration.getSocketFactory().createSocket(fqdn, port);
                    }
                } catch (UnknownHostException e2) {
                    String str = "Could not connect to " + fqdn + Constants.COLON_SEPARATOR + port + ".";
                    xMPPException = new XMPPException(str, new XMPPError(XMPPError.Condition.remote_server_timeout, str), e2);
                } catch (IOException e3) {
                    String str2 = "XMPPError connecting to " + fqdn + Constants.COLON_SEPARATOR + port + ".";
                    XMPPException xMPPException2 = new XMPPException(str2, new XMPPError(XMPPError.Condition.remote_server_error, str2), e3);
                    z = true;
                    xMPPException = xMPPException2;
                }
                if (xMPPException == null) {
                    connectionConfiguration.setUsedHostAddress(next);
                } else {
                    next.setException(xMPPException);
                    linkedList.add(next);
                }
            }
            this.socketClosed = false;
            initConnection();
            return;
        } while (it.hasNext());
        StringBuilder sb = new StringBuilder();
        Iterator it2 = linkedList.iterator();
        while (it2.hasNext()) {
            sb.append(((HostAddress) it2.next()).getErrorMessage());
            sb.append("; ");
        }
        throw new XMPPException(sb.toString(), z ? new XMPPError(XMPPError.Condition.remote_server_error) : new XMPPError(XMPPError.Condition.remote_server_timeout));
    }

    private void initConnection() throws XMPPException {
        boolean z = this.packetReader == null || this.packetWriter == null;
        this.compressionHandler = null;
        this.serverAckdCompression = false;
        initReaderAndWriter();
        try {
            if (z) {
                this.packetWriter = new PacketWriter(this);
                this.packetReader = new PacketReader(this);
                if (this.config.isDebuggerEnabled()) {
                    addPacketListener(this.debugger.getReaderListener(), null);
                    if (this.debugger.getWriterListener() != null) {
                        addPacketSendingListener(this.debugger.getWriterListener(), null);
                    }
                }
            } else {
                this.packetWriter.init();
                this.packetReader.init();
            }
            this.packetWriter.startup();
            this.packetReader.startup();
            this.connected = true;
            if (z) {
                Iterator<ConnectionCreationListener> it = Connection.getConnectionCreationListeners().iterator();
                while (it.hasNext()) {
                    it.next().connectionCreated(this);
                }
            } else {
                if (this.wasAuthenticated) {
                    return;
                }
                notifyReconnection();
            }
        } catch (XMPPException e2) {
            PacketWriter packetWriter = this.packetWriter;
            if (packetWriter != null) {
                try {
                    packetWriter.shutdown();
                } catch (Throwable unused) {
                }
                this.packetWriter = null;
            }
            PacketReader packetReader = this.packetReader;
            if (packetReader != null) {
                try {
                    packetReader.shutdown();
                } catch (Throwable unused2) {
                }
                this.packetReader = null;
            }
            Reader reader = this.reader;
            if (reader != null) {
                try {
                    reader.close();
                } catch (Throwable unused3) {
                }
                this.reader = null;
            }
            Writer writer = this.writer;
            if (writer != null) {
                try {
                    writer.close();
                } catch (Throwable unused4) {
                }
                this.writer = null;
            }
            Socket socket = this.socket;
            if (socket != null) {
                try {
                    socket.close();
                } catch (Exception unused5) {
                }
                this.socket = null;
            }
            setWasAuthenticated(this.authenticated);
            this.chatManager = null;
            this.authenticated = false;
            this.connected = false;
            throw e2;
        }
    }

    private void initReaderAndWriter() throws XMPPException {
        try {
            XMPPInputOutputStream xMPPInputOutputStream = this.compressionHandler;
            if (xMPPInputOutputStream == null) {
                this.reader = new BufferedReader(new InputStreamReader(this.socket.getInputStream(), "UTF-8"));
                this.writer = new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream(), "UTF-8"));
            } else {
                try {
                    this.writer = new BufferedWriter(new OutputStreamWriter(xMPPInputOutputStream.getOutputStream(this.socket.getOutputStream()), "UTF-8"));
                    this.reader = new BufferedReader(new InputStreamReader(this.compressionHandler.getInputStream(this.socket.getInputStream()), "UTF-8"));
                } catch (Exception e2) {
                    e2.printStackTrace();
                    this.compressionHandler = null;
                    this.reader = new BufferedReader(new InputStreamReader(this.socket.getInputStream(), "UTF-8"));
                    this.writer = new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream(), "UTF-8"));
                }
            }
            initDebugger();
        } catch (IOException e3) {
            throw new XMPPException("XMPPError establishing connection with server.", new XMPPError(XMPPError.Condition.remote_server_error, "XMPPError establishing connection with server."), e3);
        }
    }

    private XMPPInputOutputStream maybeGetCompressionHandler() {
        if (this.compressionMethods == null) {
            return null;
        }
        for (XMPPInputOutputStream xMPPInputOutputStream : Connection.compressionHandlers) {
            if (xMPPInputOutputStream.isSupported()) {
                if (this.compressionMethods.contains(xMPPInputOutputStream.getCompressionMethod())) {
                    return xMPPInputOutputStream;
                }
            }
        }
        return null;
    }

    private void requestStreamCompression(String str) {
        try {
            this.writer.write("<compress xmlns='http://jabber.org/protocol/compress'>");
            this.writer.write("<method>" + str + "</method></compress>");
            this.writer.flush();
        } catch (IOException e2) {
            notifyConnectionError(e2);
        }
    }

    private void setWasAuthenticated(boolean z) {
        if (this.wasAuthenticated) {
            return;
        }
        this.wasAuthenticated = z;
    }

    private boolean useCompression() {
        if (this.authenticated) {
            throw new IllegalStateException("Compression should be negotiated before authentication.");
        }
        XMPPInputOutputStream maybeGetCompressionHandler = maybeGetCompressionHandler();
        this.compressionHandler = maybeGetCompressionHandler;
        if (maybeGetCompressionHandler == null) {
            return false;
        }
        requestStreamCompression(maybeGetCompressionHandler.getCompressionMethod());
        synchronized (this) {
            try {
                wait(SmackConfiguration.getPacketReplyTimeout() * 5);
            } catch (InterruptedException unused) {
            }
        }
        return isUsingCompression();
    }

    public void addPacketWriterInterceptor(PacketInterceptor packetInterceptor, PacketFilter packetFilter) {
        addPacketInterceptor(packetInterceptor, packetFilter);
    }

    public void addPacketWriterListener(PacketListener packetListener, PacketFilter packetFilter) {
        addPacketSendingListener(packetListener, packetFilter);
    }

    @Override
    public void connect() throws XMPPException {
        connectUsingConfiguration(this.config);
        if (this.connected && this.wasAuthenticated) {
            if (isAnonymous()) {
                loginAnonymously();
            } else {
                login(this.config.getUsername(), this.config.getPassword(), this.config.getResource());
            }
            notifyReconnection();
        }
    }

    public boolean containPacketListener(PacketListener packetListener) {
        return this.recvListeners.containsKey(packetListener);
    }

    @Override
    public synchronized void disconnect(Presence presence) {
        ReconnectionManager.done = true;
        PacketReader packetReader = this.packetReader;
        PacketWriter packetWriter = this.packetWriter;
        if (packetReader != null && packetWriter != null) {
            if (isConnected()) {
                shutdown(presence, 1);
                Roster roster = this.roster;
                if (roster != null) {
                    roster.cleanup();
                    this.roster = null;
                }
                this.chatManager = null;
                this.wasAuthenticated = false;
                packetWriter.cleanup();
                packetReader.cleanup();
            }
        }
    }

    @Override
    public String getConnectionID() {
        if (isConnected()) {
            return this.connectionID;
        }
        return null;
    }

    @Override
    public Roster getRoster() {
        synchronized (this) {
            if (isAuthenticated() && !isAnonymous()) {
                if (!this.config.isRosterLoadedAtLogin()) {
                    this.roster.reload();
                }
                Roster roster = this.roster;
                if (!roster.rosterInitialized) {
                    try {
                        synchronized (roster) {
                            long packetReplyTimeout = SmackConfiguration.getPacketReplyTimeout();
                            long currentTimeMillis = System.currentTimeMillis();
                            while (true) {
                                Roster roster2 = this.roster;
                                if (roster2.rosterInitialized || packetReplyTimeout <= 0) {
                                    break;
                                }
                                roster2.wait(packetReplyTimeout);
                                long currentTimeMillis2 = System.currentTimeMillis();
                                packetReplyTimeout -= currentTimeMillis2 - currentTimeMillis;
                                currentTimeMillis = currentTimeMillis2;
                            }
                        }
                    } catch (InterruptedException unused) {
                    }
                }
                return this.roster;
            }
            if (this.roster == null) {
                this.roster = new Roster(this);
            }
            return this.roster;
        }
    }

    @Override
    public String getUser() {
        if (isAuthenticated()) {
            return this.user;
        }
        return null;
    }

    @Override
    public boolean isAnonymous() {
        return this.anonymous;
    }

    @Override
    public boolean isAuthenticated() {
        return this.authenticated;
    }

    @Override
    public boolean isConnected() {
        return this.connected;
    }

    @Override
    public boolean isSecureConnection() {
        return isUsingTLS();
    }

    public boolean isSocketClosed() {
        return this.socketClosed;
    }

    public boolean isSocketConnected() {
        return this.socket.isConnected();
    }

    @Override
    public boolean isUsingCompression() {
        return this.compressionHandler != null && this.serverAckdCompression;
    }

    public boolean isUsingTLS() {
        return this.usingTLS;
    }

    @Override
    public synchronized void login(String str, String str2, String str3) throws XMPPException {
        SmackDebugger smackDebugger;
        if (!isConnected()) {
            throw new XMPPException("Not connected to server.");
        }
        if (this.authenticated) {
            throw new XMPPException("Already logged in to server.");
        }
        String trim = str.toLowerCase().trim();
        String authenticate = (this.config.isSASLAuthenticationEnabled() && this.saslAuthentication.hasNonAnonymousAuthentication()) ? str2 != null ? this.saslAuthentication.authenticate(trim, str2, str3) : this.saslAuthentication.authenticate(trim, str3, this.config.getCallbackHandler()) : new NonSASLAuthentication(this).authenticate(trim, str2, str3);
        if (authenticate != null) {
            this.user = authenticate;
            this.config.setServiceName(StringUtils.parseServer(authenticate));
        } else {
            String str4 = String.valueOf(trim) + "@" + getServiceName();
            this.user = str4;
            if (str3 != null) {
                this.user = String.valueOf(str4) + CookieSpec.PATH_DELIM + str3;
            }
        }
        if (this.config.isCompressionEnabled()) {
            useCompression();
        }
        this.authenticated = true;
        this.anonymous = false;
        if (this.roster == null) {
            if (this.rosterStorage == null) {
                this.roster = new Roster(this);
            } else {
                this.roster = new Roster(this, this.rosterStorage);
            }
        }
        if (this.config.isRosterLoadedAtLogin()) {
            this.roster.reload();
        }
        if (this.config.isSendPresence()) {
            this.packetWriter.sendPacket(new Presence(Presence.Type.available));
        }
        this.config.setLoginInfo(trim, str2, str3);
        if (this.config.isDebuggerEnabled() && (smackDebugger = this.debugger) != null) {
            smackDebugger.userHasLogged(this.user);
        }
    }

    @Override
    public synchronized void loginAnonymously() throws XMPPException {
        SmackDebugger smackDebugger;
        if (!isConnected()) {
            throw new XMPPException("Not connected to server.");
        }
        if (this.authenticated) {
            throw new XMPPException("Already logged in to server.");
        }
        String authenticateAnonymously = (this.config.isSASLAuthenticationEnabled() && this.saslAuthentication.hasAnonymousAuthentication()) ? this.saslAuthentication.authenticateAnonymously() : new NonSASLAuthentication(this).authenticateAnonymously();
        this.user = authenticateAnonymously;
        this.config.setServiceName(StringUtils.parseServer(authenticateAnonymously));
        if (this.config.isCompressionEnabled()) {
            useCompression();
        }
        this.packetWriter.sendPacket(new Presence(Presence.Type.available));
        this.authenticated = true;
        this.anonymous = true;
        if (this.config.isDebuggerEnabled() && (smackDebugger = this.debugger) != null) {
            smackDebugger.userHasLogged(this.user);
        }
    }

    synchronized void notifyConnectionError(Exception exc) {
        PacketReader packetReader = this.packetReader;
        if (packetReader != null && this.packetWriter != null) {
            if (packetReader.done && this.packetWriter.done) {
                return;
            }
            this.packetReader.done = true;
            this.packetWriter.done = true;
            shutdown(new Presence(Presence.Type.unavailable), 2);
            exc.printStackTrace();
            Iterator<ConnectionListener> it = getConnectionListeners().iterator();
            while (it.hasNext()) {
                try {
                    it.next().connectionClosedOnError(exc);
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
        }
    }

    protected void notifyReconnection() {
        Iterator<ConnectionListener> it = getConnectionListeners().iterator();
        while (it.hasNext()) {
            try {
                it.next().reconnectionSuccessful();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }

    void proceedTLSReceived() throws java.lang.Exception {
        throw new UnsupportedOperationException("Method not decompiled: org.jivesoftware.smack.XMPPConnection.proceedTLSReceived():void");
    }

    public void removePacketWriterInterceptor(PacketInterceptor packetInterceptor) {
        removePacketInterceptor(packetInterceptor);
    }

    public void removePacketWriterListener(PacketListener packetListener) {
        removePacketSendingListener(packetListener);
    }

    @Override
    public void sendPacket(Packet packet) {
        if (!isConnected()) {
            throw new IllegalStateException("Not connected to server.");
        }
        Objects.requireNonNull(packet, "Packet is null.");
        this.packetWriter.sendPacket(packet);
    }

    void setAvailableCompressionMethods(Collection<String> collection) {
        this.compressionMethods = collection;
    }

    @Override
    public void setRosterStorage(RosterStorage rosterStorage) throws IllegalStateException {
        if (this.roster != null) {
            throw new IllegalStateException("Roster is already initialized");
        }
        this.rosterStorage = rosterStorage;
    }

    protected void shutdown(Presence presence, int i2) {
        PacketWriter packetWriter = this.packetWriter;
        if (packetWriter != null) {
            packetWriter.sendPacket(presence);
        }
        setWasAuthenticated(this.authenticated);
        this.authenticated = false;
        PacketReader packetReader = this.packetReader;
        if (packetReader != null) {
            packetReader.shutdown();
        }
        PacketWriter packetWriter2 = this.packetWriter;
        if (packetWriter2 != null) {
            packetWriter2.shutdown();
        }
        try {
            Thread.sleep(150L);
        } catch (Exception unused) {
        }
        this.socketClosed = true;
        try {
            this.socket.close();
        } catch (Exception e2) {
            e2.printStackTrace();
        }
        this.connected = false;
        Reader reader = this.reader;
        if (reader != null) {
            try {
                reader.close();
            } catch (Throwable unused2) {
            }
            this.reader = null;
        }
        Writer writer = this.writer;
        if (writer != null) {
            try {
                writer.close();
            } catch (Throwable unused3) {
            }
            this.writer = null;
        }
        try {
            this.socket.close();
        } catch (Exception unused4) {
        }
        this.saslAuthentication.init();
    }

    void startStreamCompression() throws Exception {
        this.serverAckdCompression = true;
        initReaderAndWriter();
        this.packetWriter.setWriter(this.writer);
        this.packetWriter.openStream();
        synchronized (this) {
            notify();
        }
    }

    void startTLSReceived(boolean z) {
        if (z && this.config.getSecurityMode() == ConnectionConfiguration.SecurityMode.disabled) {
            notifyConnectionError(new IllegalStateException("TLS required by server but not allowed by connection configuration"));
            return;
        }
        if (this.config.getSecurityMode() == ConnectionConfiguration.SecurityMode.disabled) {
            return;
        }
        try {
            this.writer.write("<starttls xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\"/>");
            this.writer.flush();
        } catch (IOException e2) {
            notifyConnectionError(e2);
        }
    }

    void streamCompressionDenied() {
        synchronized (this) {
            notify();
        }
    }

    public XMPPConnection(String str) {
        super(new ConnectionConfiguration(str));
        this.connectionID = null;
        this.user = null;
        this.connected = false;
        this.socketClosed = false;
        this.authenticated = false;
        this.wasAuthenticated = false;
        this.anonymous = false;
        this.usingTLS = false;
        this.roster = null;
        this.serverAckdCompression = false;
        this.config.setCompressionEnabled(false);
        this.config.setSASLAuthenticationEnabled(true);
        this.config.setDebuggerEnabled(Connection.DEBUG_ENABLED);
    }

    public XMPPConnection(ConnectionConfiguration connectionConfiguration) {
        super(connectionConfiguration);
        this.connectionID = null;
        this.user = null;
        this.connected = false;
        this.socketClosed = false;
        this.authenticated = false;
        this.wasAuthenticated = false;
        this.anonymous = false;
        this.usingTLS = false;
        this.roster = null;
        this.serverAckdCompression = false;
    }

    public XMPPConnection(ConnectionConfiguration connectionConfiguration, CallbackHandler callbackHandler) {
        super(connectionConfiguration);
        this.connectionID = null;
        this.user = null;
        this.connected = false;
        this.socketClosed = false;
        this.authenticated = false;
        this.wasAuthenticated = false;
        this.anonymous = false;
        this.usingTLS = false;
        this.roster = null;
        this.serverAckdCompression = false;
        connectionConfiguration.setCallbackHandler(callbackHandler);
    }
}