View Javadoc
1   /*
2    * Copyright 2014 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
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.StringUtil;
25  
26  /**
27   * Encodes a server-side {@link Socks5Message} into a {@link ByteBuf}.
28   */
29  @Sharable
30  public class Socks5ServerEncoder extends MessageToByteEncoder<Socks5Message> {
31  
32      public static final Socks5ServerEncoder DEFAULT = new Socks5ServerEncoder(Socks5AddressEncoder.DEFAULT);
33  
34      private final Socks5AddressEncoder addressEncoder;
35  
36      /**
37       * Creates a new instance with the default {@link Socks5AddressEncoder}.
38       */
39      protected Socks5ServerEncoder() {
40          this(Socks5AddressEncoder.DEFAULT);
41      }
42  
43      /**
44       * Creates a new instance with the specified {@link Socks5AddressEncoder}.
45       */
46      public Socks5ServerEncoder(Socks5AddressEncoder addressEncoder) {
47          if (addressEncoder == null) {
48              throw new NullPointerException("addressEncoder");
49          }
50  
51          this.addressEncoder = addressEncoder;
52      }
53  
54      /**
55       * Returns the {@link Socks5AddressEncoder} of this encoder.
56       */
57      protected final Socks5AddressEncoder addressEncoder() {
58          return addressEncoder;
59      }
60  
61      @Override
62      protected void encode(ChannelHandlerContext ctx, Socks5Message msg, ByteBuf out) throws Exception {
63          if (msg instanceof Socks5InitialResponse) {
64              encodeAuthMethodResponse((Socks5InitialResponse) msg, out);
65          } else if (msg instanceof Socks5PasswordAuthResponse) {
66              encodePasswordAuthResponse((Socks5PasswordAuthResponse) msg, out);
67          } else if (msg instanceof Socks5CommandResponse) {
68              encodeCommandResponse((Socks5CommandResponse) msg, out);
69          } else {
70              throw new EncoderException("unsupported message type: " + StringUtil.simpleClassName(msg));
71          }
72      }
73  
74      private static void encodeAuthMethodResponse(Socks5InitialResponse msg, ByteBuf out) {
75          out.writeByte(msg.version().byteValue());
76          out.writeByte(msg.authMethod().byteValue());
77      }
78  
79      private static void encodePasswordAuthResponse(Socks5PasswordAuthResponse msg, ByteBuf out) {
80          out.writeByte(0x01);
81          out.writeByte(msg.status().byteValue());
82      }
83  
84      private void encodeCommandResponse(Socks5CommandResponse msg, ByteBuf out) throws Exception {
85          out.writeByte(msg.version().byteValue());
86          out.writeByte(msg.status().byteValue());
87          out.writeByte(0x00);
88  
89          final Socks5AddressType bndAddrType = msg.bndAddrType();
90          out.writeByte(bndAddrType.byteValue());
91          addressEncoder.encodeAddress(bndAddrType, msg.bndAddr(), out);
92  
93          out.writeShort(msg.bndPort());
94      }
95  }