| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- {
- $Project$
- $Workfile$
- $Revision$
- $DateUTC$
- $Id$
- This file is part of the Indy (Internet Direct) project, and is offered
- under the dual-licensing agreement described on the Indy website.
- (http://www.indyproject.org/)
- Copyright:
- (c) 1993-2005, Chad Z. Hower and the Indy Pit Crew. All rights reserved.
- }
- {
- $Log$
- }
- {
- Rev 1.10 11/12/2004 11:30:16 AM JPMugaas
- Expansions for IPv6.
- Rev 1.9 11/11/2004 10:25:22 PM JPMugaas
- Added OpenProxy and CloseProxy so you can do RecvFrom and SendTo functions
- from the UDP client with SOCKS. You must call OpenProxy before using
- RecvFrom or SendTo. When you are finished, you must use CloseProxy to close
- any connection to the Proxy. Connect and disconnect also call OpenProxy and
- CloseProxy.
- Rev 1.8 11/11/2004 3:42:52 AM JPMugaas
- Moved strings into RS. Socks will now raise an exception if you attempt to
- use SOCKS4 and SOCKS4A with UDP. Those protocol versions do not support UDP
- at all.
- Rev 1.7 11/9/2004 8:18:00 PM JPMugaas
- Attempt to add SOCKS support in UDP.
- Rev 1.6 6/6/2004 11:51:56 AM JPMugaas
- Fixed TODO with an exception
- Rev 1.5 2004.02.03 4:17:04 PM czhower
- For unit name changes.
- Rev 1.4 10/15/2003 10:59:06 PM DSiders
- Corrected spelling error in resource string name.
- Added resource string for circular links exception in transparent proxy.
- Rev 1.3 10/15/2003 10:10:18 PM DSiders
- Added localization comments.
- Rev 1.2 5/16/2003 9:22:38 AM BGooijen
- Added Listen(...)
- Rev 1.1 5/14/2003 6:41:00 PM BGooijen
- Added Bind(...)
- Rev 1.0 12/2/2002 05:01:26 PM JPMugaas
- Rechecked in due to file corruption.
- }
- unit IdCustomTransparentProxy;
- interface
- {$I IdCompilerDefines.inc}
- //we need to put this in Delphi mode to work
- uses
- Classes,
- IdComponent,
- IdException,
- IdGlobal,
- IdIOHandler,
- IdSocketHandle,
- IdBaseComponent;
- type
- EIdTransparentProxyCircularLink = class(EIdException);
- EIdTransparentProxyUDPNotSupported = class(EIdException);
- TIdCustomTransparentProxyClass = class of TIdCustomTransparentProxy;
- TIdCustomTransparentProxy = class(TIdComponent)
- protected
- FHost: String;
- FPassword: String;
- FPort: TIdPort;
- FIPVersion : TIdIPVersion;
- FUsername: String;
- {$IFDEF USE_OBJECT_ARC}[Weak]{$ENDIF} FChainedProxy: TIdCustomTransparentProxy;
- //
- procedure InitComponent; override;
- function GetEnabled: Boolean; virtual; abstract;
- procedure SetEnabled(AValue: Boolean); virtual;
- procedure MakeConnection(AIOHandler: TIdIOHandler; const AHost: string; const APort: TIdPort; const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION); virtual; abstract;
- {$IFNDEF USE_OBJECT_ARC}
- procedure Notification(AComponent: TComponent; Operation: TOperation); override;
- {$ENDIF}
- procedure SetChainedProxy(const AValue: TIdCustomTransparentProxy);
- public
- procedure Assign(ASource: TPersistent); override;
- procedure OpenUDP(AHandle : TIdSocketHandle; const AHost: string = ''; const APort: TIdPort = 0; const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION); virtual;
- procedure CloseUDP(AHandle: TIdSocketHandle); virtual;
- function RecvFromUDP(AHandle: TIdSocketHandle; var ABuffer : TIdBytes;
- var VPeerIP: string; var VPeerPort: TIdPort; var VIPVersion: TIdIPVersion;
- AMSec: Integer = IdTimeoutDefault): Integer; virtual;
- procedure SendToUDP(AHandle: TIdSocketHandle;
- const AHost: string; const APort: TIdPort; const AIPVersion: TIdIPVersion;
- const ABuffer : TIdBytes); virtual;
- procedure Connect(AIOHandler: TIdIOHandler; const AHost: string; const APort: TIdPort; const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION);
- //
- procedure Bind(AIOHandler: TIdIOHandler; const AHost: string; const APort: TIdPort; const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION);overload;virtual;
- procedure Bind(AIOHandler: TIdIOHandler; const APort: TIdPort); overload;
- function Listen(AIOHandler: TIdIOHandler; const ATimeOut: Integer): Boolean; virtual;
- //
- property Enabled: Boolean read GetEnabled write SetEnabled;
- property Host: String read FHost write FHost;
- property Password: String read FPassword write FPassword;
- property Port: TIdPort read FPort write FPort;
- property IPVersion : TIdIPVersion read FIPVersion write FIPVersion default ID_DEFAULT_IP_VERSION;
- property Username: String read FUsername write FUsername;
- property ChainedProxy: TIdCustomTransparentProxy read FChainedProxy write SetChainedProxy;
- end;
- implementation
- uses
- IdResourceStringsCore, IdExceptionCore;
- { TIdCustomTransparentProxy }
- procedure TIdCustomTransparentProxy.InitComponent;
- begin
- inherited;
- FIPVersion := ID_DEFAULT_IP_VERSION;
- end;
- procedure TIdCustomTransparentProxy.Assign(ASource: TPersistent);
- var
- LSource: TIdCustomTransparentProxy;
- Begin
- if ASource is TIdCustomTransparentProxy then begin
- LSource := TIdCustomTransparentProxy(ASource);
- FHost := LSource.Host;
- FPassword := LSource.Password;
- FPort := LSource.Port;
- FIPVersion := LSource.IPVersion;
- FUsername := LSource.Username;
- end else begin
- inherited Assign(ASource);
- end;
- End;//
- procedure TIdCustomTransparentProxy.Connect(AIOHandler: TIdIOHandler; const AHost: string; const APort: TIdPort; const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION);
- var
- // under ARC, convert a weak reference to a strong reference before working with it
- LChainedProxy: TIdCustomTransparentProxy;
- begin
- LChainedProxy := FChainedProxy;
- if Assigned(LChainedProxy) and LChainedProxy.Enabled then begin
- MakeConnection(AIOHandler, LChainedProxy.Host, LChainedProxy.Port);
- LChainedProxy.Connect(AIOHandler, AHost, APort, AIPVersion);
- end else begin
- MakeConnection(AIOHandler, AHost, APort, AIPVersion);
- end;
- end;
- procedure RaiseProxyBindError;
- {$IFDEF USE_NORETURN}noreturn;{$ENDIF}
- begin
- raise EIdTransparentProxyCantBind.Create(RSTransparentProxyCannotBind);
- end;
- function TIdCustomTransparentProxy.Listen(AIOHandler: TIdIOHandler; const ATimeOut: integer):boolean;
- begin
- {$IFNDEF USE_NORETURN}
- Result := False; // keep the compiler happy
- {$ENDIF}
- RaiseProxyBindError;
- end;
- procedure TIdCustomTransparentProxy.Bind(AIOHandler: TIdIOHandler; const AHost: string; const APort: TIdPort; const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION);
- begin
- RaiseProxyBindError;
- end;
- procedure TIdCustomTransparentProxy.Bind(AIOHandler: TIdIOHandler; const APort: TIdPort);
- begin
- Bind(AIOHandler, '0.0.0.0', APort); {do not localize}
- end;
- procedure TIdCustomTransparentProxy.SetEnabled(AValue: Boolean);
- Begin
- End;
- // under ARC, all weak references to a freed object get nil'ed automatically
- {$IFNDEF USE_OBJECT_ARC}
- procedure TIdCustomTransparentProxy.Notification(AComponent: TComponent; Operation: TOperation);
- begin
- if (Operation = opRemove) and (AComponent = FChainedProxy) then begin
- FChainedProxy := nil;
- end;
- inherited Notification(AComponent,Operation);
- end;
- {$ENDIF}
- procedure TIdCustomTransparentProxy.SetChainedProxy(const AValue: TIdCustomTransparentProxy);
- var
- LNextValue: TIdCustomTransparentProxy;
- // under ARC, convert a weak reference to a strong reference before working with it
- LChainedProxy: TIdCustomTransparentProxy;
- begin
- LChainedProxy := FChainedProxy;
- if LChainedProxy <> AValue then
- begin
- LNextValue := AValue;
- while Assigned(LNextValue) do begin
- if LNextValue = Self then begin
- raise EIdTransparentProxyCircularLink.CreateFmt(RSInterceptCircularLink, [ClassName]);// -> One EIDCircularLink exception
- end;
- LNextValue := LNextValue.ChainedProxy;
- end;
- // under ARC, all weak references to a freed object get nil'ed automatically
- {$IFNDEF USE_OBJECT_ARC}
- if Assigned(LChainedProxy) then begin
- LChainedProxy.RemoveFreeNotification(Self);
- end;
- {$ENDIF}
- FChainedProxy := AValue;
- {$IFNDEF USE_OBJECT_ARC}
- if Assigned(AValue) then begin
- AValue.FreeNotification(Self);
- end;
- {$ENDIF}
- end;
- end;
- procedure RaiseUDPNotSupportedError;
- {$IFDEF USE_NORETURN}noreturn;{$ENDIF}
- begin
- raise EIdTransparentProxyUDPNotSupported.Create(RSTransparentProxyCanNotSupportUDP);
- end;
- procedure TIdCustomTransparentProxy.CloseUDP(AHandle: TIdSocketHandle);
- begin
- RaiseUDPNotSupportedError;
- end;
- procedure TIdCustomTransparentProxy.OpenUDP(AHandle: TIdSocketHandle;
- const AHost: string = ''; const APort: TIdPort = 0;
- const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION);
- begin
- RaiseUDPNotSupportedError;;
- end;
- function TIdCustomTransparentProxy.RecvFromUDP(AHandle: TIdSocketHandle;
- var ABuffer : TIdBytes; var VPeerIP: string; var VPeerPort: TIdPort;
- var VIPVersion: TIdIPVersion; AMSec: Integer = IdTimeoutDefault): Integer;
- begin
- {$IFNDEF USE_NORETURN}
- Result := 0; // keep the compiler happy
- {$ENDIF}
- RaiseUDPNotSupportedError;
- end;
- procedure TIdCustomTransparentProxy.SendToUDP(AHandle: TIdSocketHandle;
- const AHost: string; const APort: TIdPort; const AIPVersion: TIdIPVersion;
- const ABuffer : TIdBytes);
- begin
- RaiseUDPNotSupportedError;
- end;
- end.
|