Search in sources :

Example 1 with Socks5Reply

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;
}
Also used : Socks5Request(com.github.jh3nd3rs0n.jargyle.transport.socks5.Socks5Request) MethodSubnegotiationResults(com.github.jh3nd3rs0n.jargyle.server.socks5.MethodSubnegotiationResults) Socks5Reply(com.github.jh3nd3rs0n.jargyle.transport.socks5.Socks5Reply)

Example 2 with Socks5Reply

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;
}
Also used : InputStream(java.io.InputStream) Socks5Reply(com.github.jh3nd3rs0n.jargyle.transport.socks5.Socks5Reply) Reply(com.github.jh3nd3rs0n.jargyle.transport.socks5.Reply) Socks5Reply(com.github.jh3nd3rs0n.jargyle.transport.socks5.Socks5Reply) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException)

Example 3 with Socks5Reply

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;
}
Also used : Socks5Request(com.github.jh3nd3rs0n.jargyle.transport.socks5.Socks5Request) Socks5Reply(com.github.jh3nd3rs0n.jargyle.transport.socks5.Socks5Reply) CommandNotSupportedException(com.github.jh3nd3rs0n.jargyle.transport.socks5.CommandNotSupportedException) IOException(java.io.IOException) AddressTypeNotSupportedException(com.github.jh3nd3rs0n.jargyle.transport.socks5.AddressTypeNotSupportedException)

Example 4 with Socks5Reply

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;
}
Also used : UnknownHostException(java.net.UnknownHostException) DatagramSocket(java.net.DatagramSocket) Socks5Reply(com.github.jh3nd3rs0n.jargyle.transport.socks5.Socks5Reply) IOException(java.io.IOException) InetAddress(java.net.InetAddress)

Example 5 with Socks5Reply

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();
        }
    }
}
Also used : DatagramSocket(java.net.DatagramSocket) Socks5Reply(com.github.jh3nd3rs0n.jargyle.transport.socks5.Socks5Reply) IOException(java.io.IOException) InetAddress(java.net.InetAddress) FirewallRule(com.github.jh3nd3rs0n.jargyle.server.rules.impl.FirewallRule) Socks5ReplyFirewallRule(com.github.jh3nd3rs0n.jargyle.server.rules.impl.Socks5ReplyFirewallRule)

Aggregations

Socks5Reply (com.github.jh3nd3rs0n.jargyle.transport.socks5.Socks5Reply)18 IOException (java.io.IOException)10 InetAddress (java.net.InetAddress)9 InetSocketAddress (java.net.InetSocketAddress)5 Socket (java.net.Socket)5 Host (com.github.jh3nd3rs0n.jargyle.common.net.Host)4 FirewallRule (com.github.jh3nd3rs0n.jargyle.server.rules.impl.FirewallRule)4 Socks5ReplyFirewallRule (com.github.jh3nd3rs0n.jargyle.server.rules.impl.Socks5ReplyFirewallRule)4 DatagramSocket (java.net.DatagramSocket)4 UnknownHostException (java.net.UnknownHostException)4 HostResolver (com.github.jh3nd3rs0n.jargyle.client.HostResolver)3 Socks5Request (com.github.jh3nd3rs0n.jargyle.transport.socks5.Socks5Request)3 SocketException (java.net.SocketException)3 RelayServer (com.github.jh3nd3rs0n.jargyle.server.RelayServer)2 Socks5ReplyFirewallRules (com.github.jh3nd3rs0n.jargyle.server.rules.impl.Socks5ReplyFirewallRules)2 Reply (com.github.jh3nd3rs0n.jargyle.transport.socks5.Reply)2 ServerSocket (java.net.ServerSocket)2 Properties (com.github.jh3nd3rs0n.jargyle.client.Properties)1 SocketSettings (com.github.jh3nd3rs0n.jargyle.common.net.SocketSettings)1 Socks5RequestFirewallRules (com.github.jh3nd3rs0n.jargyle.server.rules.impl.Socks5RequestFirewallRules)1