Search in sources :

Example 1 with Host

use of com.github.jh3nd3rs0n.jargyle.common.net.Host in project jargyle by jh3nd3rs0n.

the class SocksServerTest method testGetHostForChangingConfiguration.

@Test
public void testGetHostForChangingConfiguration() throws IOException {
    ModifiableConfiguration configuration = ModifiableConfiguration.newInstance();
    SocksServer socksServer = new SocksServer(configuration);
    try {
        socksServer.start();
        configuration.addSetting(GeneralSettingSpecConstants.HOST.newSetting(Host.newInstance("127.0.0.1")));
        Host expectedHost = GeneralSettingSpecConstants.HOST.getDefaultSetting().getValue();
        Host actualHost = socksServer.getHost();
        assertEquals(expectedHost, actualHost);
    } finally {
        if (!socksServer.getState().equals(SocksServer.State.STOPPED)) {
            socksServer.stop();
        }
    }
}
Also used : Host(com.github.jh3nd3rs0n.jargyle.common.net.Host) Test(org.junit.Test)

Example 2 with Host

use of com.github.jh3nd3rs0n.jargyle.common.net.Host in project jargyle by jh3nd3rs0n.

the class ConnectCommandWorker method newServerFacingSocket.

private Socket newServerFacingSocket() throws IOException {
    Socks5Reply socks5Rep = null;
    HostResolver hostResolver = this.netObjectFactory.newHostResolver();
    Socket serverFacingSocket = null;
    int connectTimeout = this.settings.getLastValue(Socks5SettingSpecConstants.SOCKS5_ON_CONNECT_SERVER_FACING_CONNECT_TIMEOUT).intValue();
    if (this.settings.getLastValue(Socks5SettingSpecConstants.SOCKS5_ON_CONNECT_PREPARE_SERVER_FACING_SOCKET)) {
        serverFacingSocket = netObjectFactory.newSocket();
        if (!this.configureServerFacingSocket(serverFacingSocket)) {
            return null;
        }
        try {
            serverFacingSocket.connect(new InetSocketAddress(hostResolver.resolve(this.desiredDestinationAddress), this.desiredDestinationPort), connectTimeout);
        } catch (UnknownHostException e) {
            LOGGER.error(ObjectLogMessageHelper.objectLogMessage(this, "Error in connecting the server-facing socket"), e);
            socks5Rep = Socks5Reply.newFailureInstance(Reply.HOST_UNREACHABLE);
            this.commandWorkerContext.sendSocks5Reply(this, socks5Rep, LOGGER);
            return null;
        }
    } else {
        Host bindHost = this.settings.getLastValue(Socks5SettingSpecConstants.SOCKS5_ON_CONNECT_SERVER_FACING_BIND_HOST);
        InetAddress bindInetAddress = bindHost.toInetAddress();
        try {
            serverFacingSocket = this.netObjectFactory.newSocket(this.desiredDestinationAddress, this.desiredDestinationPort, bindInetAddress, 0);
        } catch (UnknownHostException e) {
            LOGGER.error(ObjectLogMessageHelper.objectLogMessage(this, "Error in creating the server-facing socket"), e);
            socks5Rep = Socks5Reply.newFailureInstance(Reply.HOST_UNREACHABLE);
            this.commandWorkerContext.sendSocks5Reply(this, socks5Rep, LOGGER);
            return null;
        }
    }
    return serverFacingSocket;
}
Also used : UnknownHostException(java.net.UnknownHostException) InetSocketAddress(java.net.InetSocketAddress) Socks5Reply(com.github.jh3nd3rs0n.jargyle.transport.socks5.Socks5Reply) Host(com.github.jh3nd3rs0n.jargyle.common.net.Host) HostResolver(com.github.jh3nd3rs0n.jargyle.client.HostResolver) InetAddress(java.net.InetAddress) Socket(java.net.Socket)

Example 3 with Host

use of com.github.jh3nd3rs0n.jargyle.common.net.Host in project jargyle by jh3nd3rs0n.

the class ConnectCommandWorker method configureServerFacingSocket.

