Search in sources :

Example 6 with Socks5Reply

use of com.github.jh3nd3rs0n.jargyle.transport.socks5.Socks5Reply in project jargyle by jh3nd3rs0n.

the class ConnectCommandWorker method run.

@Override
public void run() throws IOException {
    Socks5Reply socks5Rep = null;
    Socket serverFacingSocket = null;
    try {
        serverFacingSocket = this.newServerFacingSocket();
        if (serverFacingSocket == null) {
            return;
        }
        String serverBoundAddress = serverFacingSocket.getInetAddress().getHostAddress();
        int serverBoundPort = serverFacingSocket.getPort();
        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;
        }
        RelayServer.Builder builder = new RelayServer.Builder(this.clientFacingSocket, serverFacingSocket);
        builder.bufferSize(this.settings.getLastValue(Socks5SettingSpecConstants.SOCKS5_ON_CONNECT_RELAY_BUFFER_SIZE).intValue());
        builder.idleTimeout(this.settings.getLastValue(Socks5SettingSpecConstants.SOCKS5_ON_CONNECT_RELAY_IDLE_TIMEOUT).intValue());
        try {
            TcpBasedCommandWorkerHelper.passData(builder);
        } catch (IOException e) {
            LOGGER.error(ObjectLogMessageHelper.objectLogMessage(this, "Error in starting to pass data"), e);
        }
    } finally {
        if (serverFacingSocket != null && !serverFacingSocket.isClosed()) {
            serverFacingSocket.close();
        }
    }
}
Also used : RelayServer(com.github.jh3nd3rs0n.jargyle.server.RelayServer) Socks5Reply(com.github.jh3nd3rs0n.jargyle.transport.socks5.Socks5Reply) IOException(java.io.IOException) FirewallRule(com.github.jh3nd3rs0n.jargyle.server.rules.impl.FirewallRule) Socks5ReplyFirewallRule(com.github.jh3nd3rs0n.jargyle.server.rules.impl.Socks5ReplyFirewallRule) Socket(java.net.Socket)

Example 7 with Socks5Reply

use of com.github.jh3nd3rs0n.jargyle.transport.socks5.Socks5Reply in project jargyle by jh3nd3rs0n.

the class ResolveCommandWorker method run.

@Override
public void run() throws IOException {
    Socks5Reply socks5Rep = null;
    HostResolver hostResolver = this.netObjectFactory.newHostResolver();
    InetAddress inetAddress = null;
    try {
        inetAddress = hostResolver.resolve(this.desiredDestinationAddress);
    } catch (UnknownHostException e) {
        LOGGER.error(ObjectLogMessageHelper.objectLogMessage(this, "Error in resolving the hostname"), e);
        socks5Rep = Socks5Reply.newFailureInstance(Reply.HOST_UNREACHABLE);
        this.commandWorkerContext.sendSocks5Reply(this, socks5Rep, LOGGER);
        return;
    } catch (IOException e) {
        LOGGER.error(ObjectLogMessageHelper.objectLogMessage(this, "Error in resolving the hostname"), e);
        socks5Rep = Socks5Reply.newFailureInstance(Reply.GENERAL_SOCKS_SERVER_FAILURE);
        this.commandWorkerContext.sendSocks5Reply(this, socks5Rep, LOGGER);
        return;
    }
    String serverBoundAddress = inetAddress.getHostAddress();
    int serverBoundPort = desiredDestinationPort;
    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;
    }
    this.commandWorkerContext.sendSocks5Reply(this, socks5Rep, LOGGER);
}
Also used : UnknownHostException(java.net.UnknownHostException) Socks5Reply(com.github.jh3nd3rs0n.jargyle.transport.socks5.Socks5Reply) IOException(java.io.IOException) HostResolver(com.github.jh3nd3rs0n.jargyle.client.HostResolver) InetAddress(java.net.InetAddress) FirewallRule(com.github.jh3nd3rs0n.jargyle.server.rules.impl.FirewallRule) Socks5ReplyFirewallRule(com.github.jh3nd3rs0n.jargyle.server.rules.impl.Socks5ReplyFirewallRule)

Example 8 with Socks5Reply

use of com.github.jh3nd3rs0n.jargyle.transport.socks5.Socks5Reply in project jargyle by jh3nd3rs0n.

the class Socks5RequestWorkerContext method canAllowSocks5Reply.

public final boolean canAllowSocks5Reply(final Object worker, final FirewallRule.Context context, final Logger logger) {
    Socks5ReplyFirewallRules socks5ReplyFirewallRules = this.getSettings().getLastValue(Socks5SettingSpecConstants.SOCKS5_SOCKS5_REPLY_FIREWALL_RULES);
    socks5ReplyFirewallRules.applyTo(context);
    if (FirewallRuleAction.ALLOW.equals(context.getFirewallRuleAction())) {
        return true;
    }
    Socks5Reply rep = Socks5Reply.newFailureInstance(Reply.CONNECTION_NOT_ALLOWED_BY_RULESET);
    this.sendSocks5Reply(worker, rep, logger);
    return false;
}
Also used : Socks5ReplyFirewallRules(com.github.jh3nd3rs0n.jargyle.server.rules.impl.Socks5ReplyFirewallRules) Socks5Reply(com.github.jh3nd3rs0n.jargyle.transport.socks5.Socks5Reply)

