正在查看: bbinstant v6.24.0 应用的 Socks5BytestreamManager.java JAVA 源代码文件
本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。
正在查看: bbinstant v6.24.0 应用的 Socks5BytestreamManager.java JAVA 源代码文件
本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。
package org.jivesoftware.smackx.bytestreams.socks5;
import java.io.IOException;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeoutException;
import org.jivesoftware.smack.ConnectionCreationListener;
import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPConnectionRegistry;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smackx.bytestreams.BytestreamListener;
import org.jivesoftware.smackx.bytestreams.BytestreamManager;
import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.disco.packet.DiscoverItems;
import org.jxmpp.jid.i;
public final class Socks5BytestreamManager extends Manager implements BytestreamManager {
private static final String SESSION_ID_PREFIX = "js5_";
private static final Map<XMPPConnection, Socks5BytestreamManager> managers;
private static final Random randomGenerator;
private final List<BytestreamListener> allRequestListeners;
private List<String> ignoredBytestreamRequests;
private final InitiationListener initiationListener;
private i lastWorkingProxy;
private final Set<i> proxyBlacklist;
private int proxyConnectionTimeout;
private boolean proxyPrioritizationEnabled;
private int targetResponseTimeout;
private final Map<i, BytestreamListener> userListeners;
static {
XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
@Override
public void connectionCreated(XMPPConnection xMPPConnection) {
Socks5BytestreamManager.getBytestreamManager(xMPPConnection);
}
});
randomGenerator = new Random();
managers = new WeakHashMap();
}
private Socks5BytestreamManager(XMPPConnection xMPPConnection) {
super(xMPPConnection);
this.userListeners = new ConcurrentHashMap();
this.allRequestListeners = Collections.synchronizedList(new LinkedList());
this.targetResponseTimeout = 10000;
this.proxyConnectionTimeout = 10000;
this.proxyBlacklist = Collections.synchronizedSet(new HashSet());
this.proxyPrioritizationEnabled = true;
this.ignoredBytestreamRequests = Collections.synchronizedList(new LinkedList());
this.initiationListener = new InitiationListener(this);
activate();
}
private void activate() {
connection().registerIQRequestHandler(this.initiationListener);
enableService();
}
private static Bytestream createBytestreamInitiation(String str, i iVar, List<Bytestream.StreamHost> list) {
Bytestream bytestream = new Bytestream(str);
Iterator<Bytestream.StreamHost> it = list.iterator();
while (it.hasNext()) {
bytestream.addStreamHost(it.next());
}
bytestream.setType(IQ.Type.set);
bytestream.setTo(iVar);
return bytestream;
}
private static Bytestream createStreamHostRequest(i iVar) {
Bytestream bytestream = new Bytestream();
bytestream.setType(IQ.Type.get);
bytestream.setTo(iVar);
return bytestream;
}
private List<i> determineProxies() throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
XMPPConnection connection = connection();
ServiceDiscoveryManager instanceFor = ServiceDiscoveryManager.getInstanceFor(connection);
ArrayList arrayList = new ArrayList();
for (DiscoverItems.Item item : instanceFor.discoverItems(connection.getXMPPServiceDomain()).getItems()) {
if (!this.proxyBlacklist.contains(item.getEntityID())) {
try {
if (instanceFor.discoverInfo(item.getEntityID()).hasIdentity("proxy", "bytestreams")) {
arrayList.add(item.getEntityID());
} else {
this.proxyBlacklist.add(item.getEntityID());
}
} catch (SmackException.NoResponseException | XMPPException.XMPPErrorException unused) {
this.proxyBlacklist.add(item.getEntityID());
}
}
}
return arrayList;
}
private List<Bytestream.StreamHost> determineStreamHostInfos(List<i> list) {
XMPPConnection connection = connection();
ArrayList arrayList = new ArrayList();
List<Bytestream.StreamHost> localStreamHost = getLocalStreamHost();
if (localStreamHost != null) {
arrayList.addAll(localStreamHost);
}
for (i iVar : list) {
try {
arrayList.addAll(((Bytestream) connection.createStanzaCollectorAndSend(createStreamHostRequest(iVar)).nextResultOrThrow()).getStreamHosts());
} catch (Exception unused) {
this.proxyBlacklist.add(iVar);
}
}
return arrayList;
}
private void enableService() {
ServiceDiscoveryManager.getInstanceFor(connection()).addFeature(Bytestream.NAMESPACE);
}
public static synchronized Socks5BytestreamManager getBytestreamManager(XMPPConnection xMPPConnection) {
synchronized (Socks5BytestreamManager.class) {
if (xMPPConnection == null) {
return null;
}
Map<XMPPConnection, Socks5BytestreamManager> map = managers;
Socks5BytestreamManager socks5BytestreamManager = map.get(xMPPConnection);
if (socks5BytestreamManager == null) {
socks5BytestreamManager = new Socks5BytestreamManager(xMPPConnection);
map.put(xMPPConnection, socks5BytestreamManager);
}
return socks5BytestreamManager;
}
}
private List<Bytestream.StreamHost> getLocalStreamHost() {
XMPPConnection connection = connection();
Socks5Proxy socks5Proxy = Socks5Proxy.getSocks5Proxy();
if (!socks5Proxy.isRunning()) {
return null;
}
List<String> localAddresses = socks5Proxy.getLocalAddresses();
if (localAddresses.isEmpty()) {
return null;
}
int port = socks5Proxy.getPort();
ArrayList arrayList = new ArrayList();
for (String str : localAddresses) {
String[] strArr = {"127.0.0.1", "0:0:0:0:0:0:0:1", "::1"};
int i = 0;
while (true) {
if (i >= 3) {
arrayList.add(new Bytestream.StreamHost(connection.getUser(), str, port));
break;
}
if (str.startsWith(strArr[i])) {
break;
}
i++;
}
}
return arrayList;
}
private static String getNextSessionID() {
return SESSION_ID_PREFIX + Math.abs(randomGenerator.nextLong());
}
private boolean supportsSocks5(i iVar) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException {
return ServiceDiscoveryManager.getInstanceFor(connection()).supportsFeature(iVar, Bytestream.NAMESPACE);
}
@Override
public void addIncomingBytestreamListener(BytestreamListener bytestreamListener) {
this.allRequestListeners.add(bytestreamListener);
}
public synchronized void disableService() {
try {
XMPPConnection connection = connection();
connection.unregisterIQRequestHandler(this.initiationListener);
this.initiationListener.shutdown();
this.allRequestListeners.clear();
this.userListeners.clear();
this.lastWorkingProxy = null;
this.proxyBlacklist.clear();
this.ignoredBytestreamRequests.clear();
Map<XMPPConnection, Socks5BytestreamManager> map = managers;
map.remove(connection);
if (map.size() == 0) {
Socks5Proxy.getSocks5Proxy().stop();
}
ServiceDiscoveryManager instanceFor = ServiceDiscoveryManager.getInstanceFor(connection);
if (instanceFor != null) {
instanceFor.removeFeature(Bytestream.NAMESPACE);
}
} catch (Throwable th) {
throw th;
}
}
protected List<BytestreamListener> getAllRequestListeners() {
return this.allRequestListeners;
}
protected XMPPConnection getConnection() {
return connection();
}
protected List<String> getIgnoredBytestreamRequests() {
return this.ignoredBytestreamRequests;
}
public int getProxyConnectionTimeout() {
if (this.proxyConnectionTimeout <= 0) {
this.proxyConnectionTimeout = 10000;
}
return this.proxyConnectionTimeout;
}
public int getTargetResponseTimeout() {
if (this.targetResponseTimeout <= 0) {
this.targetResponseTimeout = 10000;
}
return this.targetResponseTimeout;
}
protected BytestreamListener getUserListener(i iVar) {
return this.userListeners.get(iVar);
}
public void ignoreBytestreamRequestOnce(String str) {
this.ignoredBytestreamRequests.add(str);
}
public boolean isProxyPrioritizationEnabled() {
return this.proxyPrioritizationEnabled;
}
@Override
public void removeIncomingBytestreamListener(BytestreamListener bytestreamListener) {
this.allRequestListeners.remove(bytestreamListener);
}
protected void replyRejectPacket(IQ iq) throws SmackException.NotConnectedException, InterruptedException {
connection().sendStanza(IQ.createErrorResponse(iq, XMPPError.getBuilder(XMPPError.Condition.not_acceptable)));
}
public void setProxyConnectionTimeout(int i) {
this.proxyConnectionTimeout = i;
}
public void setProxyPrioritizationEnabled(boolean z) {
this.proxyPrioritizationEnabled = z;
}
public void setTargetResponseTimeout(int i) {
this.targetResponseTimeout = i;
}
@Override
public void addIncomingBytestreamListener(BytestreamListener bytestreamListener, i iVar) {
this.userListeners.put(iVar, bytestreamListener);
}
@Override
public void removeIncomingBytestreamListener(String str) {
this.userListeners.remove(str);
}
@Override
public Socks5BytestreamSession establishSession(i iVar) throws XMPPException, IOException, InterruptedException, SmackException {
return establishSession(iVar, getNextSessionID());
}
@Override
public Socks5BytestreamSession establishSession(i iVar, String str) throws IOException, InterruptedException, SmackException.NoResponseException, SmackException, XMPPException {
XMPPConnection connection = connection();
if (supportsSocks5(iVar)) {
ArrayList arrayList = new ArrayList();
Bytestream.StreamHost streamHost = null;
try {
arrayList.addAll(determineProxies());
e = null;
} catch (XMPPException.XMPPErrorException e) {
e = e;
}
List<Bytestream.StreamHost> determineStreamHostInfos = determineStreamHostInfos(arrayList);
if (determineStreamHostInfos.isEmpty()) {
if (e != null) {
throw e;
}
throw new SmackException("no SOCKS5 proxies available");
}
String createDigest = Socks5Utils.createDigest(str, connection.getUser(), iVar);
if (this.proxyPrioritizationEnabled && this.lastWorkingProxy != null) {
Iterator<Bytestream.StreamHost> it = determineStreamHostInfos.iterator();
while (true) {
if (!it.hasNext()) {
break;
}
Bytestream.StreamHost next = it.next();
if (next.getJID().equals((CharSequence) this.lastWorkingProxy)) {
streamHost = next;
break;
}
}
if (streamHost != null) {
determineStreamHostInfos.remove(streamHost);
determineStreamHostInfos.add(0, streamHost);
}
}
Socks5Proxy socks5Proxy = Socks5Proxy.getSocks5Proxy();
try {
try {
socks5Proxy.addTransfer(createDigest);
Bytestream createBytestreamInitiation = createBytestreamInitiation(str, iVar, determineStreamHostInfos);
Bytestream.StreamHost streamHost2 = createBytestreamInitiation.getStreamHost(((Bytestream) connection.createStanzaCollectorAndSend(createBytestreamInitiation).nextResultOrThrow(getTargetResponseTimeout())).getUsedHost().getJID());
if (streamHost2 != null) {
Socket socket = new Socks5ClientForInitiator(streamHost2, createDigest, connection, str, iVar).getSocket(getProxyConnectionTimeout());
this.lastWorkingProxy = streamHost2.getJID();
return new Socks5BytestreamSession(socket, streamHost2.getJID().equals((CharSequence) connection.getUser()));
}
throw new SmackException("Remote user responded with unknown host");
} catch (TimeoutException unused) {
throw new IOException("Timeout while connecting to SOCKS5 proxy");
}
} finally {
socks5Proxy.removeTransfer(createDigest);
}
}
throw new SmackException.FeatureNotSupportedException("SOCKS5 Bytestream", iVar);
}
}