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 * Returns 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 * @return the endpoint address
106 */
107 public byte[] getIpAddress() {
108 if (getEndpointAddress() == null) {
109 return SocksProxyConstants.FAKE_IP;
110 }
111
112 return getEndpointAddress().getAddress().getAddress();
113 }
114
115 /**
116 * Return the server port as a byte array.
117 *
118 * @return the server port
119 */
120 public byte[] getPort() {
121 byte[] port = new byte[2];
122 int p = (getEndpointAddress() == null ? this.port : getEndpointAddress().getPort());
123 port[1] = (byte) p;
124 port[0] = (byte) (p >> 8);
125 return port;
126 }
127
128 /**
129 * Return the command code.
130 *
131 * @return the command code
132 */
133 public byte getCommandCode() {
134 return commandCode;
135 }
136
137 /**
138 * Return the protocol version.
139 *
140 * @return the protocol version
141 */
142 public byte getProtocolVersion() {
143 return protocolVersion;
144 }
145
146 /**
147 * Return the user name.
148 *
149 * @return the user name
150 */
151 public String getUserName() {
152 return userName;
153 }
154
155 /**
156 * Return the server host name.
157 *
158 * @return the server host name
159 */
160 public synchronized final String getHost() {
161 if (host == null) {
162 InetSocketAddress adr = getEndpointAddress();
163
164 if (adr != null && !adr.isUnresolved()) {
165 host = getEndpointAddress().getHostName();
166 }
167 }
168
169 return host;
170 }
171
172 /**
173 * Return the user password.
174 *
175 * @return the user password
176 */
177 public String getPassword() {
178 return password;
179 }
180
181 /**
182 * Set the user password
183 *
184 * @param password the user password value
185 */
186 public void setPassword(String password) {
187 this.password = password;
188 }
189
190 /**
191 * Return the Kerberos service name.
192 *
193 * @return the Kerberos service name
194 */
195 public String getServiceKerberosName() {
196 return serviceKerberosName;
197 }
198
199 /**
200 * Set the Kerberos service name.
201 *
202 * @param serviceKerberosName the Kerberos service name
203 */
204 public void setServiceKerberosName(String serviceKerberosName) {
205 this.serviceKerberosName = serviceKerberosName;
206 }
207 }