PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

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