Search in sources :

Example 1 with Dictionary

use of org.tinyradius.core.dictionary.Dictionary in project tinyradius-netty by globalreachtech.

the class IntegerAttribute method stringParser.

public static byte[] stringParser(Dictionary dictionary, int vendorId, int type, String value) {
    final int integer = dictionary.getAttributeTemplate(vendorId, type).map(at -> at.getEnumeration(value)).orElseGet(() -> Integer.parseUnsignedInt(value));
    final ByteBuf byteBuf = Unpooled.buffer(Integer.BYTES, Integer.BYTES).writeInt(integer);
    return dictionary.getAttributeTemplate(vendorId, type).filter(AttributeTemplate::isTagged).map(// skip first octet if has_tag
    x -> byteBuf.copy(1, 3)).orElse(byteBuf).array();
}
Also used : AttributeTemplate(org.tinyradius.core.attribute.AttributeTemplate) ByteBuf(io.netty.buffer.ByteBuf) Dictionary(org.tinyradius.core.dictionary.Dictionary) ByteBuffer(java.nio.ByteBuffer) Unpooled(io.netty.buffer.Unpooled) ByteBuf(io.netty.buffer.ByteBuf)

Example 2 with Dictionary

use of org.tinyradius.core.dictionary.Dictionary 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 3 with Dictionary

use of org.tinyradius.core.dictionary.Dictionary in project tinyradius-netty by globalreachtech.

the class TestServer method main.

public static void main(String[] args) throws Exception {
    final Dictionary dictionary = DefaultDictionary.INSTANCE;
    final EventLoopGroup eventLoopGroup = new NioEventLoopGroup(4);
    final Bootstrap bootstrap = new Bootstrap().channel(NioDatagramChannel.class).group(eventLoopGroup);
    final SecretProvider secretProvider = remote -> remote.getAddress().getHostAddress().equals("127.0.0.1") ? "testing123" : null;
    final ServerPacketCodec serverPacketCodec = new ServerPacketCodec(dictionary, secretProvider);
    final Timer timer = new HashedWheelTimer();
    final BasicCachingHandler cachingHandlerAuth = new BasicCachingHandler(timer, 5000);
    final BasicCachingHandler cachingHandlerAcct = new BasicCachingHandler(timer, 5000);
    final SimpleAccessHandler simpleAccessHandler = new SimpleAccessHandler();
    final SimpleAccountingHandler simpleAccountingHandler = new SimpleAccountingHandler();
    try (RadiusServer server = new RadiusServer(bootstrap, new ChannelInitializer<DatagramChannel>() {

        @Override
        protected void initChannel(DatagramChannel ch) {
            ch.pipeline().addLast(serverPacketCodec, cachingHandlerAuth, simpleAccessHandler);
        }
    }, new ChannelInitializer<DatagramChannel>() {

        @Override
        protected void initChannel(DatagramChannel ch) {
            ch.pipeline().addLast(serverPacketCodec, cachingHandlerAcct, simpleAccountingHandler);
        }
    }, new InetSocketAddress(11812), new InetSocketAddress(11813))) {
        server.isReady().addListener(future1 -> {
            if (future1.isSuccess()) {
                logger.info("Server started");
            } else {
                logger.info("Failed to start server", future1.cause());
                server.close();
                eventLoopGroup.shutdownGracefully();
            }
        });
        System.in.read();
    }
    eventLoopGroup.shutdownGracefully();
}
Also used : ServerPacketCodec(org.tinyradius.io.server.handler.ServerPacketCodec) BasicCachingHandler(org.tinyradius.io.server.handler.BasicCachingHandler) RequestHandler(org.tinyradius.io.server.handler.RequestHandler) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) DatagramChannel(io.netty.channel.socket.DatagramChannel) AccountingRequest(org.tinyradius.core.packet.request.AccountingRequest) AccessRequestPap(org.tinyradius.core.packet.request.AccessRequestPap) RadiusServer(org.tinyradius.io.server.RadiusServer) RadiusRequest(org.tinyradius.core.packet.request.RadiusRequest) EventLoopGroup(io.netty.channel.EventLoopGroup) ChannelInitializer(io.netty.channel.ChannelInitializer) AccessRequest(org.tinyradius.core.packet.request.AccessRequest) RequestCtx(org.tinyradius.io.server.RequestCtx) Dictionary(org.tinyradius.core.dictionary.Dictionary) InetSocketAddress(java.net.InetSocketAddress) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SecretProvider(org.tinyradius.io.server.SecretProvider) RadiusResponse(org.tinyradius.core.packet.response.RadiusResponse) Bootstrap(io.netty.bootstrap.Bootstrap) Logger(org.apache.logging.log4j.Logger) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) DefaultDictionary(org.tinyradius.core.dictionary.DefaultDictionary) HashedWheelTimer(io.netty.util.HashedWheelTimer) Timer(io.netty.util.Timer) LogManager(org.apache.logging.log4j.LogManager) RadiusPacketException(org.tinyradius.core.RadiusPacketException) PacketType(org.tinyradius.core.packet.PacketType) Dictionary(org.tinyradius.core.dictionary.Dictionary) DefaultDictionary(org.tinyradius.core.dictionary.DefaultDictionary) ServerPacketCodec(org.tinyradius.io.server.handler.ServerPacketCodec) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(io.netty.channel.socket.DatagramChannel) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) HashedWheelTimer(io.netty.util.HashedWheelTimer) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) HashedWheelTimer(io.netty.util.HashedWheelTimer) Timer(io.netty.util.Timer) BasicCachingHandler(org.tinyradius.io.server.handler.BasicCachingHandler) RadiusServer(org.tinyradius.io.server.RadiusServer) Bootstrap(io.netty.bootstrap.Bootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SecretProvider(org.tinyradius.io.server.SecretProvider)

Example 4 with Dictionary

use of org.tinyradius.core.dictionary.Dictionary in project tinyradius-netty by globalreachtech.

the class RadiusClientTest method communicateEndpointListFirstSuccess.

@Test
void communicateEndpointListFirstSuccess() throws RadiusPacketException {
    final byte id = (byte) random.nextInt(256);
    final RadiusResponse response = RadiusResponse.create(DefaultDictionary.INSTANCE, (byte) 2, id, null, Collections.emptyList());
    final CapturingOutboundHandler capturingOutboundHandler = spy(new CapturingOutboundHandler(p -> p.trySuccess(response)));
    final RadiusClient radiusClient = new RadiusClient(bootstrap, address, timeoutHandler, capturingOutboundHandler);
    final InetSocketAddress address2 = new InetSocketAddress(1);
    // never used
    final RadiusEndpoint stubEndpoint2 = new RadiusEndpoint(address2, "secret2");
    final InetSocketAddress address3 = new InetSocketAddress(2);
    // never used
    final RadiusEndpoint stubEndpoint3 = new RadiusEndpoint(address3, "secret3");
    final List<RadiusEndpoint> endpoints = Arrays.asList(stubEndpoint, stubEndpoint2, stubEndpoint3);
    final RadiusRequest request = RadiusRequest.create(dictionary, (byte) 1, (byte) 1, null, Collections.emptyList());
    final Future<RadiusResponse> future = radiusClient.communicate(request, endpoints);
    await().until(future::isDone);
    assertTrue(future.isSuccess());
    assertEquals(response, future.getNow());
    assertEquals(1, capturingOutboundHandler.requests.size());
    assertEquals("secret", capturingOutboundHandler.requests.get(0).getEndpoint().getSecret());
}
Also used : FixedTimeoutHandler(org.tinyradius.io.client.timeout.FixedTimeoutHandler) Arrays(java.util.Arrays) TimeoutException(java.util.concurrent.TimeoutException) ArrayList(java.util.ArrayList) SecureRandom(java.security.SecureRandom) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Spy(org.mockito.Spy) RadiusRequest(org.tinyradius.core.packet.request.RadiusRequest) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Awaitility.await(org.awaitility.Awaitility.await) ChannelOutboundHandlerAdapter(io.netty.channel.ChannelOutboundHandlerAdapter) Promise(io.netty.util.concurrent.Promise) Dictionary(org.tinyradius.core.dictionary.Dictionary) InetSocketAddress(java.net.InetSocketAddress) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) TimeoutHandler(org.tinyradius.io.client.timeout.TimeoutHandler) RadiusResponse(org.tinyradius.core.packet.response.RadiusResponse) Test(org.junit.jupiter.api.Test) Consumer(java.util.function.Consumer) Bootstrap(io.netty.bootstrap.Bootstrap) Mockito(org.mockito.Mockito) List(java.util.List) RadiusEndpoint(org.tinyradius.io.RadiusEndpoint) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) DefaultDictionary(org.tinyradius.core.dictionary.DefaultDictionary) HashedWheelTimer(io.netty.util.HashedWheelTimer) Assertions(org.junit.jupiter.api.Assertions) Timer(io.netty.util.Timer) Future(io.netty.util.concurrent.Future) Collections(java.util.Collections) RadiusPacketException(org.tinyradius.core.RadiusPacketException) RadiusResponse(org.tinyradius.core.packet.response.RadiusResponse) RadiusEndpoint(org.tinyradius.io.RadiusEndpoint) InetSocketAddress(java.net.InetSocketAddress) RadiusRequest(org.tinyradius.core.packet.request.RadiusRequest) Test(org.junit.jupiter.api.Test)