private boolean configureServerFacingSocket(final Socket serverFacingSocket) {
    SocketSettings socketSettings = this.settings.getLastValue(Socks5SettingSpecConstants.SOCKS5_ON_CONNECT_SERVER_FACING_SOCKET_SETTINGS);
    Host bindHost = this.settings.getLastValue(Socks5SettingSpecConstants.SOCKS5_ON_CONNECT_SERVER_FACING_BIND_HOST);
    InetAddress bindInetAddress = bindHost.toInetAddress();
    try {
        socketSettings.applyTo(serverFacingSocket);
        serverFacingSocket.bind(new InetSocketAddress(bindInetAddress, 0));
    } catch (SocketException e) {
        LOGGER.error(ObjectLogMessageHelper.objectLogMessage(this, "Error in setting the server-facing socket"), e);
        Socks5Reply socks5Rep = Socks5Reply.newFailureInstance(Reply.GENERAL_SOCKS_SERVER_FAILURE);
        this.commandWorkerContext.sendSocks5Reply(this, socks5Rep, LOGGER);
        return false;
    } catch (IOException e) {
        LOGGER.error(ObjectLogMessageHelper.objectLogMessage(this, "Error in binding the server-facing socket"), e);
        Socks5Reply socks5Rep = Socks5Reply.newFailureInstance(Reply.GENERAL_SOCKS_SERVER_FAILURE);
        this.commandWorkerContext.sendSocks5Reply(this, socks5Rep, LOGGER);
        return false;
    }
    return true;
}
Also used : SocketException(java.net.SocketException) InetSocketAddress(java.net.InetSocketAddress) Socks5Reply(com.github.jh3nd3rs0n.jargyle.transport.socks5.Socks5Reply) Host(com.github.jh3nd3rs0n.jargyle.common.net.Host) IOException(java.io.IOException) SocketSettings(com.github.jh3nd3rs0n.jargyle.common.net.SocketSettings) InetAddress(java.net.InetAddress)

Example 4 with Host

use of com.github.jh3nd3rs0n.jargyle.common.net.Host in project jargyle by jh3nd3rs0n.

the class UdpAssociateCommandWorker method newClientFacingDatagramSocket.

private DatagramSocket newClientFacingDatagramSocket() {
    Host bindHost = this.settings.getLastValue(Socks5SettingSpecConstants.SOCKS5_ON_UDP_ASSOCIATE_CLIENT_FACING_BIND_HOST);
    InetAddress bindInetAddress = bindHost.toInetAddress();
    DatagramSocket clientFacingDatagramSock = null;
    try {
        clientFacingDatagramSock = new DatagramSocket(new InetSocketAddress(bindInetAddress, 0));
    } catch (SocketException e) {
        LOGGER.error(ObjectLogMessageHelper.objectLogMessage(this, "Error in creating the client-facing UDP socket"), e);
        Socks5Reply socks5Rep = Socks5Reply.newFailureInstance(Reply.GENERAL_SOCKS_SERVER_FAILURE);
        this.commandWorkerContext.sendSocks5Reply(this, socks5Rep, LOGGER);
        return null;
    }
    return clientFacingDatagramSock;
}
Also used : SocketException(java.net.SocketException) DatagramSocket(java.net.DatagramSocket) InetSocketAddress(java.net.InetSocketAddress) Socks5Reply(com.github.jh3nd3rs0n.jargyle.transport.socks5.Socks5Reply) Host(com.github.jh3nd3rs0n.jargyle.common.net.Host) InetAddress(java.net.InetAddress)

Example 5 with Host

use of com.github.jh3nd3rs0n.jargyle.common.net.Host in project jargyle by jh3nd3rs0n.

the class UdpAssociateCommandWorker method newPeerFacingDatagramSocket.

private DatagramSocket newPeerFacingDatagramSocket() {
    Host bindHost = this.settings.getLastValue(Socks5SettingSpecConstants.SOCKS5_ON_UDP_ASSOCIATE_PEER_FACING_BIND_HOST);
    InetAddress bindInetAddress = bindHost.toInetAddress();
    DatagramSocket peerFacingDatagramSock = null;
    try {
        peerFacingDatagramSock = this.netObjectFactory.newDatagramSocket(new InetSocketAddress(bindInetAddress, 0));
    } catch (SocketException e) {
        LOGGER.error(ObjectLogMessageHelper.objectLogMessage(this, "Error in creating the peer-facing UDP socket"), e);
        Socks5Reply socks5Rep = Socks5Reply.newFailureInstance(Reply.GENERAL_SOCKS_SERVER_FAILURE);
        this.commandWorkerContext.sendSocks5Reply(this, socks5Rep, LOGGER);
        return null;
    }
    return peerFacingDatagramSock;
}
Also used : SocketException(java.net.SocketException) DatagramSocket(java.net.DatagramSocket) InetSocketAddress(java.net.InetSocketAddress) Socks5Reply(com.github.jh3nd3rs0n.jargyle.transport.socks5.Socks5Reply) Host(com.github.jh3nd3rs0n.jargyle.common.net.Host) InetAddress(java.net.InetAddress)

Aggregations

Host (com.github.jh3nd3rs0n.jargyle.common.net.Host)5 Socks5Reply (com.github.jh3nd3rs0n.jargyle.transport.socks5.Socks5Reply)4 InetAddress (java.net.InetAddress)4 InetSocketAddress (java.net.InetSocketAddress)4 SocketException (java.net.SocketException)3 DatagramSocket (java.net.DatagramSocket)2 HostResolver (com.github.jh3nd3rs0n.jargyle.client.HostResolver)1 SocketSettings (com.github.jh3nd3rs0n.jargyle.common.net.SocketSettings)1 IOException (java.io.IOException)1 Socket (java.net.Socket)1 UnknownHostException (java.net.UnknownHostException)1 Test (org.junit.Test)1