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 <a href="http://mina.apache.org">Apache MINA Project</a>
30   * @since MINA 2.0.0-M3
31   */
32  public class SocksProxyRequest extends ProxyRequest {
33  
34      /**
35       * The SOCKS protocol version.
36       */
37      private byte protocolVersion;
38  
39      /**
40       * The command code.
41       */
42      private byte commandCode;
43  
44      /**
45       * The user name used when authenticating to the proxy server. 
46       */
47      private String userName;
48  
49      /**
50       * The user's password used when authenticating to the proxy server.
51       */
52      private String password;
53  
54      /**
55       * The SOCKS server host name.
56       */
57      private String host;
58  
59      /**
60       * The SOCKS server port.
61       */
62      private int port;
63  
64      /**
65       * The Kerberos service name used in GSSAPI authentication mode.
66       */
67      private String serviceKerberosName;
68  
69      /**
70       * Constructor used when building a SOCKS4 request.
71       * 
72       * @param protocolVersion the protocol version
73       * @param commandCode the command code
74       * @param endpointAddress the endpoint address
75       * @param userName the user name
76       */
77      public SocksProxyRequest(byte protocolVersion, byte commandCode, InetSocketAddress endpointAddress, String userName) {
78          super(endpointAddress);
79          this.protocolVersion = protocolVersion;
80          this.commandCode = commandCode;
81          this.userName = userName;
82      }
83  
84      /**
85       * Constructor used when building a SOCKS4a request.
86       * 
87       * @param commandCode the command code
88       * @param host the server host name
89       * @param port the server port
90       * @param userName the user name
91       */
92      public SocksProxyRequest(byte commandCode, String host, int port, String userName) {
93          this.protocolVersion = SocksProxyConstants.SOCKS_VERSION_4;
94          this.commandCode = commandCode;
95          this.userName = userName;
96          this.host = host;
97          this.port = port;
98      }
99  
100     /**
101      * @return the endpoint address resulting from the {@link #getEndpointAddress()}. 
102      * If not set, it will return the {@link SocksProxyConstants#FAKE_IP} constant 
103      * value which will be ignored in a SOCKS v4 request.
104      */
105     public byte[] getIpAddress() {
106         if (getEndpointAddress() == null) {
107             return SocksProxyConstants.FAKE_IP;
108         }
109 
110         return getEndpointAddress().getAddress().getAddress();
111     }
112 
113     /**
114      * Return the server port as a byte array.
115      * 
116      * @return the server port
117      */
118     public byte[] getPort() {
119         byte[] port = new byte[2];
120         int p = (getEndpointAddress() == null ? this.port : getEndpointAddress().getPort());
121         port[1] = (byte) p;
122         port[0] = (byte) (p >> 8);
123         return port;
124     }
125 
126     /**
127      * Return the command code.
128      * 
129      * @return the command code
130      */
131     public byte getCommandCode() {
132         return commandCode;
133     }
134 
135     /**
136      * Return the protocol version.
137      * 
138      * @return the protocol version
139      */
140     public byte getProtocolVersion() {
141         return protocolVersion;
142     }
143 
144     /**
145      * Return the user name.
146      * 
147      * @return the user name
148      */
149     public String getUserName() {
150         return userName;
151     }
152 
153     /**
154      * Return the server host name.
155      * 
156      * @return the server host name
157      */
158     public synchronized final String getHost() {
159         if (host == null) {
160             InetSocketAddress adr = getEndpointAddress();
161 
162             if (adr != null && adr.isUnresolved()) {
163                 host = getEndpointAddress().getHostName();
164             }
165         }
166 
167         return host;
168     }
169 
170     /**
171      * Return the user password.
172      * 
173      * @return the user password
174      */
175     public String getPassword() {
176         return password;
177     }
178 
179     /**
180      * Set the user password
181      * 
182      * @param password the user password value
183      */
184     public void setPassword(String password) {
185         this.password = password;
186     }
187 
188     /**
189      * Return the Kerberos service name.
190      * 
191      * @return the Kerberos service name
192      */
193     public String getServiceKerberosName() {
194         return serviceKerberosName;
195     }
196 
197     /**
198      * Set the Kerberos service name.
199      * 
200      * @param serviceKerberosName the Kerberos service name
201      */
202     public void setServiceKerberosName(String serviceKerberosName) {
203         this.serviceKerberosName = serviceKerberosName;
204     }
205 }