导航菜单

页面标题

页面副标题

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

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

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


package org.jivesoftware.smack;

import com.xiaomi.mipush.sdk.MiPushClient;
import java.util.Iterator;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.PrivacyItem;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smack.sasl.SASLMechanism;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

class PacketReader {
    private XMPPConnection connection;
    private String connectionID = null;
    volatile boolean done;
    private ExecutorService listenerExecutor;
    private XmlPullParser parser;
    private Thread readerThread;

    private class ListenerNotification implements Runnable {
        private Packet packet;

        public ListenerNotification(Packet packet) {
            this.packet = packet;
        }

        @Override
        public void run() {
            Iterator<Connection.ListenerWrapper> it = PacketReader.this.connection.recvListeners.values().iterator();
            while (it.hasNext()) {
                it.next().notifyListener(this.packet);
            }
        }
    }

    protected PacketReader(XMPPConnection xMPPConnection) {
        this.connection = xMPPConnection;
        init();
    }

    private void parseFeatures(XmlPullParser xmlPullParser) throws Exception {
        boolean z = false;
        boolean z2 = false;
        boolean z3 = false;
        while (!z) {
            int next = xmlPullParser.next();
            if (next == 2) {
                if (xmlPullParser.getName().equals("starttls")) {
                    z2 = true;
                } else if (xmlPullParser.getName().equals("mechanisms")) {
                    this.connection.getSASLAuthentication().setAvailableSASLMethods(PacketParserUtils.parseMechanisms(xmlPullParser));
                } else if (xmlPullParser.getName().equals("bind")) {
                    this.connection.getSASLAuthentication().bindingRequired();
                } else if (xmlPullParser.getName().equals("ver")) {
                    this.connection.getConfiguration().setRosterVersioningAvailable(true);
                } else if (xmlPullParser.getName().equals("c")) {
                    String attributeValue = xmlPullParser.getAttributeValue(null, "node");
                    String attributeValue2 = xmlPullParser.getAttributeValue(null, "ver");
                    if (attributeValue2 != null && attributeValue != null) {
                        this.connection.setServiceCapsNode(String.valueOf(attributeValue) + "#" + attributeValue2);
                    }
                } else if (xmlPullParser.getName().equals("session")) {
                    this.connection.getSASLAuthentication().sessionsSupported();
                } else if (xmlPullParser.getName().equals("compression")) {
                    this.connection.setAvailableCompressionMethods(PacketParserUtils.parseCompressionMethods(xmlPullParser));
                } else if (xmlPullParser.getName().equals(MiPushClient.COMMAND_REGISTER)) {
                    this.connection.getAccountManager().setSupportsAccountCreation(true);
                }
            } else if (next == 3) {
                if (xmlPullParser.getName().equals("starttls")) {
                    this.connection.startTLSReceived(z3);
                } else if (xmlPullParser.getName().equals("required") && z2) {
                    z3 = true;
                } else if (xmlPullParser.getName().equals("features")) {
                    z = true;
                }
            }
        }
        if (!this.connection.isSecureConnection() && !z2 && this.connection.getConfiguration().getSecurityMode() == ConnectionConfiguration.SecurityMode.required) {
            throw new XMPPException("Server does not support security (TLS), but security required by connection configuration.", new XMPPError(XMPPError.Condition.forbidden));
        }
        if (!z2 || this.connection.getConfiguration().getSecurityMode() == ConnectionConfiguration.SecurityMode.disabled) {
            releaseConnectionIDLock();
        }
    }

