Search in sources :

Example 1 with FixedTimeoutHandler

use of org.tinyradius.io.client.timeout.FixedTimeoutHandler in project tinyradius-netty by globalreachtech.

the class TestProxy method main.

public static void main(String[] args) throws Exception {
    final NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup(4);
    final Dictionary dictionary = DefaultDictionary.INSTANCE;
    final Timer timer = new HashedWheelTimer();
    final Bootstrap bootstrap = new Bootstrap().channel(NioDatagramChannel.class).group(eventLoopGroup);
    final SecretProvider secretProvider = remote -> {
        if (remote.getPort() == 11812 || remote.getPort() == 11813)
            return "testing123";
        return remote.getAddress().getHostAddress().equals("127.0.0.1") ? "proxytest" : null;
    };
    final FixedTimeoutHandler retryStrategy = new FixedTimeoutHandler(timer, 3, 1000);
    final RadiusClient radiusClient = new RadiusClient(bootstrap, new InetSocketAddress(0), retryStrategy, new ChannelInitializer<DatagramChannel>() {

        @Override
        protected void initChannel(DatagramChannel ch) {
            ch.pipeline().addLast(new ClientDatagramCodec(dictionary), new PromiseAdapter());
        }
    });
    final ChannelInitializer<DatagramChannel> channelInitializer = new ChannelInitializer<DatagramChannel>() {

        @Override
        protected void initChannel(DatagramChannel ch) {
            ch.pipeline().addLast(new ServerPacketCodec(dictionary, secretProvider), new ProxyHandler(radiusClient) {

                @Override
                public Optional<RadiusEndpoint> getProxyServer(RadiusRequest request, RadiusEndpoint client) {
                    try {
                        InetAddress address = InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 });
                        int port = request instanceof AccountingRequest ? 11813 : 11812;
                        return Optional.of(new RadiusEndpoint(new InetSocketAddress(address, port), "testing123"));
                    } catch (UnknownHostException e) {
                        return Optional.empty();
                    }
                }
            });
        }
    };
    try (RadiusServer proxy = new RadiusServer(bootstrap, channelInitializer, channelInitializer, new InetSocketAddress(1812), new InetSocketAddress(1813))) {
        proxy.isReady().addListener(future1 -> {
            if (future1.isSuccess()) {
                logger.info("Server started");
            } else {
                logger.info("Failed to start server", future1.cause());
                proxy.close();
                eventLoopGroup.shutdownGracefully();
            }
        });
        System.in.read();
    }
    eventLoopGroup.shutdownGracefully();
}
Also used : FixedTimeoutHandler(org.tinyradius.io.client.timeout.FixedTimeoutHandler) ServerPacketCodec(org.tinyradius.io.server.handler.ServerPacketCodec) ClientDatagramCodec(org.tinyradius.io.client.handler.ClientDatagramCodec) RadiusClient(org.tinyradius.io.client.RadiusClient) InetAddress(java.net.InetAddress) DatagramChannel(io.netty.channel.socket.DatagramChannel) AccountingRequest(org.tinyradius.core.packet.request.AccountingRequest) RadiusServer(org.tinyradius.io.server.RadiusServer) RadiusRequest(org.tinyradius.core.packet.request.RadiusRequest) ProxyHandler(org.tinyradius.io.server.handler.ProxyHandler) ChannelInitializer(io.netty.channel.ChannelInitializer) Dictionary(org.tinyradius.core.dictionary.Dictionary) InetSocketAddress(java.net.InetSocketAddress) UnknownHostException(java.net.UnknownHostException) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SecretProvider(org.tinyradius.io.server.SecretProvider) Bootstrap(io.netty.bootstrap.Bootstrap) Logger(org.apache.logging.log4j.Logger) RadiusEndpoint(org.tinyradius.io.RadiusEndpoint) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) DefaultDictionary(org.tinyradius.core.dictionary.DefaultDictionary) HashedWheelTimer(io.netty.util.HashedWheelTimer) Timer(io.netty.util.Timer) PromiseAdapter(org.tinyradius.io.client.handler.PromiseAdapter) Optional(java.util.Optional) LogManager(org.apache.logging.log4j.LogManager) Dictionary(org.tinyradius.core.dictionary.Dictionary) DefaultDictionary(org.tinyradius.core.dictionary.DefaultDictionary) RadiusClient(org.tinyradius.io.client.RadiusClient) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) FixedTimeoutHandler(org.tinyradius.io.client.timeout.FixedTimeoutHandler) InetSocketAddress(java.net.InetSocketAddress) ClientDatagramCodec(org.tinyradius.io.client.handler.ClientDatagramCodec) RadiusRequest(org.tinyradius.core.packet.request.RadiusRequest) AccountingRequest(org.tinyradius.core.packet.request.AccountingRequest) RadiusServer(org.tinyradius.io.server.RadiusServer) Bootstrap(io.netty.bootstrap.Bootstrap) ChannelInitializer(io.netty.channel.ChannelInitializer) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ServerPacketCodec(org.tinyradius.io.server.handler.ServerPacketCodec) Optional(java.util.Optional) UnknownHostException(java.net.UnknownHostException) ProxyHandler(org.tinyradius.io.server.handler.ProxyHandler) DatagramChannel(io.netty.channel.socket.DatagramChannel) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) HashedWheelTimer(io.netty.util.HashedWheelTimer) PromiseAdapter(org.tinyradius.io.client.handler.PromiseAdapter) HashedWheelTimer(io.netty.util.HashedWheelTimer) Timer(io.netty.util.Timer) RadiusEndpoint(org.tinyradius.io.RadiusEndpoint) InetAddress(java.net.InetAddress) SecretProvider(org.tinyradius.io.server.SecretProvider)

