IdCustomTransparentProxy.pas 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. {$IF DEFINED(HAS_UNSAFE_OBJECTREF)}[Unsafe]
  71. {$ELSEIF DEFINED(HAS_WEAK_OBJECT_REF)}[Weak]
  72. {$IFEND} FChainedProxy: TIdCustomTransparentProxy;
  73. //
  74. function GetEnabled: Boolean; virtual; abstract;
  75. procedure SetEnabled(AValue: Boolean); virtual;
  76. procedure MakeConnection(AIOHandler: TIdIOHandler; const AHost: string; const APort: TIdPort; const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION); virtual; abstract;
  77. {$IFDEF USE_OBJECT_REF_FREENOTIF}
  78. procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  79. {$ENDIF}
  80. procedure SetChainedProxy(const AValue: TIdCustomTransparentProxy);
  81. public
  82. constructor Create(AOwner: TComponent); override;
  83. procedure Assign(ASource: TPersistent); override;
  84. procedure OpenUDP(AHandle : TIdSocketHandle; const AHost: string = ''; const APort: TIdPort = 0; const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION); virtual;
  85. procedure CloseUDP(AHandle: TIdSocketHandle); virtual;
  86. function RecvFromUDP(AHandle: TIdSocketHandle; var ABuffer : TIdBytes;
  87. var VPeerIP: string; var VPeerPort: TIdPort; var VIPVersion: TIdIPVersion;
  88. AMSec: Integer = IdTimeoutDefault): Integer; virtual;
  89. procedure SendToUDP(AHandle: TIdSocketHandle;
  90. const AHost: string; const APort: TIdPort; const AIPVersion: TIdIPVersion;
  91. const ABuffer : TIdBytes); virtual;
  92. procedure Connect(AIOHandler: TIdIOHandler; const AHost: string; const APort: TIdPort; const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION);
  93. //
  94. procedure Bind(AIOHandler: TIdIOHandler; const AHost: string; const APort: TIdPort; const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION);overload;virtual;
  95. procedure Bind(AIOHandler: TIdIOHandler; const APort: TIdPort); overload;
  96. function Listen(AIOHandler: TIdIOHandler; const ATimeOut: Integer): Boolean; virtual;
  97. //
  98. property Enabled: Boolean read GetEnabled write SetEnabled;
  99. property Host: String read FHost write FHost;
  100. property Password: String read FPassword write FPassword;
  101. property Port: TIdPort read FPort write FPort;
  102. property IPVersion : TIdIPVersion read FIPVersion write FIPVersion default ID_DEFAULT_IP_VERSION;
  103. property Username: String read FUsername write FUsername;
  104. property ChainedProxy: TIdCustomTransparentProxy read FChainedProxy write SetChainedProxy;
  105. end;
  106. implementation
  107. uses
  108. IdResourceStringsCore, IdExceptionCore;
  109. { TIdCustomTransparentProxy }
  110. constructor TIdCustomTransparentProxy.Create(AOwner: TComponent);
  111. begin
  112. inherited Create(AOwner);
  113. FIPVersion := ID_DEFAULT_IP_VERSION;
  114. end;
  115. procedure TIdCustomTransparentProxy.Assign(ASource: TPersistent);
  116. var
  117. LSource: TIdCustomTransparentProxy;
  118. Begin
  119. if ASource is TIdCustomTransparentProxy then begin
  120. LSource := TIdCustomTransparentProxy(ASource);
  121. FHost := LSource.Host;
  122. FPassword := LSource.Password;
  123. FPort := LSource.Port;
  124. FIPVersion := LSource.IPVersion;
  125. FUsername := LSource.Username;
  126. end else begin
  127. inherited Assign(ASource);
  128. end;
  129. End;//
  130. procedure TIdCustomTransparentProxy.Connect(AIOHandler: TIdIOHandler; const AHost: string; const APort: TIdPort; const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION);
  131. var
  132. // under ARC, convert a weak reference to a strong reference before working with it
  133. LChainedProxy: TIdCustomTransparentProxy;
  134. begin
  135. LChainedProxy := FChainedProxy;
  136. if Assigned(LChainedProxy) and LChainedProxy.Enabled then begin
  137. MakeConnection(AIOHandler, LChainedProxy.Host, LChainedProxy.Port);
  138. LChainedProxy.Connect(AIOHandler, AHost, APort, AIPVersion);
  139. end else begin
  140. MakeConnection(AIOHandler, AHost, APort, AIPVersion);
  141. end;
  142. end;
  143. function TIdCustomTransparentProxy.Listen(AIOHandler: TIdIOHandler; const ATimeOut: integer):boolean;
  144. begin
  145. raise EIdTransparentProxyCantBind.Create(RSTransparentProxyCannotBind);
  146. end;
  147. procedure TIdCustomTransparentProxy.Bind(AIOHandler: TIdIOHandler; const AHost: string; const APort: TIdPort; const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION);
  148. begin
  149. raise EIdTransparentProxyCantBind.Create(RSTransparentProxyCannotBind);
  150. end;
  151. procedure TIdCustomTransparentProxy.Bind(AIOHandler: TIdIOHandler; const APort: TIdPort);
  152. begin
  153. Bind(AIOHandler, '0.0.0.0', APort); {do not localize}
  154. end;
  155. procedure TIdCustomTransparentProxy.SetEnabled(AValue: Boolean);
  156. Begin
  157. End;
  158. // under ARC, all weak references to a freed object get nil'ed automatically
  159. {$IFDEF USE_OBJECT_REF_FREENOTIF}
  160. procedure TIdCustomTransparentProxy.Notification(AComponent: TComponent; Operation: TOperation);
  161. begin
  162. if (Operation = opRemove) and (AComponent = FChainedProxy) then begin
  163. FChainedProxy := nil;
  164. end;
  165. inherited Notification(AComponent,Operation);
  166. end;
  167. {$ENDIF}
  168. procedure TIdCustomTransparentProxy.SetChainedProxy(const AValue: TIdCustomTransparentProxy);
  169. var
  170. // under ARC, convert weak references to strong references before working with them
  171. LNextValue: TIdCustomTransparentProxy;
  172. LChainedProxy: TIdCustomTransparentProxy;
  173. begin
  174. LChainedProxy := FChainedProxy;
  175. if LChainedProxy <> AValue then
  176. begin
  177. LNextValue := AValue;
  178. while Assigned(LNextValue) do begin
  179. if LNextValue = Self then begin
  180. raise EIdTransparentProxyCircularLink.CreateFmt(RSInterceptCircularLink, [ClassName]);// -> One EIDCircularLink exception
  181. end;
  182. LNextValue := LNextValue.ChainedProxy;
  183. end;
  184. // under ARC, all weak references to a freed object get nil'ed automatically
  185. {$IFDEF USE_OBJECT_REF_FREENOTIF}
  186. if Assigned(LChainedProxy) then begin
  187. LChainedProxy.RemoveFreeNotification(Self);
  188. end;
  189. {$ENDIF}
  190. FChainedProxy := AValue;
  191. {$IFDEF USE_OBJECT_REF_FREENOTIF}
  192. if Assigned(AValue) then begin
  193. AValue.FreeNotification(Self);
  194. end;
  195. {$ENDIF}
  196. end;
  197. end;
  198. procedure TIdCustomTransparentProxy.CloseUDP(AHandle: TIdSocketHandle);
  199. begin
  200. raise EIdTransparentProxyUDPNotSupported.Create(RSTransparentProxyCanNotSupportUDP);
  201. end;
  202. procedure TIdCustomTransparentProxy.OpenUDP(AHandle: TIdSocketHandle;
  203. const AHost: string = ''; const APort: TIdPort = 0;
  204. const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION);
  205. begin
  206. raise EIdTransparentProxyUDPNotSupported.Create(RSTransparentProxyCanNotSupportUDP);
  207. end;
  208. function TIdCustomTransparentProxy.RecvFromUDP(AHandle: TIdSocketHandle;
  209. var ABuffer : TIdBytes; var VPeerIP: string; var VPeerPort: TIdPort;
  210. var VIPVersion: TIdIPVersion; AMSec: Integer = IdTimeoutDefault): Integer;
  211. begin
  212. raise EIdTransparentProxyUDPNotSupported.Create(RSTransparentProxyCanNotSupportUDP);
  213. end;
  214. procedure TIdCustomTransparentProxy.SendToUDP(AHandle: TIdSocketHandle;
  215. const AHost: string; const APort: TIdPort; const AIPVersion: TIdIPVersion;
  216. const ABuffer : TIdBytes);
  217. begin
  218. raise EIdTransparentProxyUDPNotSupported.Create(RSTransparentProxyCanNotSupportUDP);
  219. end;
  220. end.