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