Example 2 with FixedTimeoutHandler

use of org.tinyradius.io.client.timeout.FixedTimeoutHandler in project tinyradius-netty by globalreachtech.

the class RadiusClientTest method retainPacketsWithRetries.

@Test
void retainPacketsWithRetries() throws RadiusPacketException {
    final byte id = (byte) random.nextInt(256);
    final CapturingOutboundHandler capturingOutboundHandler = new CapturingOutboundHandler(a -> {
    });
    final RadiusClient radiusClient = new RadiusClient(bootstrap, address, new FixedTimeoutHandler(timer, 2, 0), capturingOutboundHandler);
    final RadiusRequest request = RadiusRequest.create(dictionary, (byte) 1, (byte) 1, null, Collections.emptyList());
    final Future<RadiusResponse> future = radiusClient.communicate(request, stubEndpoint);
    await().until(future::isDone);
    assertFalse(future.isSuccess());
    assertEquals("Client send timeout - max attempts reached: 2", future.cause().getMessage());
    // unpooled, let GC handle it
    assertEquals(1, request.toByteBuf().refCnt());
}
Also used : RadiusResponse(org.tinyradius.core.packet.response.RadiusResponse) FixedTimeoutHandler(org.tinyradius.io.client.timeout.FixedTimeoutHandler) RadiusRequest(org.tinyradius.core.packet.request.RadiusRequest) Test(org.junit.jupiter.api.Test)

Example 3 with FixedTimeoutHandler

use of org.tinyradius.io.client.timeout.FixedTimeoutHandler in project tinyradius-netty by globalreachtech.

the class TestClient method main.

/**
 * Radius command line client.
 *
 * @param args [host, sharedSecret, username, password]
 */
