All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.rx.net.shadowsocks.ServerReceiveHandler Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
package org.rx.net.shadowsocks;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;

import java.net.InetSocketAddress;

@ChannelHandler.Sharable
public class ServerReceiveHandler extends SimpleChannelInboundHandler {
    public static final ServerReceiveHandler DEFAULT = new ServerReceiveHandler();

    public ServerReceiveHandler() {
        super(false);
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
        boolean isUdp = ctx.channel().attr(SSCommon.IS_UDP).get();
        if (isUdp) {
            DatagramPacket udpRaw = ((DatagramPacket) msg);
            ByteBuf buf = udpRaw.content();
            if (buf.readableBytes() < 4) { //no cipher, min size = 1 + 1 + 2 ,[1-byte type][variable-length host][2-byte port]
                return;
            }
            ctx.channel().attr(SSCommon.REMOTE_ADDRESS).set(udpRaw.sender());
            ctx.fireChannelRead(buf);
            return;
        }

        ctx.channel().attr(SSCommon.REMOTE_ADDRESS).set((InetSocketAddress) ctx.channel().remoteAddress());
        ctx.channel().pipeline().remove(this);
        ctx.fireChannelRead(msg);
    }
}