    public void parsePackets(Thread thread) {
        try {
            int eventType = this.parser.getEventType();
            do {
                if (eventType == 2) {
                    if (this.parser.getName().equals("message")) {
                        processPacket(PacketParserUtils.parseMessage(this.parser));
                    } else if (this.parser.getName().equals("iq")) {
                        processPacket(PacketParserUtils.parseIQ(this.parser, this.connection));
                    } else if (this.parser.getName().equals("presence")) {
                        processPacket(PacketParserUtils.parsePresence(this.parser));
                    } else if (!this.parser.getName().equals("stream")) {
                        if (this.parser.getName().equals("error")) {
                            throw new XMPPException(PacketParserUtils.parseStreamError(this.parser));
                        }
                        if (this.parser.getName().equals("features")) {
                            parseFeatures(this.parser);
                        } else if (this.parser.getName().equals("proceed")) {
                            this.connection.proceedTLSReceived();
                            resetParser();
                        } else if (this.parser.getName().equals("failure")) {
                            String namespace = this.parser.getNamespace(null);
                            if ("urn:ietf:params:xml:ns:xmpp-tls".equals(namespace)) {
                                throw new Exception("TLS negotiation has failed");
                            }
                            if ("http://jabber.org/protocol/compress".equals(namespace)) {
                                this.connection.streamCompressionDenied();
                            } else {
                                processPacket(PacketParserUtils.parseSASLFailure(this.parser));
                                this.connection.getSASLAuthentication().authenticationFailed();
                            }
                        } else if (this.parser.getName().equals("challenge")) {
                            String nextText = this.parser.nextText();
                            processPacket(new SASLMechanism.Challenge(nextText));
                            this.connection.getSASLAuthentication().challengeReceived(nextText);
                        } else if (this.parser.getName().equals("success")) {
                            processPacket(new SASLMechanism.Success(this.parser.nextText()));
                            this.connection.packetWriter.openStream();
                            resetParser();
                            this.connection.getSASLAuthentication().authenticated();
                        } else if (this.parser.getName().equals("compressed")) {
                            this.connection.startStreamCompression();
                            resetParser();
                        }
                    } else if ("jabber:client".equals(this.parser.getNamespace(null))) {
                        for (int i2 = 0; i2 < this.parser.getAttributeCount(); i2++) {
                            if (this.parser.getAttributeName(i2).equals("id")) {
                                this.connectionID = this.parser.getAttributeValue(i2);
                                if (!"1.0".equals(this.parser.getAttributeValue("", "version"))) {
                                    releaseConnectionIDLock();
                                }
                            } else if (this.parser.getAttributeName(i2).equals(PrivacyItem.PrivacyRule.SUBSCRIPTION_FROM)) {
                                this.connection.config.setServiceName(this.parser.getAttributeValue(i2));
                            }
                        }
                    }
                } else if (eventType == 3 && this.parser.getName().equals("stream")) {
                    this.connection.disconnect();
                }
                eventType = this.parser.next();
                if (this.done || eventType == 1) {
                    return;
                }
            } while (thread == this.readerThread);
        } catch (Exception e2) {
            if (this.done || this.connection.isSocketClosed()) {
                return;
            }
            this.connection.notifyConnectionError(e2);
        }
    }

    private void processPacket(Packet packet) {
        if (packet == null) {
            return;
        }
        Iterator<PacketCollector> it = this.connection.getPacketCollectors().iterator();
        while (it.hasNext()) {
            it.next().processPacket(packet);
        }
        this.listenerExecutor.submit(new ListenerNotification(packet));
    }

    private synchronized void releaseConnectionIDLock() {
        notify();
    }

    private void resetParser() {
        try {
            XmlPullParser newPullParser = XmlPullParserFactory.newInstance().newPullParser();
            this.parser = newPullParser;
            newPullParser.setFeature("http://xmlpull.org/v1/doc/features.html#process-namespaces", true);
            this.parser.setInput(this.connection.reader);
        } catch (XmlPullParserException e2) {
            e2.printStackTrace();
        }
    }

    void cleanup() {
        this.connection.recvListeners.clear();
        this.connection.collectors.clear();
    }

    protected void init() {
        this.done = false;
        this.connectionID = null;
        Thread thread = new Thread() {
            @Override
            public void run() {
                PacketReader.this.parsePackets(this);
            }
        };
        this.readerThread = thread;
        thread.setName("Smack Packet Reader (" + this.connection.connectionCounterValue + ")");
        this.readerThread.setDaemon(true);
        this.listenerExecutor = Executors.newSingleThreadExecutor(new ThreadFactory() {
            @Override
            public Thread newThread(Runnable runnable) {
                Thread thread2 = new Thread(runnable, "Smack Listener Processor (" + PacketReader.this.connection.connectionCounterValue + ")");
                thread2.setDaemon(true);
                return thread2;
            }
        });
        resetParser();
    }

    public void shutdown() {
        if (!this.done) {
            Iterator<ConnectionListener> it = this.connection.getConnectionListeners().iterator();
            while (it.hasNext()) {
                try {
                    it.next().connectionClosed();
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
        }
        this.done = true;
        this.listenerExecutor.shutdown();
    }

    public synchronized void startup() throws XMPPException {
        try {
            try {
                this.readerThread.start();
                try {
                    wait(SmackConfiguration.getPacketReplyTimeout() * 3);
                } catch (InterruptedException unused) {
                }
                String str = this.connectionID;
                if (str == null) {
                    throw new XMPPException("Connection failed. No response from server.");
                }
                this.connection.connectionID = str;
            } catch (Throwable th) {
                throw th;
            }
        } catch (Exception e2) {
            throw new XMPPException(e2.toString());
        }
    }
}