public static void main(String[] args) throws RadiusPacketException {
    if (args.length != 4) {
        logger.info("Usage: TestClient hostName sharedSecret userName password");
        System.exit(1);
    }
    String host = args[0];
    String shared = args[1];
    String user = args[2];
    String pass = args[3];
    final NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup(4);
    final Dictionary dictionary = DefaultDictionary.INSTANCE;
    final Timer timer = new HashedWheelTimer();
    final Bootstrap bootstrap = new Bootstrap().group(eventLoopGroup).channel(NioDatagramChannel.class);
    RadiusClient rc = new RadiusClient(bootstrap, new InetSocketAddress(0), new FixedTimeoutHandler(timer), new ChannelInitializer<DatagramChannel>() {

        @Override
        protected void initChannel(DatagramChannel ch) {
            ch.pipeline().addLast(new ClientDatagramCodec(dictionary), new PromiseAdapter(), new BlacklistHandler(60_000, 3));
        }
    });
    final RadiusEndpoint authEndpoint = new RadiusEndpoint(new InetSocketAddress(host, 1812), shared);
    final RadiusEndpoint acctEndpoint = new RadiusEndpoint(new InetSocketAddress(host, 1813), shared);
    // 1. Send Access-Request
    final AccessRequestPap ar = (AccessRequestPap) ((AccessRequest) RadiusRequest.create(dictionary, ACCESS_REQUEST, (byte) 1, null, Collections.emptyList())).withPapPassword(pass).addAttribute("User-Name", user).addAttribute("NAS-Identifier", "this.is.my.nas-identifier.de").addAttribute("NAS-IP-Address", "192.168.0.100").addAttribute("Service-Type", "Login-User").addAttribute("WISPr-Redirection-URL", "http://www.sourceforge.net/").addAttribute("WISPr-Location-ID", "net.sourceforge.ap1");
    logger.info("Packet before it is sent\n" + ar + "\n");
    RadiusResponse response = rc.communicate(ar, authEndpoint).syncUninterruptibly().getNow();
    logger.info("Packet after it was sent\n" + ar + "\n");
    logger.info("Response\n" + response + "\n");
    // 2. Send Accounting-Request
    final AccountingRequest acc = (AccountingRequest) RadiusRequest.create(dictionary, ACCOUNTING_REQUEST, (byte) 2, null, new ArrayList<>()).addAttribute("User-Name", "username").addAttribute("Acct-Status-Type", "1").addAttribute("Acct-Session-Id", "1234567890").addAttribute("NAS-Identifier", "this.is.my.nas-identifier.de").addAttribute("NAS-Port", "0");
    logger.info(acc + "\n");
    response = rc.communicate(acc, acctEndpoint).syncUninterruptibly().getNow();
    logger.info("Response: " + response);
    rc.close();
}
Also used : Dictionary(org.tinyradius.core.dictionary.Dictionary) DefaultDictionary(org.tinyradius.core.dictionary.DefaultDictionary) RadiusClient(org.tinyradius.io.client.RadiusClient) InetSocketAddress(java.net.InetSocketAddress) FixedTimeoutHandler(org.tinyradius.io.client.timeout.FixedTimeoutHandler) DatagramChannel(io.netty.channel.socket.DatagramChannel) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) BlacklistHandler(org.tinyradius.io.client.handler.BlacklistHandler) HashedWheelTimer(io.netty.util.HashedWheelTimer) ClientDatagramCodec(org.tinyradius.io.client.handler.ClientDatagramCodec) RadiusResponse(org.tinyradius.core.packet.response.RadiusResponse) PromiseAdapter(org.tinyradius.io.client.handler.PromiseAdapter) HashedWheelTimer(io.netty.util.HashedWheelTimer) Timer(io.netty.util.Timer) AccountingRequest(org.tinyradius.core.packet.request.AccountingRequest) Bootstrap(io.netty.bootstrap.Bootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) AccessRequestPap(org.tinyradius.core.packet.request.AccessRequestPap)

Aggregations

FixedTimeoutHandler (org.tinyradius.io.client.timeout.FixedTimeoutHandler)3 Bootstrap (io.netty.bootstrap.Bootstrap)2 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)2 DatagramChannel (io.netty.channel.socket.DatagramChannel)2 NioDatagramChannel (io.netty.channel.socket.nio.NioDatagramChannel)2 HashedWheelTimer (io.netty.util.HashedWheelTimer)2 Timer (io.netty.util.Timer)2 InetSocketAddress (java.net.InetSocketAddress)2 DefaultDictionary (org.tinyradius.core.dictionary.DefaultDictionary)2 Dictionary (org.tinyradius.core.dictionary.Dictionary)2 AccountingRequest (org.tinyradius.core.packet.request.AccountingRequest)2 RadiusRequest (org.tinyradius.core.packet.request.RadiusRequest)2 RadiusResponse (org.tinyradius.core.packet.response.RadiusResponse)2 RadiusClient (org.tinyradius.io.client.RadiusClient)2 ClientDatagramCodec (org.tinyradius.io.client.handler.ClientDatagramCodec)2 PromiseAdapter (org.tinyradius.io.client.handler.PromiseAdapter)2 ChannelInitializer (io.netty.channel.ChannelInitializer)1 InetAddress (java.net.InetAddress)1 UnknownHostException (java.net.UnknownHostException)1 Optional (java.util.Optional)1