Example 9 with Socks5Reply

use of com.github.jh3nd3rs0n.jargyle.transport.socks5.Socks5Reply in project jargyle by jh3nd3rs0n.

the class BindCommandWorker method run.

@Override
public void run() throws IOException {
    Socks5Reply socks5Rep = null;
    ServerSocket listenSocket = null;
    Socket inboundSocket = null;
    try {
        listenSocket = this.netObjectFactory.newServerSocket();
        if (!this.configureListenSocket(listenSocket)) {
            return;
        }
        if (!this.bindListenSocket(listenSocket)) {
            return;
        }
        InetAddress inetAddress = listenSocket.getInetAddress();
        String serverBoundAddress = inetAddress.getHostAddress();
        int serverBoundPort = listenSocket.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;
        }
        inboundSocket = this.acceptInboundSocketFrom(listenSocket);
        listenSocket.close();
        if (inboundSocket == null) {
            return;
        }
        if (!this.configureInboundSocket(inboundSocket)) {
            return;
        }
        serverBoundAddress = inboundSocket.getInetAddress().getHostAddress();
        serverBoundPort = inboundSocket.getLocalPort();
        socks5Rep = Socks5Reply.newInstance(Reply.SUCCEEDED, serverBoundAddress, serverBoundPort);
        context = new Socks5ReplyFirewallRule.Context(this.clientFacingSocket.getInetAddress().getHostAddress(), this.clientFacingSocket.getLocalAddress().getHostAddress(), this.methodSubnegotiationResults, this.socks5Request, socks5Rep);
        if (!this.canAllowSecondSocks5Reply(context)) {
            return;
        }
        if (!this.commandWorkerContext.sendSocks5Reply(this, socks5Rep, LOGGER)) {
            return;
        }
        RelayServer.Builder builder = new RelayServer.Builder(this.clientFacingSocket, inboundSocket);
        builder.bufferSize(this.settings.getLastValue(Socks5SettingSpecConstants.SOCKS5_ON_BIND_RELAY_BUFFER_SIZE).intValue());
        builder.idleTimeout(this.settings.getLastValue(Socks5SettingSpecConstants.SOCKS5_ON_BIND_RELAY_IDLE_TIMEOUT).intValue());
        try {
            TcpBasedCommandWorkerHelper.passData(builder);
        } catch (IOException e) {
            LOGGER.error(ObjectLogMessageHelper.objectLogMessage(this, "Error in starting to pass data"), e);
        }
    } finally {
        if (inboundSocket != null && !inboundSocket.isClosed()) {
            inboundSocket.close();
        }
        if (listenSocket != null && !listenSocket.isClosed()) {
            listenSocket.close();
        }
    }
}
Also used : Socks5Reply(com.github.jh3nd3rs0n.jargyle.transport.socks5.Socks5Reply) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) Socks5ReplyFirewallRule(com.github.jh3nd3rs0n.jargyle.server.rules.impl.Socks5ReplyFirewallRule) RelayServer(com.github.jh3nd3rs0n.jargyle.server.RelayServer) InetAddress(java.net.InetAddress) FirewallRule(com.github.jh3nd3rs0n.jargyle.server.rules.impl.FirewallRule) Socks5ReplyFirewallRule(com.github.jh3nd3rs0n.jargyle.server.rules.impl.Socks5ReplyFirewallRule) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket)

Example 10 with Socks5Reply

use of com.github.jh3nd3rs0n.jargyle.transport.socks5.Socks5Reply in project jargyle by jh3nd3rs0n.

the class BindCommandWorker method bindListenSocket.

private boolean bindListenSocket(final ServerSocket listenSocket) {
    Socks5Reply socks5Rep = null;
    HostResolver hostResolver = this.netObjectFactory.newHostResolver();
    try {
        listenSocket.bind(new InetSocketAddress(hostResolver.resolve(this.desiredDestinationAddress), this.desiredDestinationPort));
    } catch (IOException e) {
        LOGGER.error(ObjectLogMessageHelper.objectLogMessage(this, "Error in binding the listen socket"), e);
        socks5Rep = Socks5Reply.newFailureInstance(Reply.GENERAL_SOCKS_SERVER_FAILURE);
        this.commandWorkerContext.sendSocks5Reply(this, socks5Rep, LOGGER);
        return false;
    }
    return true;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Socks5Reply(com.github.jh3nd3rs0n.jargyle.transport.socks5.Socks5Reply) IOException(java.io.IOException) HostResolver(com.github.jh3nd3rs0n.jargyle.client.HostResolver)

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