Example 5 with Dictionary

use of org.tinyradius.core.dictionary.Dictionary 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

Dictionary (org.tinyradius.core.dictionary.Dictionary)15 Test (org.junit.jupiter.api.Test)11 DefaultDictionary (org.tinyradius.core.dictionary.DefaultDictionary)10 Bootstrap (io.netty.bootstrap.Bootstrap)6 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)6 NioDatagramChannel (io.netty.channel.socket.nio.NioDatagramChannel)6 HashedWheelTimer (io.netty.util.HashedWheelTimer)6 Timer (io.netty.util.Timer)6 InetSocketAddress (java.net.InetSocketAddress)6 RadiusRequest (org.tinyradius.core.packet.request.RadiusRequest)5 RadiusResponse (org.tinyradius.core.packet.response.RadiusResponse)5 FixedTimeoutHandler (org.tinyradius.io.client.timeout.FixedTimeoutHandler)5 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)4 RadiusPacketException (org.tinyradius.core.RadiusPacketException)4 AttributeTemplate (org.tinyradius.core.attribute.AttributeTemplate)4 RadiusEndpoint (org.tinyradius.io.RadiusEndpoint)4 ChannelOutboundHandlerAdapter (io.netty.channel.ChannelOutboundHandlerAdapter)3 ChannelPromise (io.netty.channel.ChannelPromise)3 DatagramChannel (io.netty.channel.socket.DatagramChannel)3 Future (io.netty.util.concurrent.Future)3