正在查看: imToken v3.28.8 应用的 SocksRequest.java JAVA 源代码文件
本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。
正在查看: imToken v3.28.8 应用的 SocksRequest.java JAVA 源代码文件
本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。
package com.subgraph.orchid.socks;
import com.subgraph.orchid.TorConfig;
import com.subgraph.orchid.data.IPv4Address;
import java.io.IOException;
import java.net.Socket;
import java.util.logging.Logger;
public abstract class SocksRequest {
private static final Logger logger = Logger.getLogger(SocksRequest.class.getName());
private IPv4Address address;
private byte[] addressData;
private final TorConfig config;
private String hostname;
private long lastWarningTimestamp = 0;
private int port;
private final Socket socket;
public abstract int getCommandCode();
public abstract boolean isConnectRequest();
public abstract void readRequest() throws SocksRequestException;
abstract void sendConnectionRefused() throws SocksRequestException;
abstract void sendError(boolean z) throws SocksRequestException;
abstract void sendSuccess() throws SocksRequestException;
protected SocksRequest(TorConfig torConfig, Socket socket) {
this.config = torConfig;
this.socket = socket;
}
public int getPort() {
return this.port;
}
public IPv4Address getAddress() {
return this.address;
}
public boolean hasHostname() {
return this.hostname != null;
}
public String getHostname() {
return this.hostname;
}
public String getTarget() {
if (this.config.getSafeLogging()) {
return "[scrubbed]:" + this.port;
}
if (this.hostname != null) {
return this.hostname + ":" + this.port;
}
return this.address + ":" + this.port;
}
protected void setPortData(byte[] bArr) throws SocksRequestException {
if (bArr.length != 2) {
throw new SocksRequestException();
}
this.port = (bArr[1] & 255) | ((bArr[0] & 255) << 8);
}
protected void setIPv4AddressData(byte[] bArr) throws SocksRequestException {
logUnsafeSOCKS();
if (bArr.length != 4) {
throw new SocksRequestException();
}
this.addressData = bArr;
int i = 0;
for (byte b : bArr) {
i = (i << 8) | (b & 255);
}
this.address = new IPv4Address(i);
}
private boolean testRateLimit() {
long currentTimeMillis = System.currentTimeMillis();
long j = currentTimeMillis - this.lastWarningTimestamp;
this.lastWarningTimestamp = currentTimeMillis;
return j > 5000;
}
private void logUnsafeSOCKS() throws SocksRequestException {
if ((this.config.getWarnUnsafeSocks() || this.config.getSafeSocks()) && testRateLimit()) {
logger.warning("Your application is giving Orchid only an IP address. Applications that do DNS resolves themselves may leak information. Consider using Socks4a (e.g. via privoxy or socat) instead. For more information please see https://wiki.torproject.org/TheOnionRouter/TorFAQ#SOCKSAndDNS");
}
if (this.config.getSafeSocks()) {
throw new SocksRequestException("Rejecting unsafe SOCKS request");
}
}
protected void setHostname(String str) {
this.hostname = str;
}
protected byte[] readPortData() throws SocksRequestException {
byte[] bArr = new byte[2];
readAll(bArr, 0, 2);
return bArr;
}
protected byte[] readIPv4AddressData() throws SocksRequestException {
byte[] bArr = new byte[4];
readAll(bArr);
return bArr;
}
protected byte[] readIPv6AddressData() throws SocksRequestException {
byte[] bArr = new byte[16];
readAll(bArr);
return bArr;
}
protected String readNullTerminatedString() throws SocksRequestException {
try {
StringBuilder sb = new StringBuilder();
while (true) {
int read = this.socket.getInputStream().read();
if (read == -1) {
throw new SocksRequestException();
}
if (read == 0) {
return sb.toString();
}
sb.append((char) read);
}
} catch (IOException e) {
throw new SocksRequestException(e);
}
}
protected int readByte() throws SocksRequestException {
try {
int read = this.socket.getInputStream().read();
if (read != -1) {
return read;
}
throw new SocksRequestException();
} catch (IOException e) {
throw new SocksRequestException(e);
}
}
protected void readAll(byte[] bArr) throws SocksRequestException {
readAll(bArr, 0, bArr.length);
}
protected void readAll(byte[] bArr, int i, int i2) throws SocksRequestException {
while (i2 > 0) {
try {
int read = this.socket.getInputStream().read(bArr, i, i2);
if (read == -1) {
throw new SocksRequestException();
}
i += read;
i2 -= read;
} catch (IOException e) {
throw new SocksRequestException(e);
}
}
}
protected void socketWrite(byte[] bArr) throws SocksRequestException {
try {
this.socket.getOutputStream().write(bArr);
} catch (IOException e) {
throw new SocksRequestException(e);
}
}
}