1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.netty.handler.codec.socksx.v5;
18
19 import io.netty.buffer.ByteBuf;
20 import io.netty.buffer.ByteBufUtil;
21 import io.netty.channel.ChannelHandler.Sharable;
22 import io.netty.channel.ChannelHandlerContext;
23 import io.netty.handler.codec.EncoderException;
24 import io.netty.handler.codec.MessageToByteEncoder;
25 import io.netty.util.internal.ObjectUtil;
26 import io.netty.util.internal.StringUtil;
27
28
29
30
31 @Sharable
32 public class Socks5ServerEncoder extends MessageToByteEncoder<Socks5Message> {
33
34 public static final Socks5ServerEncoder DEFAULT = new Socks5ServerEncoder(Socks5AddressEncoder.DEFAULT);
35
36 private final Socks5AddressEncoder addressEncoder;
37
38
39
40
41 protected Socks5ServerEncoder() {
42 this(Socks5AddressEncoder.DEFAULT);
43 }
44
45
46
47
48 public Socks5ServerEncoder(Socks5AddressEncoder addressEncoder) {
49 super(Socks5Message.class);
50 this.addressEncoder = ObjectUtil.checkNotNull(addressEncoder, "addressEncoder");
51 }
52
53
54
55
56 protected final Socks5AddressEncoder addressEncoder() {
57 return addressEncoder;
58 }
59
60 @Override
61 protected void encode(ChannelHandlerContext ctx, Socks5Message msg, ByteBuf out) throws Exception {
62 if (msg instanceof Socks5InitialResponse) {
63 encodeAuthMethodResponse((Socks5InitialResponse) msg, out);
64 } else if (msg instanceof Socks5PasswordAuthResponse) {
65 encodePasswordAuthResponse((Socks5PasswordAuthResponse) msg, out);
66 } else if (msg instanceof Socks5CommandResponse) {
67 encodeCommandResponse((Socks5CommandResponse) msg, out);
68 } else {
69 throw new EncoderException("unsupported message type: " + StringUtil.simpleClassName(msg));
70 }
71 }
72
73 private static void encodeAuthMethodResponse(Socks5InitialResponse msg, ByteBuf out) {
74 out.writeByte(msg.version().byteValue());
75 out.writeByte(msg.authMethod().byteValue());
76 }
77
78 private static void encodePasswordAuthResponse(Socks5PasswordAuthResponse msg, ByteBuf out) {
79 out.writeByte(0x01);
80 out.writeByte(msg.status().byteValue());
81 }
82
83 private void encodeCommandResponse(Socks5CommandResponse msg, ByteBuf out) throws Exception {
84 out.writeByte(msg.version().byteValue());
85 out.writeByte(msg.status().byteValue());
86 out.writeByte(0x00);
87
88 final Socks5AddressType bndAddrType = msg.bndAddrType();
89 out.writeByte(bndAddrType.byteValue());
90 addressEncoder.encodeAddress(bndAddrType, msg.bndAddr(), out);
91
92 ByteBufUtil.writeShortBE(out, msg.bndPort());
93 }
94 }