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.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.StringUtil;
26  
27  import java.util.List;
28  import java.util.RandomAccess;
29  
30  /**
31   * Encodes a client-side {@link Socks5Message} into a {@link ByteBuf}.
32   */
33  @Sharable
34  public class Socks5ClientEncoder extends MessageToByteEncoder<Socks5Message> {
35  
36      public static final Socks5ClientEncoder DEFAULT = new Socks5ClientEncoder();
37  
38      private final Socks5AddressEncoder addressEncoder;
39  
40      /**
41       * Creates a new instance with the default {@link Socks5AddressEncoder}.
42       */
43      protected Socks5ClientEncoder() {
44          this(Socks5AddressEncoder.DEFAULT);
45      }
46  
47      /**
48       * Creates a new instance with the specified {@link Socks5AddressEncoder}.
49       */
50      public Socks5ClientEncoder(Socks5AddressEncoder addressEncoder) {
51          if (addressEncoder == null) {
52              throw new NullPointerException("addressEncoder");
53          }
54  
55          this.addressEncoder = addressEncoder;
56      }
57  
58      /**
59       * Returns the {@link Socks5AddressEncoder} of this encoder.
60       */
61      protected final Socks5AddressEncoder addressEncoder() {
62          return addressEncoder;
63      }
64  
65      @Override
66      protected void encode(ChannelHandlerContext ctx, Socks5Message msg, ByteBuf out) throws Exception {
67          if (msg instanceof Socks5InitialRequest) {
68              encodeAuthMethodRequest((Socks5InitialRequest) msg, out);
69          } else if (msg instanceof Socks5PasswordAuthRequest) {
70              encodePasswordAuthRequest((Socks5PasswordAuthRequest) msg, out);
71          } else if (msg instanceof Socks5CommandRequest) {
72              encodeCommandRequest((Socks5CommandRequest) msg, out);
73          } else {
74              throw new EncoderException("unsupported message type: " + StringUtil.simpleClassName(msg));
75          }
76      }
77  
78      private static void encodeAuthMethodRequest(Socks5InitialRequest msg, ByteBuf out) {
79          out.writeByte(msg.version().byteValue());
80  
81          final List<Socks5AuthMethod> authMethods = msg.authMethods();
82          final int numAuthMethods = authMethods.size();
83          out.writeByte(numAuthMethods);
84  
85          if (authMethods instanceof RandomAccess) {
86              for (int i = 0; i < numAuthMethods; i ++) {
87                  out.writeByte(authMethods.get(i).byteValue());
88              }
89          } else {
90              for (Socks5AuthMethod a: authMethods) {
91                  out.writeByte(a.byteValue());
92              }
93          }
94      }
95  
96      private static void encodePasswordAuthRequest(Socks5PasswordAuthRequest msg, ByteBuf out) {
97          out.writeByte(0x01);
98  
99          final String username = msg.username();
100         out.writeByte(username.length());
101         ByteBufUtil.writeAscii(out, username);
102 
103         final String password = msg.password();
104         out.writeByte(password.length());
105         ByteBufUtil.writeAscii(out, password);
106     }
107 
108     private void encodeCommandRequest(Socks5CommandRequest msg, ByteBuf out) throws Exception {
109         out.writeByte(msg.version().byteValue());
110         out.writeByte(msg.type().byteValue());
111         out.writeByte(0x00);
112 
113         final Socks5AddressType dstAddrType = msg.dstAddrType();
114         out.writeByte(dstAddrType.byteValue());
115         addressEncoder.encodeAddress(dstAddrType, msg.dstAddr(), out);
116         out.writeShort(msg.dstPort());
117     }
118 }