Skip to content

Instantly share code, notes, and snippets.

@Jire
Created May 18, 2016 00:42
Show Gist options
  • Save Jire/91e2da74a8d8ddb5c5aaa0a6eab02e23 to your computer and use it in GitHub Desktop.
Save Jire/91e2da74a8d8ddb5c5aaa0a6eab02e23 to your computer and use it in GitHub Desktop.
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import java.net.InetSocketAddress;
/**
* @author Jire
*/
public final class Stress464 {
public static final String HOST = "localhost";
public static final int PORT = 43594;
public static final int CONNECTIONS = 1999;
public static void main(String[] args) {
InetSocketAddress address = new InetSocketAddress(HOST, PORT);
NioEventLoopGroup group = new NioEventLoopGroup();
ChannelInitializer<SocketChannel> initializer = new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addFirst(new ChannelInboundHandlerAdapter() {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
ByteBuf buf = ctx.alloc().buffer(1 + 1 + 1 + 4 + 1 + 1 + 4
+ 4 + (4 * 16) + 1 + 1 + 4 + 8 + 8 + 1);
// handshake
buf.writeByte(14).writeByte(0);
// revision
buf.writeByte(15).writeInt(464);
// login
buf.writeByte(16).writeByte(0).writeInt(464).writeInt(0);
for (int i = 0; i < 16; i++) buf.writeInt(0); // CRC-16 checksums
buf.writeByte(0).writeByte(0); // ISAAC block length/ID
buf.writeInt(1337); // uid
buf.writeLong(convertStringToLong("test"));
buf.writeBytes("password".getBytes()).writeByte(0);
ctx.writeAndFlush(buf);
}
});
}
};
for (int i = 0; i < CONNECTIONS; i++) {
Bootstrap bs = new Bootstrap();
bs.group(group).channel(NioSocketChannel.class)
.option(ChannelOption.SO_KEEPALIVE, true)
.handler(initializer).connect(address);
}
}
private static long reducedMapping(int x) {
long out = -1;
if (x >= 97 && x <= 122)
out = x - 96;
else if (x >= 65 && x <= 90)
out = x - 37;
else if (x >= 48 && x <= 57)
out = x - +5;
else if (x == 32)
out = 63L;
return out;
}
private static long convertStringToLong(String s) {
if (s.length() > 20) {
throw new IllegalArgumentException("String is too long: " + s);
}
long out = 0L;
for (int i = 0; i < s.length(); ++i) {
long m = reducedMapping(s.codePointAt(i));
if (m == -1) {
throw new IllegalArgumentException("Unmapped Character in String: " + s);
}
m <<= ((9 - i) * 6) + 4;
out |= m;
}
return out;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment