View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.proxy.handlers.socks;
21  
22  import java.net.InetSocketAddress;
23  
24  import org.apache.mina.proxy.handlers.ProxyRequest;
25  
26  /**
27   * SocksProxyRequest.java - Wrapper class for SOCKS requests.
28   * 
29   * @author The Apache MINA Project (dev@mina.apache.org)
30   * @version $Rev: 738311 $, $Date: 2009-01-28 01:07:01 +0100 (Wed, 28 Jan 2009) $
31   * @since MINA 2.0.0-M3
32   */
33  public class SocksProxyRequest extends ProxyRequest {
34  
35      /**
36       * The SOCKS protocol version.
37       */
38      private byte protocolVersion;
39  
40      /**
41       * The command code.
42       */
43      private byte commandCode;
44  
45      /**
46       * The user name used when authenticating to the proxy server. 
47       */
48      private String userName;
49  
50      /**
51       * The user's password used when authenticating to the proxy server.
52       */
53      private String password;
54  
55      /**
56       * The SOCKS server host name.
57       */
58      private String host;
59  
60      /**
61       * The SOCKS server port.
62       */
63      private int port;
64  
65      /**
66       * The Kerberos service name used in GSSAPI authentication mode.
67       */
68      private String serviceKerberosName;
69  
70      /**
71       * Constructor used when building a SOCKS4 request.
72       * 
73       * @param protocolVersion the protocol version
74       * @param commandCode the command code
75       * @param endpointAddress the endpoint address
76       * @param userName the user name
77       */
78      public SocksProxyRequest(byte protocolVersion, byte commandCode,
79              InetSocketAddress endpointAddress, String userName) {
80          super(endpointAddress);
81          this.protocolVersion = protocolVersion;
82          this.commandCode = commandCode;
83          this.userName = userName;
84      }
85  
86      /**
87       * Constructor used when building a SOCKS4a request.
88       * 
89       * @param commandCode the command code
90       * @param host the server host name
91       * @param port the server port
92       * @param userName the user name
93       */
94      public SocksProxyRequest(byte commandCode, String host, int port,
95              String userName) {
96          this.protocolVersion = SocksProxyConstants.SOCKS_VERSION_4;
97          this.commandCode = commandCode;
98          this.userName = userName;
99          this.host = host;
100         this.port = port;
101     }
102 
103     /**
104      * Return the endpoint address resulting from the {@link #getEndpointAddress()}. 
105      * If not set, it will return the {@link SocksProxyConstants#FAKE_IP} constant 
106      * value which will be ignored in a SOCKS v4 request.
107      *   
108      * @return the endpoint address
109      */
110     public byte[] getIpAddress() {
111         if (getEndpointAddress() == null)
112             return SocksProxyConstants.FAKE_IP;
113         else
114             return getEndpointAddress().getAddress().getAddress();
115     }
116 
117     /**
118      * Return the server port as a byte array.
119      * 
120      * @return the server port
121      */
122     public byte[] getPort() {
123         byte[] port = new byte[2];
124         int p = (int) (getEndpointAddress() == null ? this.port
125                 : getEndpointAddress().getPort());
126         port[1] = (byte) p;
127         port[0] = (byte) (p >> 8);
128         return port;
129     }
130 
131     /**
132      * Return the command code.
133      * 
134      * @return the command code
135      */
136     public byte getCommandCode() {
137         return commandCode;
138     }
139 
140     /**
141      * Return the protocol version.
142      * 
143      * @return the protocol version
144      */
145     public byte getProtocolVersion() {
146         return protocolVersion;
147     }
148 
149     /**
150      * Return the user name.
151      * 
152      * @return the user name
153      */
154     public String getUserName() {
155         return userName;
156     }
157 
158     /**
159      * Return the server host name.
160      * 
161      * @return the server host name
162      */
163     public synchronized final String getHost() {
164         if (host == null) {
165             if (getEndpointAddress() != null) {
166                 host = getEndpointAddress().getHostName();
167             }
168         }
169 
170         return host;
171     }
172 
173     /**
174      * Return the user password.
175      * 
176      * @return the user password
177      */
178     public String getPassword() {
179         return password;
180     }
181 
182     /**
183      * Set the user password
184      * 
185      * @param password the user password value
186      */
187     public void setPassword(String password) {
188         this.password = password;
189     }
190 
191     /**
192      * Return the Kerberos service name.
193      * 
194      * @return the Kerberos service name
195      */
196     public String getServiceKerberosName() {
197         return serviceKerberosName;
198     }
199 
200     /**
201      * Set the Kerberos service name.
202      * 
203      * @param serviceKerberosName the Kerberos service name
204      */
205     public void setServiceKerberosName(String serviceKerberosName) {
206         this.serviceKerberosName = serviceKerberosName;
207     }
208 }