IdCustomTransparentProxy.pas 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. {
  2. $Project$
  3. $Workfile$
  4. $Revision$
  5. $DateUTC$
  6. $Id$
  7. This file is part of the Indy (Internet Direct) project, and is offered
  8. under the dual-licensing agreement described on the Indy website.
  9. (http://www.indyproject.org/)
  10. Copyright:
  11. (c) 1993-2005, Chad Z. Hower and the Indy Pit Crew. All rights reserved.
  12. }
  13. {
  14. $Log$
  15. }
  16. {
  17. Rev 1.10 11/12/2004 11:30:16 AM JPMugaas
  18. Expansions for IPv6.
  19. Rev 1.9 11/11/2004 10:25:22 PM JPMugaas
  20. Added OpenProxy and CloseProxy so you can do RecvFrom and SendTo functions
  21. from the UDP client with SOCKS. You must call OpenProxy before using
  22. RecvFrom or SendTo. When you are finished, you must use CloseProxy to close
  23. any connection to the Proxy. Connect and disconnect also call OpenProxy and
  24. CloseProxy.
  25. Rev 1.8 11/11/2004 3:42:52 AM JPMugaas
  26. Moved strings into RS. Socks will now raise an exception if you attempt to
  27. use SOCKS4 and SOCKS4A with UDP. Those protocol versions do not support UDP
  28. at all.
  29. Rev 1.7 11/9/2004 8:18:00 PM JPMugaas
  30. Attempt to add SOCKS support in UDP.
  31. Rev 1.6 6/6/2004 11:51:56 AM JPMugaas
  32. Fixed TODO with an exception
  33. Rev 1.5 2004.02.03 4:17:04 PM czhower
  34. For unit name changes.
  35. Rev 1.4 10/15/2003 10:59:06 PM DSiders
  36. Corrected spelling error in resource string name.
  37. Added resource string for circular links exception in transparent proxy.
  38. Rev 1.3 10/15/2003 10:10:18 PM DSiders
  39. Added localization comments.
  40. Rev 1.2 5/16/2003 9:22:38 AM BGooijen
  41. Added Listen(...)
  42. Rev 1.1 5/14/2003 6:41:00 PM BGooijen
  43. Added Bind(...)
  44. Rev 1.0 12/2/2002 05:01:26 PM JPMugaas
  45. Rechecked in due to file corruption.
  46. }
  47. unit IdCustomTransparentProxy;
  48. interface
  49. {$I IdCompilerDefines.inc}
  50. //we need to put this in Delphi mode to work
  51. uses
  52. Classes,
  53. IdComponent,
  54. IdException,
  55. IdGlobal,
  56. IdIOHandler,
  57. IdSocketHandle,
  58. IdBaseComponent;
  59. type
  60. EIdTransparentProxyCircularLink = class(EIdException);
  61. EIdTransparentProxyUDPNotSupported = class(EIdException);
  62. TIdCustomTransparentProxyClass = class of TIdCustomTransparentProxy;
  63. TIdCustomTransparentProxy = class(TIdComponent)
  64. protected
  65. FHost: String;
  66. FPassword: String;
  67. FPort: TIdPort;
  68. FIPVersion : TIdIPVersion;
  69. FUsername: String;
  70. {$IFDEF USE_OBJECT_ARC}[Weak]{$ENDIF} FChainedProxy: TIdCustomTransparentProxy;
  71. //
  72. procedure InitComponent; override;
  73. function GetEnabled: Boolean; virtual; abstract;
  74. procedure SetEnabled(AValue: Boolean); virtual;
  75. procedure MakeConnection(AIOHandler: TIdIOHandler; const AHost: string; const APort: TIdPort; const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION); virtual; abstract;
  76. {$IFNDEF USE_OBJECT_ARC}
  77. procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  78. {$ENDIF}
  79. procedure SetChainedProxy(const AValue: TIdCustomTransparentProxy);
  80. public
  81. procedure Assign(ASource: TPersistent); override;
  82. procedure OpenUDP(AHandle : TIdSocketHandle; const AHost: string = ''; const APort: TIdPort = 0; const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION); virtual;
  83. procedure CloseUDP(AHandle: TIdSocketHandle); virtual;
  84. function RecvFromUDP(AHandle: TIdSocketHandle; var ABuffer : TIdBytes;
  85. var VPeerIP: string; var VPeerPort: TIdPort; var VIPVersion: TIdIPVersion;
  86. AMSec: Integer = IdTimeoutDefault): Integer; virtual;
  87. procedure SendToUDP(AHandle: TIdSocketHandle;
  88. const AHost: string; const APort: TIdPort; const AIPVersion: TIdIPVersion;
  89. const ABuffer : TIdBytes); virtual;
  90. procedure Connect(AIOHandler: TIdIOHandler; const AHost: string; const APort: TIdPort; const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION);
  91. //
  92. procedure Bind(AIOHandler: TIdIOHandler; const AHost: string; const APort: TIdPort; const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION);overload;virtual;
  93. procedure Bind(AIOHandler: TIdIOHandler; const APort: TIdPort); overload;
  94. function Listen(AIOHandler: TIdIOHandler; const ATimeOut: Integer): Boolean; virtual;
  95. //
  96. property Enabled: Boolean read GetEnabled write SetEnabled;
  97. property Host: String read FHost write FHost;
  98. property Password: String read FPassword write FPassword;
  99. property Port: TIdPort read FPort write FPort;
  100. property IPVersion : TIdIPVersion read FIPVersion write FIPVersion default ID_DEFAULT_IP_VERSION;
  101. property Username: String read FUsername write FUsername;
  102. property ChainedProxy: TIdCustomTransparentProxy read FChainedProxy write SetChainedProxy;
  103. end;
  104. implementation
  105. uses
  106. IdResourceStringsCore, IdExceptionCore;
  107. { TIdCustomTransparentProxy }
  108. procedure TIdCustomTransparentProxy.InitComponent;
  109. begin
  110. inherited;
  111. FIPVersion := ID_DEFAULT_IP_VERSION;
  112. end;
  113. procedure TIdCustomTransparentProxy.Assign(ASource: TPersistent);
  114. var
  115. LSource: TIdCustomTransparentProxy;
  116. Begin
  117. if ASource is TIdCustomTransparentProxy then begin
  118. LSource := TIdCustomTransparentProxy(ASource);
  119. FHost := LSource.Host;
  120. FPassword := LSource.Password;
  121. FPort := LSource.Port;
  122. FIPVersion := LSource.IPVersion;
  123. FUsername := LSource.Username;
  124. end else begin
  125. inherited Assign(ASource);
  126. end;
  127. End;//
  128. procedure TIdCustomTransparentProxy.Connect(AIOHandler: TIdIOHandler; const AHost: string; const APort: TIdPort; const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION);
  129. var
  130. // under ARC, convert a weak reference to a strong reference before working with it
  131. LChainedProxy: TIdCustomTransparentProxy;
  132. begin
  133. LChainedProxy := FChainedProxy;
  134. if Assigned(LChainedProxy) and LChainedProxy.Enabled then begin
  135. MakeConnection(AIOHandler, LChainedProxy.Host, LChainedProxy.Port);
  136. LChainedProxy.Connect(AIOHandler, AHost, APort, AIPVersion);
  137. end else begin
  138. MakeConnection(AIOHandler, AHost, APort, AIPVersion);
  139. end;
  140. end;
  141. procedure RaiseProxyBindError;
  142. {$IFDEF USE_NORETURN}noreturn;{$ENDIF}
  143. begin
  144. raise EIdTransparentProxyCantBind.Create(RSTransparentProxyCannotBind);
  145. end;
  146. function TIdCustomTransparentProxy.Listen(AIOHandler: TIdIOHandler; const ATimeOut: integer):boolean;
  147. begin
  148. {$IFNDEF USE_NORETURN}
  149. Result := False; // keep the compiler happy
  150. {$ENDIF}
  151. RaiseProxyBindError;
  152. end;
  153. procedure TIdCustomTransparentProxy.Bind(AIOHandler: TIdIOHandler; const AHost: string; const APort: TIdPort; const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION);
  154. begin
  155. RaiseProxyBindError;
  156. end;
  157. procedure TIdCustomTransparentProxy.Bind(AIOHandler: TIdIOHandler; const APort: TIdPort);
  158. begin
  159. Bind(AIOHandler, '0.0.0.0', APort); {do not localize}
  160. end;
  161. procedure TIdCustomTransparentProxy.SetEnabled(AValue: Boolean);
  162. Begin
  163. End;
  164. // under ARC, all weak references to a freed object get nil'ed automatically
  165. {$IFNDEF USE_OBJECT_ARC}
  166. procedure TIdCustomTransparentProxy.Notification(AComponent: TComponent; Operation: TOperation);
  167. begin
  168. if (Operation = opRemove) and (AComponent = FChainedProxy) then begin
  169. FChainedProxy := nil;
  170. end;
  171. inherited Notification(AComponent,Operation);
  172. end;
  173. {$ENDIF}
  174. procedure TIdCustomTransparentProxy.SetChainedProxy(const AValue: TIdCustomTransparentProxy);
  175. var
  176. LNextValue: TIdCustomTransparentProxy;
  177. // under ARC, convert a weak reference to a strong reference before working with it
  178. LChainedProxy: TIdCustomTransparentProxy;
  179. begin
  180. LChainedProxy := FChainedProxy;
  181. if LChainedProxy <> AValue then
  182. begin
  183. LNextValue := AValue;
  184. while Assigned(LNextValue) do begin
  185. if LNextValue = Self then begin
  186. raise EIdTransparentProxyCircularLink.CreateFmt(RSInterceptCircularLink, [ClassName]);// -> One EIDCircularLink exception
  187. end;
  188. LNextValue := LNextValue.ChainedProxy;
  189. end;
  190. // under ARC, all weak references to a freed object get nil'ed automatically
  191. {$IFNDEF USE_OBJECT_ARC}
  192. if Assigned(LChainedProxy) then begin
  193. LChainedProxy.RemoveFreeNotification(Self);
  194. end;
  195. {$ENDIF}
  196. FChainedProxy := AValue;
  197. {$IFNDEF USE_OBJECT_ARC}
  198. if Assigned(AValue) then begin
  199. AValue.FreeNotification(Self);
  200. end;
  201. {$ENDIF}
  202. end;
  203. end;
  204. procedure RaiseUDPNotSupportedError;
  205. {$IFDEF USE_NORETURN}noreturn;{$ENDIF}
  206. begin
  207. raise EIdTransparentProxyUDPNotSupported.Create(RSTransparentProxyCanNotSupportUDP);
  208. end;
  209. procedure TIdCustomTransparentProxy.CloseUDP(AHandle: TIdSocketHandle);
  210. begin
  211. RaiseUDPNotSupportedError;
  212. end;
  213. procedure TIdCustomTransparentProxy.OpenUDP(AHandle: TIdSocketHandle;
  214. const AHost: string = ''; const APort: TIdPort = 0;
  215. const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION);
  216. begin
  217. RaiseUDPNotSupportedError;;
  218. end;
  219. function TIdCustomTransparentProxy.RecvFromUDP(AHandle: TIdSocketHandle;
  220. var ABuffer : TIdBytes; var VPeerIP: string; var VPeerPort: TIdPort;
  221. var VIPVersion: TIdIPVersion; AMSec: Integer = IdTimeoutDefault): Integer;
  222. begin
  223. {$IFNDEF USE_NORETURN}
  224. Result := 0; // keep the compiler happy
  225. {$ENDIF}
  226. RaiseUDPNotSupportedError;
  227. end;
  228. procedure TIdCustomTransparentProxy.SendToUDP(AHandle: TIdSocketHandle;
  229. const AHost: string; const APort: TIdPort; const AIPVersion: TIdIPVersion;
  230. const ABuffer : TIdBytes);
  231. begin
  232. RaiseUDPNotSupportedError;
  233. end;
  234. end.