📄 adwnport.pas
字号:
if DeviceLayer = dlWinsock then begin
TapiMode := tmOff;
FUseMSRShadow := False;
end else
TapiMode := tmAuto;
end;
function TApdCustomWinsockPort.DoAccept(Addr : LongInt): Boolean;
begin
Result := True;
if Assigned(FOnWsAccept) then FOnWsAccept(Self, TInAddr(Addr), Result);
end;
procedure TApdCustomWinsockPort.DoConnect;
begin
FConnectFired := True; {!!.05}
if Assigned(FOnWsConnect) then FOnWsConnect(Self);
end;
procedure TApdCustomWinsockPort.DoDisconnect;
begin
if (FConnectFired or (FWsMode = WsServer))and Assigned(FOnWsDisconnect){!!.06}
then FOnWsDisconnect(Self);
FConnectFired := False; {!!.05}
end;
procedure TApdCustomWinsockPort.DoError(ErrCode : Integer);
begin
{if (ErrCode >= 10052) and (ErrCode <= 10054) then} {!!.05}
case ErrCode of {!!.05}
10052, {wsaENetReset} {!!.05}
10053, {wsaEConnAborted} {!!.05}
10054, {wsaEConnReset} {!!.05}
10061: {wsaEConnRefused} {!!.05}
if (FWsMode = WsClient) then begin
PostMessage(ComWindow, CM_APDSOCKETQUIT, 0, 0);
end else
DoDisconnect;
end;
if Assigned(FOnWsError) then FOnWsError(Self, ErrCode);
end;
procedure TApdCustomWinsockPort.EnumHostAddresses;
const
MaxAddrs = 256;
var
HostStr : array[0..255] of AnsiChar;
HostEnt : PHostEnt;
i : Integer;
begin
FWsLocalAddresses.Clear;
if SockFuncs.GetHostName (@HostStr, SizeOf (HostStr)) = 0 then begin
HostEnt := SockFuncs.GetHostByName (@HostStr);
if assigned (HostEnt) then
for i := 0 to MaxAddrs - 1 do
if (HostEnt.h_addr_list[i] = nil) then
break
else
FWsLocalAddresses.Add (Format ('%d.%d.%d.%d',
[Integer (HostEnt.h_addr_list[i].S_un_b.s_b1),
Integer (HostEnt.h_addr_list[i].S_un_b.s_b2),
Integer (HostEnt.h_addr_list[i].S_un_b.s_b3),
Integer (HostEnt.h_addr_list[i].S_un_b.s_b4)]));
end;
end;
function TApdCustomWinsockPort.InitializePort : Integer;
var
TempAddress : DWORD;
ErrCode : Integer;
TempPort : Word;
begin
if DeviceLayer = dlWinsock then begin
{ Convert or lookup the address }
if FWsMode = WsClient then begin
FDispatcher.DeviceName := format( {!!.06}
'Winsock client, connect to %s on port %s', [WsAddress, WsPort]);{!!.06}
if FWsAddress = '' then
raise EApdSocketException.CreateTranslate(WSAEDESTADDRREQ, 0, 0);
{ Handle SOCKS negotiation if needed }
if WsSocksServerInfo.SocksVersion <> svNone then begin
Result := OpenSocksConnection;
Exit;
end;
TempAddress := (ApdSocket.String2NetAddr(FWsAddress).S_addr);
if TempAddress = INADDR_NONE then
TempAddress := (ApdSocket.LookupName(FWsAddress).S_addr);
if TempAddress = 0 then
if Assigned(FOnWsError) then begin
OnWsError(Self, ADWSCANTRESOLVE);
Result := -ADWSCANTRESOLVE;
Exit;
end else
raise EApdSocketException.CreateTranslate(ADWSCANTRESOLVE, 0, 0);
end else begin
TempAddress := INADDR_ANY;
FDispatcher.DeviceName := format( {!!.06}
'Winsock server on port %s', [WsPort]); {!!.06}
end;
{ Convert or lookup the port }
Val(FWsPort, TempPort, ErrCode);
if ErrCode <> 0 then
TempPort := ApdSocket.LookupService(FWsPort);
if TempPort = 0 then
raise EApdSocketException.CreateTranslate(ADWSINVPORT, 0, 0);
TempPort := ApdSocket.htons(TempPort);
{ Init the socket }
TApdWinSockDispatcher(Dispatcher).InitSocketData(WsLocalAddressIndex,
TempAddress, TempPort,
(FWsMode = WsClient), FWsTelnet);
Result := Dispatcher.InitSocket(FInSize, FOutSize);
if Result < 0 then
raise EApdSocketException.CreateTranslate(-Result, 0, 0);
PortState := psOpen;
end else
Result := inherited InitializePort;
end;
{ This is our access to the Comport window }
procedure TApdCustomWinsockPort.SockWndProc(var Message: TMessage);
procedure Default;
begin
with Message do
Result := CallWindowProc(FComWindowProc, ComWindow, Msg, wParam, lParam);
end;
begin
if Message.Msg = CM_APDSOCKETMESSAGE then
with Message do begin
case wParam of
FD_CONNECT : DoConnect;
FD_CLOSE : PostMessage(ComWindow, CM_APDSOCKETQUIT, 0, 0);
FD_ACCEPT : Result := LongInt(DoAccept(LParam));
FD_CLOSE or FD_CONNECT : DoDisconnect;
else
DoError(wParam);
end;
end
else if Message.Msg = CM_APDSOCKETQUIT then
Open := False
else Default;
end;
procedure TApdCustomWinsockPort.PortOpen;
begin
FSockInstance := MakeObjectInstance(SockWndProc);
FComWindowProc := Pointer(SetWindowLong(ComWindow, GWL_WNDPROC, LongInt(FSockInstance)));
{ Perform the SOCKS negotiation }
if WsSocksServerInfo.SocksVersion <> svNone then
ContinueSocksNegotiation;
inherited PortOpen;
end;
procedure TApdCustomWinsockPort.PortClose;
begin
inherited PortClose;
DoDisconnect;
end;
procedure TApdCustomWinsockPort.ValidateComport;
begin
{ If we're in Winsock mode, we don't care if it's a valid comport number }
if DeviceLayer <> dlWinsock then
inherited ValidateComport;
end;
procedure TApdCustomWinsockPort.SetUseMSRShadow(NewUse : Boolean);
begin
inherited SetUseMSRShadow(NewUse);
if DeviceLayer = dlWinsock then
FUseMSRShadow := False;
end;
procedure TApdCustomWinsockPort.SetWsAddress(Value : string);
begin
if (Value = FWsAddress) then Exit;
if (PortState = psOpen) then
raise EApdSocketException.CreateTranslate(ADWSCANTCHANGE, 0, 0)
else
FWsAddress := Trim(Value);
end;
procedure TApdCustomWinsockPort.SetWsLocalAddresses (Value : TStringList);
begin
FWsLocalAddresses.Assign (Value);
end;
function TApdCustomWinsockPort.GetWsLocalAddresses: TStringList; {!!.04}
begin {!!.04}
EnumHostAddresses; {!!.04}
Result := FWsLocalAddresses; {!!.04}
end; {!!.04}
procedure TApdCustomWinsockPort.SetWsLocalAddressIndex (Value : Integer);
begin
if Value <> FWsLocalAddressIndex then
FWsLocalAddressIndex := Value;
end;
procedure TApdCustomWinsockPort.SetWsMode(Value : TWsMode);
begin
if (Value = FWsMode) then Exit;
if (PortState = psOpen) then
raise EApdSocketException.CreateTranslate(ADWSCANTCHANGE, 0, 0)
else
FWsMode := Value;
end;
procedure TApdCustomWinsockPort.SetWsPort(Value : string);
begin
if (Value = FWsPort) then Exit;
if (PortState = psOpen) then
raise EApdSocketException.CreateTranslate(ADWSCANTCHANGE, 0, 0)
else
FWsPort := Trim(Value);
end;
function TApdCustomWinsockPort.OpenSocksConnection : Integer;
{ Create the Socks reply data packet and open the socket }
begin
{ Set up the data packet }
FConnectPacket := TApdDataPacket.Create (Self);
FConnectPacket.Enabled := False;
FConnectPacket.ComPort := Self;
FConnectPacket.StartCond := scAnyData;
FConnectPacket.Timeout := 0;
FConnectPacket.AutoEnable := False;
{ Open the socket }
Result := OpenSocksSocket;
end;
function TApdCustomWinsockPort.OpenSocksSocket : Integer;
{ Open a socket for SOCKS negotiation }
var
TempAddress : DWORD;
TempPort : Word;
begin
{ Convert or lookup the address }
TempAddress := (ApdSocket.String2NetAddr (WsSocksServerInfo.Address).S_addr);
if TempAddress = INADDR_NONE then
TempAddress := (ApdSocket.LookupName (WsSocksServerInfo.Address).S_addr);
if TempAddress = 0 then begin
FSocksComplete := True;
if Assigned (FOnWsError) then begin
Result := -ADWSCANTRESOLVE;
OnWsError (Self, ADWSCANTRESOLVE);
Exit;
end else
raise EApdSocketException.CreateTranslate(ADWSCANTRESOLVE, 0, 0);
end;
{ Convert or lookup the port }
TempPort := ApdSocket.htons (WsSocksServerInfo.Port);
{ Init the socket }
TApdWinSockDispatcher (Dispatcher).InitSocketData (WsLocalAddressIndex,
TempAddress, TempPort,
(FWsMode = WsClient), FWsTelnet);
Result := Dispatcher.InitSocket (FInSize, FOutSize);
if Result < 0 then begin
FSocksComplete := True;
if Assigned (FOnWsError) then begin
Result := -ADWSCANTRESOLVE;
OnWsError (Self, ADWSCANTRESOLVE);
Exit;
end else
raise EApdSocketException.CreateTranslate (-Result, 0, 0);
end;
PortState := psOpen;
end;
procedure TApdCustomWinsockPort.ContinueSocksNegotiation;
{ Actually perform the SOCKS negotiation }
begin
try
{ Perform SOCKS negotiation based upon the socks version }
case WsSocksServerInfo.SocksVersion of
svNone : Exit;
svSocks4 : ConnectToSocks4;
svSocks4a : ConnectToSocks4a;
svSocks5 : ConnectToSocks5;
end;
{ Wait for the negotiation to complete }
while FSocksComplete = False do
DelayTicks (1, True);
finally
{ release SOCKS reply data packet }
FConnectPacket.Free;
end;
end;
procedure TApdCustomWinsockPort.ConnectToSocks4;
{ Sends a Socks 4 request }
var
TempAddress : DWORD;
ErrCode : Integer;
TempPort : Word;
begin
EnableSocks4Reply;
{ Convert or lookup the port }
Val (FWsPort, TempPort, ErrCode);
if ErrCode <> 0 then
TempPort := ApdSocket.LookupService (FWsPort);
if TempPort = 0 then
raise EApdSocketException.CreateTranslate (ADWSINVPORT, 0, 0);
TempPort := ApdSocket.htons (TempPort);
{ Convert or lookup the IP address }
if FWsAddress = '' then
raise EApdSocketException.CreateTranslate (WSAEDESTADDRREQ, 0, 0);
TempAddress := (ApdSocket.String2NetAddr (FWsAddress).S_addr);
if TempAddress = INADDR_NONE then
TempAddress := (ApdSocket.LookupName (FWsAddress).S_addr);
if TempAddress = 0 then
if Assigned (FOnWsError) then begin
OnWsError (Self, ADWSCANTRESOLVE);
Exit;
end else
raise EApdSocketException.CreateTranslate (ADWSCANTRESOLVE, 0, 0);
{ Send the SOCKS 4 request }
PutChar (#$04); { VN - Version Number }
PutChar (#$01); { CD - Command Code - 1 }
PutChar (Char (TempPort and $00ff)); { DSTPORT - Destination Port }
PutChar (Char ((TempPort and $ff00) shr 8));
PutChar (Char (TempAddress and $000000ff)); { DSTID - Dest IP }
PutChar (Char ((TempAddress and $0000ff00) shr 8));
PutChar (Char ((TempAddress and $00ff0000) shr 16));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -