View Javadoc

1   /*
2    * Copyright 2012 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  package io.netty.example.socksproxy;
17  
18  import io.netty.channel.ChannelHandler;
19  import io.netty.channel.ChannelHandlerContext;
20  import io.netty.channel.SimpleChannelInboundHandler;
21  import io.netty.handler.codec.socks.SocksAuthResponse;
22  import io.netty.handler.codec.socks.SocksAuthScheme;
23  import io.netty.handler.codec.socks.SocksAuthStatus;
24  import io.netty.handler.codec.socks.SocksCmdRequest;
25  import io.netty.handler.codec.socks.SocksCmdRequestDecoder;
26  import io.netty.handler.codec.socks.SocksCmdType;
27  import io.netty.handler.codec.socks.SocksInitResponse;
28  import io.netty.handler.codec.socks.SocksRequest;
29  
30  
31  @ChannelHandler.Sharable
32  public final class SocksServerHandler extends SimpleChannelInboundHandler<SocksRequest> {
33  
34      @Override
35      public void channelRead0(ChannelHandlerContext ctx, SocksRequest socksRequest) throws Exception {
36          switch (socksRequest.requestType()) {
37              case INIT: {
38                  // auth support example
39                  //ctx.pipeline().addFirst(new SocksAuthRequestDecoder());
40                  //ctx.write(new SocksInitResponse(SocksAuthScheme.AUTH_PASSWORD));
41                  ctx.pipeline().addFirst(new SocksCmdRequestDecoder());
42                  ctx.write(new SocksInitResponse(SocksAuthScheme.NO_AUTH));
43                  break;
44              }
45              case AUTH:
46                  ctx.pipeline().addFirst(new SocksCmdRequestDecoder());
47                  ctx.write(new SocksAuthResponse(SocksAuthStatus.SUCCESS));
48                  break;
49              case CMD:
50                  SocksCmdRequest req = (SocksCmdRequest) socksRequest;
51                  if (req.cmdType() == SocksCmdType.CONNECT) {
52                      ctx.pipeline().addLast(new SocksServerConnectHandler());
53                      ctx.pipeline().remove(this);
54                      ctx.fireChannelRead(socksRequest);
55                  } else {
56                      ctx.close();
57                  }
58                  break;
59              case UNKNOWN:
60                  ctx.close();
61                  break;
62          }
63      }
64  
65      @Override
66      public void channelReadComplete(ChannelHandlerContext ctx) {
67          ctx.flush();
68      }
69  
70      @Override
71      public void exceptionCaught(ChannelHandlerContext ctx, Throwable throwable) {
72          throwable.printStackTrace();
73          SocksServerUtils.closeOnFlush(ctx.channel());
74      }
75  }