use of com.github.jh3nd3rs0n.jargyle.transport.socks5.Socks5Reply in project jargyle by jh3nd3rs0n.
the class Socks5ReplyFirewallRule method appliesTo.
@Override
public boolean appliesTo(final Rule.Context context) {
if (!(context instanceof Context)) {
return false;
}
Context cntxt = (Context) context;
if (cntxt.getFirewallRuleAction() != null) {
return false;
}
MethodSubnegotiationResults methSubnegotiationResults = cntxt.getMethodSubnegotiationResults();
Socks5Request socks5Req = cntxt.getSocks5Request();
Socks5Reply socks5Rep = cntxt.getSocks5Reply();
if (this.clientAddressRange != null && !this.clientAddressRange.contains(cntxt.getClientAddress())) {
return false;
}
if (this.socksServerAddressRange != null && !this.socksServerAddressRange.contains(cntxt.getSocksServerAddress())) {
return false;
}
if (this.method != null && !this.method.equals(methSubnegotiationResults.getMethod())) {
return false;
}
if (this.user != null && !this.user.equals(methSubnegotiationResults.getUser())) {
return false;
}
if (this.command != null && !this.command.equals(socks5Req.getCommand())) {
return false;
}
if (this.desiredDestinationAddressRange != null && !this.desiredDestinationAddressRange.contains(socks5Req.getDesiredDestinationAddress())) {
return false;
}
if (this.desiredDestinationPortRange != null && !this.desiredDestinationPortRange.contains(Port.newInstance(socks5Req.getDesiredDestinationPort()))) {
return false;
}
if (this.serverBoundAddressRange != null && !this.serverBoundAddressRange.contains(socks5Rep.getServerBoundAddress())) {
return false;
}
if (this.serverBoundPortRange != null && !this.serverBoundPortRange.contains(Port.newInstance(socks5Rep.getServerBoundPort()))) {
return false;
}
return true;
}
use of com.github.jh3nd3rs0n.jargyle.transport.socks5.Socks5Reply in project jargyle by jh3nd3rs0n.
the class Socks5Client method receiveSocks5Reply.
protected Socks5Reply receiveSocks5Reply(final Socket connectedInternalSocket) throws IOException {
Socks5Reply socks5Rep = null;
try {
InputStream inputStream = connectedInternalSocket.getInputStream();
socks5Rep = Socks5Reply.newInstanceFrom(inputStream);
} catch (IOException e) {
SocksClientExceptionThrowingHelper.throwAsSocksClientException(e, this);
}
Reply reply = socks5Rep.getReply();
if (!reply.equals(Reply.SUCCEEDED)) {
throw new FailureSocks5ReplyException(this, socks5Rep);
}
return socks5Rep;
}
use of com.github.jh3nd3rs0n.jargyle.transport.socks5.Socks5Reply in project jargyle by jh3nd3rs0n.
the class Socks5Worker method newSocks5Request.
private Socks5Request newSocks5Request() {
Socks5Request socks5Request = null;
try {
socks5Request = Socks5Request.newInstanceFrom(this.clientFacingInputStream);
} catch (AddressTypeNotSupportedException e) {
LOGGER.debug(ObjectLogMessageHelper.objectLogMessage(this, "Unable to parse the SOCKS5 request"), e);
Socks5Reply socks5Rep = Socks5Reply.newFailureInstance(Reply.ADDRESS_TYPE_NOT_SUPPORTED);
this.socks5WorkerContext.sendSocks5Reply(this, socks5Rep, LOGGER);
return null;
} catch (CommandNotSupportedException e) {
LOGGER.debug(ObjectLogMessageHelper.objectLogMessage(this, "Unable to parse the SOCKS5 request"), e);
Socks5Reply socks5Rep = Socks5Reply.newFailureInstance(Reply.COMMAND_NOT_SUPPORTED);
this.socks5WorkerContext.sendSocks5Reply(this, socks5Rep, LOGGER);
return null;
} catch (IOException e) {
ClientFacingIOExceptionLoggingHelper.log(LOGGER, ObjectLogMessageHelper.objectLogMessage(this, "Error in parsing the SOCKS5 request"), e);
return null;
}
LOGGER.debug(ObjectLogMessageHelper.objectLogMessage(this, "Received %s", socks5Request.toString()));
return socks5Request;
}
use of com.github.jh3nd3rs0n.jargyle.transport.socks5.Socks5Reply in project jargyle by jh3nd3rs0n.
the class UdpAssociateCommandWorker method wrapClientFacingDatagramSocket.
private DatagramSocket wrapClientFacingDatagramSocket(final DatagramSocket clientFacingDatagramSock, final String clientHost, final int clientPort) {
DatagramSocket clientFacingDatagramSck = clientFacingDatagramSock;
if (!AllZerosAddressConstants.isAllZerosAddress(clientHost) && clientPort > 0) {
InetAddress udpClientHostInetAddress = null;
try {
udpClientHostInetAddress = InetAddress.getByName(clientHost);
} catch (UnknownHostException e) {
LOGGER.error(ObjectLogMessageHelper.objectLogMessage(this, "Error in resolving the client host %s", clientHost), e);
Socks5Reply socks5Rep = Socks5Reply.newFailureInstance(Reply.HOST_UNREACHABLE);
this.commandWorkerContext.sendSocks5Reply(this, socks5Rep, LOGGER);
return null;
}
clientFacingDatagramSck.connect(udpClientHostInetAddress, clientPort);
}
if (clientFacingDatagramSck.isConnected() && this.clientFacingDtlsDatagramSocketFactory != null) {
try {
clientFacingDatagramSck = this.clientFacingDtlsDatagramSocketFactory.newDatagramSocket(clientFacingDatagramSck, clientHost, clientPort);
} catch (IOException e) {
LOGGER.error(ObjectLogMessageHelper.objectLogMessage(this, "Error in wrapping the client-facing UDP socket"), e);
Socks5Reply socks5Rep = Socks5Reply.newFailureInstance(Reply.GENERAL_SOCKS_SERVER_FAILURE);
this.commandWorkerContext.sendSocks5Reply(this, socks5Rep, LOGGER);
return null;
}
}
try {
clientFacingDatagramSck = this.methodSubnegotiationResults.getDatagramSocket(clientFacingDatagramSck);
} catch (IOException e) {
LOGGER.error(ObjectLogMessageHelper.objectLogMessage(this, "Error in wrapping the client-facing UDP socket"), e);
Socks5Reply socks5Rep = Socks5Reply.newFailureInstance(Reply.GENERAL_SOCKS_SERVER_FAILURE);
this.commandWorkerContext.sendSocks5Reply(this, socks5Rep, LOGGER);
return null;
}
return clientFacingDatagramSck;
}
use of com.github.jh3nd3rs0n.jargyle.transport.socks5.Socks5Reply in project jargyle by jh3nd3rs0n.
the class UdpAssociateCommandWorker method run.
@Override
public void run() throws IOException {
Socks5Reply socks5Rep = null;
String desiredDestinationAddr = this.desiredDestinationAddress;
if (AllZerosAddressConstants.isAllZerosAddress(desiredDestinationAddr)) {
desiredDestinationAddr = this.clientFacingSocket.getInetAddress().getHostAddress();
}
int desiredDestinationPrt = this.desiredDestinationPort;
DatagramSocket peerFacingDatagramSock = null;
DatagramSocket clientFacingDatagramSock = null;
try {
peerFacingDatagramSock = this.newPeerFacingDatagramSocket();
if (peerFacingDatagramSock == null) {
return;
}
if (!this.configurePeerFacingDatagramSocket(peerFacingDatagramSock)) {
return;
}
clientFacingDatagramSock = this.newClientFacingDatagramSocket();
if (clientFacingDatagramSock == null) {
return;
}
if (!this.configureClientFacingDatagramSocket(clientFacingDatagramSock)) {
return;
}
DatagramSocket clientFacingDatagramSck = this.wrapClientFacingDatagramSocket(clientFacingDatagramSock, desiredDestinationAddr, desiredDestinationPrt);
if (clientFacingDatagramSck == null) {
return;
}
clientFacingDatagramSock = clientFacingDatagramSck;
InetAddress inetAddress = clientFacingDatagramSock.getLocalAddress();
String serverBoundAddress = inetAddress.getHostAddress();
if (AllZerosAddressConstants.isAllZerosAddress(serverBoundAddress)) {
inetAddress = this.clientFacingSocket.getLocalAddress();
serverBoundAddress = inetAddress.getHostAddress();
}
int serverBoundPort = clientFacingDatagramSock.getLocalPort();
socks5Rep = Socks5Reply.newInstance(Reply.SUCCEEDED, serverBoundAddress, serverBoundPort);
FirewallRule.Context context = new Socks5ReplyFirewallRule.Context(this.clientFacingSocket.getInetAddress().getHostAddress(), this.clientFacingSocket.getLocalAddress().getHostAddress(), this.methodSubnegotiationResults, this.socks5Request, socks5Rep);
if (!this.commandWorkerContext.canAllowSocks5Reply(this, context, LOGGER)) {
return;
}
if (!this.commandWorkerContext.sendSocks5Reply(this, socks5Rep, LOGGER)) {
return;
}
UdpRelayServer.Builder builder = new UdpRelayServer.Builder(desiredDestinationAddr, desiredDestinationPrt, clientFacingDatagramSock, peerFacingDatagramSock);
builder.bufferSize(this.settings.getLastValue(Socks5SettingSpecConstants.SOCKS5_ON_UDP_ASSOCIATE_RELAY_BUFFER_SIZE).intValue());
builder.hostResolver(this.netObjectFactory.newHostResolver());
builder.idleTimeout(this.settings.getLastValue(Socks5SettingSpecConstants.SOCKS5_ON_UDP_ASSOCIATE_RELAY_IDLE_TIMEOUT).intValue());
builder.inboundSocks5UdpFirewallRules(this.settings.getLastValue(Socks5SettingSpecConstants.SOCKS5_ON_UDP_ASSOCIATE_INBOUND_SOCKS5_UDP_FIREWALL_RULES));
builder.methodSubnegotiationResults(this.methodSubnegotiationResults);
builder.outboundSocks5UdpFirewallRules(this.settings.getLastValue(Socks5SettingSpecConstants.SOCKS5_ON_UDP_ASSOCIATE_OUTBOUND_SOCKS5_UDP_FIREWALL_RULES));
try {
this.passPackets(builder);
} catch (IOException e) {
LOGGER.error(ObjectLogMessageHelper.objectLogMessage(this, "Error in starting the UDP association"), e);
}
} finally {
if (clientFacingDatagramSock != null && !clientFacingDatagramSock.isClosed()) {
clientFacingDatagramSock.close();
}
if (peerFacingDatagramSock != null && !peerFacingDatagramSock.isClosed()) {
peerFacingDatagramSock.close();
}
}
}
Aggregations