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();
}
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();
}
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();
}
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());
}
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();
}
Aggregations