/eclipse_projects/PubkeyGenerator/src/net/sourceforge/jsocks/Proxy.java

https://gitlab.com/imxieke/remote-desktop-clients · Java · 426 lines · 254 code · 56 blank · 116 comment · 16 complexity · d06f38c3552600703b50580af3119b94 MD5 · raw file

  1. package net.sourceforge.jsocks;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.InterruptedIOException;
  5. import java.io.OutputStream;
  6. import java.net.InetAddress;
  7. import java.net.Socket;
  8. import java.net.UnknownHostException;
  9. /**
  10. * Abstract class Proxy, base for classes Socks4Proxy and Socks5Proxy. Defines
  11. * methods for specifying default proxy, to be used by all classes of this
  12. * package.
  13. */
  14. public abstract class Proxy {
  15. // Data members
  16. // protected InetRange directHosts = new InetRange();
  17. public static final int SOCKS_SUCCESS = 0;
  18. public static final int SOCKS_FAILURE = 1;
  19. public static final int SOCKS_BADCONNECT = 2;
  20. public static final int SOCKS_BADNETWORK = 3;
  21. public static final int SOCKS_HOST_UNREACHABLE = 4;
  22. public static final int SOCKS_CONNECTION_REFUSED = 5;
  23. public static final int SOCKS_TTL_EXPIRE = 6;
  24. public static final int SOCKS_CMD_NOT_SUPPORTED = 7;
  25. public static final int SOCKS_ADDR_NOT_SUPPORTED = 8;
  26. public static final int SOCKS_NO_PROXY = 1 << 16;
  27. public static final int SOCKS_PROXY_NO_CONNECT = 2 << 16;
  28. public static final int SOCKS_PROXY_IO_ERROR = 3 << 16;
  29. public static final int SOCKS_AUTH_NOT_SUPPORTED = 4 << 16;
  30. // Public instance methods
  31. // ========================
  32. public static final int SOCKS_AUTH_FAILURE = 5 << 16;
  33. public static final int SOCKS_JUST_ERROR = 6 << 16;
  34. public static final int SOCKS_DIRECT_FAILED = 7 << 16;
  35. // Public Static(Class) Methods
  36. // ==============================
  37. public static final int SOCKS_METHOD_NOTSUPPORTED = 8 << 16;
  38. public static final int SOCKS_CMD_CONNECT = 0x1;
  39. static final int SOCKS_CMD_BIND = 0x2;
  40. static final int SOCKS_CMD_UDP_ASSOCIATE = 0x3;
  41. /**
  42. * Get current default proxy.
  43. *
  44. * @return Current default proxy, or null if none is set.
  45. */
  46. public static Proxy getDefaultProxy() {
  47. return defaultProxy;
  48. }
  49. /**
  50. * Parses strings in the form: host[:port:user:password], and creates proxy
  51. * from information obtained from parsing.
  52. * <p>
  53. * Defaults: port = 1080.<br>
  54. * If user specified but not password, creates Socks4Proxy, if user not
  55. * specified creates Socks5Proxy, if both user and password are speciefied
  56. * creates Socks5Proxy with user/password authentication.
  57. *
  58. * @param proxy_entry
  59. * String in the form host[:port:user:password]
  60. * @return Proxy created from the string, null if entry was somehow
  61. * invalid(host unknown for example, or empty string)
  62. */
  63. public static Proxy parseProxy(String proxy_entry) {
  64. String proxy_host;
  65. int proxy_port = 1080;
  66. String proxy_user = null;
  67. String proxy_password = null;
  68. Proxy proxy;
  69. java.util.StringTokenizer st = new java.util.StringTokenizer(
  70. proxy_entry, ":");
  71. if (st.countTokens() < 1)
  72. return null;
  73. proxy_host = st.nextToken();
  74. if (st.hasMoreTokens())
  75. try {
  76. proxy_port = Integer.parseInt(st.nextToken().trim());
  77. } catch (NumberFormatException nfe) {
  78. }
  79. if (st.hasMoreTokens())
  80. proxy_user = st.nextToken();
  81. if (st.hasMoreTokens())
  82. proxy_password = st.nextToken();
  83. try {
  84. if (proxy_user == null)
  85. proxy = new Socks5Proxy(proxy_host, proxy_port);
  86. else if (proxy_password == null)
  87. proxy = new Socks4Proxy(proxy_host, proxy_port, proxy_user);
  88. else {
  89. proxy = new Socks5Proxy(proxy_host, proxy_port);
  90. /*
  91. * UserPasswordAuthentication upa = new
  92. * UserPasswordAuthentication( proxy_user, proxy_password);
  93. *
  94. * ((Socks5Proxy)proxy).setAuthenticationMethod(upa.METHOD_ID,upa
  95. * );
  96. */
  97. }
  98. } catch (UnknownHostException uhe) {
  99. return null;
  100. }
  101. return proxy;
  102. }
  103. /**
  104. * Sets SOCKS5 proxy as default. Default proxy only supports
  105. * no-authentication.
  106. *
  107. * @param ipAddress
  108. * Host address on which SOCKS5 server is running.
  109. * @param port
  110. * Port on which SOCKS5 server is running.
  111. */
  112. public static void setDefaultProxy(InetAddress ipAddress, int port) {
  113. defaultProxy = new Socks5Proxy(ipAddress, port);
  114. }
  115. // Protected Methods
  116. // =================
  117. /**
  118. * Sets SOCKS4 proxy as default.
  119. *
  120. * @param ipAddress
  121. * Host address on which SOCKS4 server is running.
  122. * @param port
  123. * Port on which SOCKS4 server is running.
  124. * @param user
  125. * Username to use for communications with proxy.
  126. */
  127. public static void setDefaultProxy(InetAddress ipAddress, int port,
  128. String user) {
  129. defaultProxy = new Socks4Proxy(ipAddress, port, user);
  130. }
  131. /**
  132. * Sets default proxy.
  133. *
  134. * @param p
  135. * Proxy to use as default proxy.
  136. */
  137. public static void setDefaultProxy(Proxy p) {
  138. defaultProxy = p;
  139. }
  140. /**
  141. * Sets SOCKS5 proxy as default. Default proxy only supports
  142. * no-authentication.
  143. *
  144. * @param hostName
  145. * Host name on which SOCKS5 server is running.
  146. * @param port
  147. * Port on which SOCKS5 server is running.
  148. */
  149. public static void setDefaultProxy(String hostName, int port)
  150. throws UnknownHostException {
  151. defaultProxy = new Socks5Proxy(hostName, port);
  152. }
  153. /**
  154. * Sets SOCKS4 proxy as default.
  155. *
  156. * @param hostName
  157. * Host name on which SOCKS4 server is running.
  158. * @param port
  159. * Port on which SOCKS4 server is running.
  160. * @param user
  161. * Username to use for communications with proxy.
  162. */
  163. public static void setDefaultProxy(String hostName, int port, String user)
  164. throws UnknownHostException {
  165. defaultProxy = new Socks4Proxy(hostName, port, user);
  166. }
  167. protected InetAddress proxyIP = null;
  168. protected String proxyHost = null;
  169. protected int proxyPort;
  170. protected Socket proxySocket = null;
  171. protected InputStream in;
  172. protected OutputStream out;
  173. protected int version;
  174. protected Proxy chainProxy = null;
  175. // Protected static/class variables
  176. protected static Proxy defaultProxy = null;
  177. Proxy(InetAddress proxyIP, int proxyPort) {
  178. this.proxyIP = proxyIP;
  179. this.proxyPort = proxyPort;
  180. }
  181. Proxy(Proxy p) {
  182. this.proxyIP = p.proxyIP;
  183. this.proxyPort = p.proxyPort;
  184. this.version = p.version;
  185. }
  186. Proxy(Proxy chainProxy, InetAddress proxyIP, int proxyPort) {
  187. this.chainProxy = chainProxy;
  188. this.proxyIP = proxyIP;
  189. this.proxyPort = proxyPort;
  190. }
  191. // Private methods
  192. // ===============
  193. // Constants
  194. // Constructors
  195. // ====================
  196. Proxy(String proxyHost, int proxyPort) throws UnknownHostException {
  197. this.proxyHost = proxyHost;
  198. this.proxyIP = InetAddress.getByName(proxyHost);
  199. this.proxyPort = proxyPort;
  200. }
  201. protected ProxyMessage accept() throws IOException, SocksException {
  202. ProxyMessage msg;
  203. try {
  204. msg = formMessage(in);
  205. } catch (InterruptedIOException iioe) {
  206. throw iioe;
  207. } catch (IOException io_ex) {
  208. endSession();
  209. throw new SocksException(SOCKS_PROXY_IO_ERROR,
  210. "While Trying accept:" + io_ex);
  211. }
  212. return msg;
  213. }
  214. protected ProxyMessage bind(InetAddress ip, int port) throws SocksException {
  215. try {
  216. startSession();
  217. ProxyMessage request = formMessage(SOCKS_CMD_BIND, ip, port);
  218. return exchange(request);
  219. } catch (SocksException se) {
  220. endSession();
  221. throw se;
  222. }
  223. }
  224. protected ProxyMessage bind(String host, int port)
  225. throws UnknownHostException, SocksException {
  226. try {
  227. startSession();
  228. ProxyMessage request = formMessage(SOCKS_CMD_BIND, host, port);
  229. return exchange(request);
  230. } catch (SocksException se) {
  231. endSession();
  232. throw se;
  233. }
  234. }
  235. protected ProxyMessage connect(InetAddress ip, int port)
  236. throws SocksException {
  237. try {
  238. startSession();
  239. ProxyMessage request = formMessage(SOCKS_CMD_CONNECT, ip, port);
  240. return exchange(request);
  241. } catch (SocksException se) {
  242. endSession();
  243. throw se;
  244. }
  245. }
  246. protected ProxyMessage connect(String host, int port)
  247. throws UnknownHostException, SocksException {
  248. try {
  249. startSession();
  250. ProxyMessage request = formMessage(SOCKS_CMD_CONNECT, host, port);
  251. return exchange(request);
  252. } catch (SocksException se) {
  253. endSession();
  254. throw se;
  255. }
  256. }
  257. protected abstract Proxy copy();
  258. protected void endSession() {
  259. try {
  260. if (proxySocket != null)
  261. proxySocket.close();
  262. proxySocket = null;
  263. } catch (IOException io_ex) {
  264. }
  265. }
  266. /**
  267. * Sends the request reads reply and returns it throws exception if
  268. * something wrong with IO or the reply code is not zero
  269. */
  270. protected ProxyMessage exchange(ProxyMessage request) throws SocksException {
  271. ProxyMessage reply;
  272. try {
  273. request.write(out);
  274. reply = formMessage(in);
  275. } catch (SocksException s_ex) {
  276. throw s_ex;
  277. } catch (IOException ioe) {
  278. throw (new SocksException(SOCKS_PROXY_IO_ERROR, "" + ioe));
  279. }
  280. return reply;
  281. }
  282. protected abstract ProxyMessage formMessage(InputStream in)
  283. throws SocksException, IOException;
  284. protected abstract ProxyMessage formMessage(int cmd, InetAddress ip,
  285. int port);
  286. protected abstract ProxyMessage formMessage(int cmd, String host, int port)
  287. throws UnknownHostException;
  288. /**
  289. * Get the ip address of the proxy server host.
  290. *
  291. * @return Proxy InetAddress.
  292. */
  293. public InetAddress getInetAddress() {
  294. return proxyIP;
  295. }
  296. /**
  297. * Get the port on which proxy server is running.
  298. *
  299. * @return Proxy port.
  300. */
  301. public int getPort() {
  302. return proxyPort;
  303. }
  304. /**
  305. * Reads the reply from the SOCKS server
  306. */
  307. protected ProxyMessage readMsg() throws SocksException, IOException {
  308. return formMessage(in);
  309. }
  310. /**
  311. * Sends the request to SOCKS server
  312. */
  313. protected void sendMsg(ProxyMessage msg) throws SocksException, IOException {
  314. msg.write(out);
  315. }
  316. protected void startSession() throws SocksException {
  317. try {
  318. proxySocket = new Socket(proxyIP, proxyPort);
  319. in = proxySocket.getInputStream();
  320. out = proxySocket.getOutputStream();
  321. } catch (SocksException se) {
  322. throw se;
  323. } catch (IOException io_ex) {
  324. throw new SocksException(SOCKS_PROXY_IO_ERROR, "" + io_ex);
  325. }
  326. }
  327. /**
  328. * Get string representation of this proxy.
  329. *
  330. * @returns string in the form:proxyHost:proxyPort \t Version versionNumber
  331. */
  332. @Override
  333. public String toString() {
  334. return ("" + proxyIP.getHostName() + ":" + proxyPort + "\tVersion " + version);
  335. }
  336. protected ProxyMessage udpAssociate(InetAddress ip, int port)
  337. throws SocksException {
  338. try {
  339. startSession();
  340. ProxyMessage request = formMessage(SOCKS_CMD_UDP_ASSOCIATE, ip,
  341. port);
  342. if (request != null)
  343. return exchange(request);
  344. } catch (SocksException se) {
  345. endSession();
  346. throw se;
  347. }
  348. // Only get here if request was null
  349. endSession();
  350. throw new SocksException(SOCKS_METHOD_NOTSUPPORTED,
  351. "This version of proxy does not support UDP associate, use version 5");
  352. }
  353. protected ProxyMessage udpAssociate(String host, int port)
  354. throws UnknownHostException, SocksException {
  355. try {
  356. startSession();
  357. ProxyMessage request = formMessage(SOCKS_CMD_UDP_ASSOCIATE, host,
  358. port);
  359. if (request != null)
  360. return exchange(request);
  361. } catch (SocksException se) {
  362. endSession();
  363. throw se;
  364. }
  365. // Only get here if request was null
  366. endSession();
  367. throw new SocksException(SOCKS_METHOD_NOTSUPPORTED,
  368. "This version of proxy does not support UDP associate, use version 5");
  369. }
  370. }