Verfasst: 13.09.2006 15:16
Diese Version gibt mehr Infos raus (Proxy_Include.pb):
Aufzurufen mitlerweile so:
Code: Alles auswählen
;+----------------------------------+
; Proxy_Connect (NEW THREADED VERSION)
;
; (c)HeX0R 2006
;
; Procedure for giving your online-applications the
; possibility to connect through different proxys
; like http, socks4 and socks5.
;
;
;+----------------------------------+
;-Proxy-Error-Constants
Enumeration
#Proxy_Error_NoError
#Proxy_Error_Wrong_Password_Or_Username
#Proxy_Error_Connection_TimedOUT
#Proxy_Error_Proxy_Doesnt_Respond
#Proxy_Error_Wrong_Answer
#Proxy_Error_WinAPI_Error
#Proxy_Error_General_SOCK_ERROR
#Proxy_Error_Connection_Not_Allowed
#Proxy_Error_Network_Unreachable
#Proxy_Error_Host_Unreachable
#Proxy_Error_Connection_Refused
#Proxy_Error_TTL_Expired
#Proxy_Error_Command_Not_Supported
#Proxy_Error_AddressType_Not_Supported
#Proxy_Error_Unknown_SOCK_ERROR
#Proxy_Error_Request_Rejected_Or_Failed
#Proxy_Error_Cant_Connect_To_Ident_Server
#Proxy_Error_Ident_Respond_Is_Wrong
EndEnumeration
InitNetwork()
;-Internal Procedures
Procedure Name2IP(*url , *dest.BYTE)
Protected Ret.l, Result.l, *retval, wsaData.WSADATA, K.l, R.l
If WSAStartup_($101, @wsaData) = 0
*retval = gethostbyname_(*url)
Result = 0
If *retval <> 0
Result = PeekL(PeekL(PeekL(*retval + 12)))
EndIf
For i = 0 To 3
Ret = PeekB(@Result + i) & $FF
R = Ret
If Ret > 99
K = Int(R / 100)
*dest\b = '0' + K
*dest + 1
R - (K * 100)
EndIf
If Ret > 9
K = Int(R / 10)
*dest\b = '0' + K
*dest + 1
R - (K * 10)
EndIf
*dest\b = '0' + R
*dest + 1
If i < 3
*dest\b = '.'
*dest + 1
EndIf
Next i
*dest\b = 0
WSACleanup_()
EndIf
EndProcedure
Procedure.l IsIP(*ip, *Buffer = 0)
Protected *b.BYTE, C.l, Fa.l, F.l, Result.l
;ReturnValues :
;0 = no ip
;1 = IPv4 - IP
;2 = IPv6 - IP
*b = *ip
C = 0
While *b\b
If *b\b = '.'
C + 1
ElseIf *b\b < '0' Or *b\b > '9'
ProcedureReturn 0
EndIf
*b + 1
Wend
If *Buffer = 0
If C = 3
Result = 1
ElseIf C = 5
Result = 2
Else
ProcedureReturn 0
EndIf
Else
Result = C + 1
EndIf
*b = *ip + MemoryStringLength(*ip) - 1
Fa = 1
F = 0
Repeat
If *b\b <> '.'
F + (*b\b - '0')*Fa
Fa * 10
EndIf
If *b\b = '.' Or *b = *ip
If F > 255
ProcedureReturn 0
EndIf
If *Buffer
PokeB(*Buffer + C, F)
EndIf
C - 1
F = 0
Fa = 1
EndIf
*b - 1
Until *b < *ip
ProcedureReturn Result
EndProcedure
;-Main-Procedure
Procedure.l Proxy_Connect(*proxy_ip, Proxy_Port, *ip_to_connect, Port_To_Connect.l, Type.l = 0, *username = 0, *userpass = 0, TimeOUT = 10)
;-Main-Procedure
;*proxy_ip = Address of a String containing IP or URL of the Proxy-Server
;Proxy_Port = Port of the Proxy-Server
;*Ip_To_Connect = Address of a String containing IP or URL of where you want to connect through proxy.
;Port_To_Connect = Port of where you want to connect through proxy
;Type = 0 -> http-proxy
; = 1 -> socks4-proxy
; = 2 -> socks5-proxy
; If no authorization is needed set *username and *userpass = 0
;TimeOUT -> Time after connection-trial will time out (in SECONDS)
;Return-Value = =0 = Something went wrong, please call Proxy_GetLastError()
; >0 = ConnectionID, anything O.K.
;
Static Proxy_Last_Error.l
Protected *Buffer, *Text, *Text2
Protected IP_Type.l
Protected NID.l, i.l, hi.l, lo.l, ver.l, method.l, reserved.l, L.l
Protected Check_Timeout.l, P.l, Rv.l, CD.l, Port.l, Phase.l, status.l
Protected atyp.l, reply.l
If *proxy_ip = 0
P = Proxy_Last_Error
Proxy_Last_Error = 0
ProcedureReturn P
EndIf
If *username = 0 Or MemoryStringLength(*username) = 0
*userpass = 0
*username = 0
ElseIf *userpass = 0 Or MemoryStringLength(*userpass) = 0
*userpass = 0
EndIf
*Buffer = AllocateMemory(128 * 1024)
If *Buffer = 0
ProcedureReturn 0
EndIf
IP_Type = IsIP(*ip_to_connect)
*Text = 0
If Type = 1 And IP_Type = 0
Dim o.l(4)
Name2IP(*ip_to_connect, @o())
If o(0) = '.0.0' And o(1) = '0.0'
ProcedureReturn #False
EndIf
*ip_to_connect = @o()
ElseIf Type = 0
*Text = *Buffer + (120 * 1024)
*Text2 = *Text + 4096
i = MemoryStringLength(?Proxy_HTTP_0)
CopyMemory(?Proxy_HTTP_0, *Text + L, i)
L + i
i = MemoryStringLength(*ip_to_connect)
CopyMemory(*ip_to_connect, *Text + L, i)
L + i
PokeB(*Text + L, ':')
L + 1
lo = Port_To_Connect
If Port_To_Connect > 9999
hi = Int(lo / 10000)
PokeB(*Text + L, '0' + hi)
L + 1
lo - (hi * 10000)
EndIf
If Port_To_Connect > 999
hi = Int(lo / 1000)
PokeB(*Text + L, '0' + hi)
L + 1
lo - (hi * 1000)
EndIf
If Port_To_Connect > 99
hi = Int(lo / 100)
PokeB(*Text + L, '0' + hi)
L + 1
lo - (hi * 100)
EndIf
If Port_To_Connect > 9
hi = Int(lo / 10)
PokeB(*Text + L, '0' + hi)
L + 1
lo - (hi * 10)
EndIf
PokeB(*Text + L, '0' + lo)
L + 1
i = MemoryStringLength(?Proxy_HTTP_1)
CopyMemory(?Proxy_HTTP_1, *Text + L, i)
L + i
If *username And *userpass
i = MemoryStringLength(*username)
CopyMemory(*username, *Text2, i)
lo = i
PokeB(*Text2 + lo, ':')
lo + 1
i = MemoryStringLength(*userpass)
CopyMemory(*userpass, *Text2 + lo, i)
lo + i
hi = Base64Encoder(*Text2, lo, *Text2 + lo + 1, 4096 - lo - 2)
i = MemoryStringLength(?Proxy_HTTP_2)
CopyMemory(?Proxy_HTTP_2, *Text + L, i)
L + i
CopyMemory(*Text2 + lo + 1, *Text + L, hi)
L + hi
PokeW(*Text + L, $0A0D)
L + 2
i = MemoryStringLength(?Proxy_HTTP_3)
CopyMemory(?Proxy_HTTP_3, *Text + L, i)
L + i
CopyMemory(*Text2 + lo + 1, *Text + L, hi)
L + hi
PokeW(*Text + L, $0A0D)
L + 2
EndIf
PokeW(*Text + L, $0A0D)
PokeB(*Text + L + 2, 0)
EndIf
NID = OpenNetworkConnection(PeekS(*proxy_ip), Proxy_Port)
If NID
;-init
Select Type
Case 0
SendNetworkData(NID, *Text, MemoryStringLength(*Text))
Case 1
PokeB(*Buffer, 4)
PokeB(*Buffer + 1, 1)
hi = Int(Port_To_Connect / 256)
lo = Port_To_Connect - (256 * hi)
PokeB(*Buffer + 2, hi)
PokeB(*Buffer + 3, lo)
hi = 4 + IsIP(*ip_to_connect, *Buffer + 4)
L = hi
If *username
L = MemoryStringLength(*username) + 1
CopyMemory(*username, *Buffer + hi, L)
L + hi
EndIf
SendNetworkData(NID, *Buffer, L)
Case 2
PokeB(*Buffer, 5)
PokeB(*Buffer + 2, 0)
If *username = 0 And *userpass = 0
L = 3
PokeB(*Buffer + 1, 1)
Else
L = 4
PokeB(*Buffer + 1, 2)
PokeB(*Buffer + 3, 2)
EndIf
SendNetworkData(NID, *Buffer, L)
EndSelect
Check_Timeout = ElapsedMilliseconds() + ( TimeOUT * 1000 )
P = 0
Repeat
Select NetworkClientEvent(NID)
Case 0
Delay(5)
Case 2
Rv = ReceiveNetworkData(NID, *Buffer + P, 4096)
If Rv > 0
P + Rv
If Rv < 4096
Select Type
Case 0
For i = 0 To P
If PeekB(*Buffer + i) = 32
Break
EndIf
Next i
If PeekB(*Buffer + i + 1) = '2' And PeekB(*Buffer + i + 2) = '0' And PeekB(*Buffer + i + 3) = '0'
Break
Else
Debug PeekS(*Buffer, P) ;<-Show HTML-Error-MSG
Proxy_Last_Error = #Proxy_Error_Wrong_Answer
CloseNetworkConnection(NID)
NID = 0
EndIf
Case 1
ver = PeekB(*Buffer) & 255
CD = PeekB(*Buffer + 1) & 255
port = (PeekB(*Buffer + 2) & 255) * 256
port + (PeekB(*Buffer + 3) & 255)
If ver = 0 And CD = 90
Break
Else
Select CD
Case 91
Debug "Request_Rejected_Or_Failed"
Proxy_Last_Error = #Proxy_Error_Request_Rejected_Or_Failed
Case 92
Debug "Cant_Connect_To_Ident_Server"
Proxy_Last_Error = #Proxy_Error_Cant_Connect_To_Ident_Server
Case 93
Debug "Ident_Respond_Is_Wrong"
Proxy_Last_Error = #Proxy_Error_Ident_Respond_Is_Wrong
EndSelect
CloseNetworkConnection(NID)
NID = 0
EndIf
Case 2
If Phase = 0
ver = PeekB(*Buffer) & 255
method = PeekB(*Buffer + 1) & 255
If method = 2
;-Phase 2 SOCK5
;Send Username/Password (if allowed)
PokeB(*Buffer, 1)
L = MemoryStringLength(*username)
PokeB(*Buffer + 1, L)
If L
CopyMemory(*username, *Buffer + 2, L + 1)
EndIf
L + 2
PokeB(*Buffer + L, MemoryStringLength(*userpass))
If *userpass
CopyMemory(*userpass, *Buffer + L + 1, MemoryStringLength(*userpass))
EndIf
SendNetworkData(NID, *Buffer, L + 1 + MemoryStringLength(*userpass))
Phase + 1
Else
Phase + 2
EndIf
ElseIf Phase = 1
status = PeekB(*Buffer + 1) & 255
If PeekB(*Buffer) = 1 And status = 0
Phase + 1
Else
Debug "Wrong Password or Username"
Proxy_Last_Error = #Proxy_Error_Wrong_Password_Or_Username
CloseNetworkConnection(NID)
NID = 0
;Not supported
EndIf
EndIf
If Phase = 2
PokeB(*Buffer, 5)
PokeB(*Buffer + 1, 1)
PokeB(*Buffer + 2, 0)
Select IP_Type
Case 0
PokeB(*Buffer + 3, 3)
hi = MemoryStringLength(*ip_to_connect)
PokeB(*Buffer + 4, hi)
CopyMemory(*ip_to_connect, *Buffer + 5, hi)
L = 5 + hi
Case 1
PokeB(*Buffer + 3, 1)
L = 4 + IsIP(*ip_to_connect, *Buffer + 4)
Case 2
PokeB(*Buffer + 3, 4)
L = 4 + IsIP(*ip_to_connect, *Buffer + 4)
EndSelect
hi = Int(Port_To_Connect / 256)
lo = Port_To_Connect - (256 * hi)
PokeB(*Buffer + L, hi)
PokeB(*Buffer + L + 1, lo)
SendNetworkData(NID, *Buffer, L + 2)
Phase = 3
ElseIf Phase = 3
ver = PeekB(*Buffer) & 255
Reply = PeekB(*Buffer + 1) & 255
reserved = PeekB(*Buffer + 2) & 255
atyp = PeekB(*Buffer + 3) & 255
If ver = 5 And Reply = 0
; Select atyp
; Case 1
; ;IPv4
; ip.s = Str(PeekB(*buffer + 4) & 255) + "." + Str(PeekB(*buffer + 5) & 255) + "."
; ip + Str(PeekB(*buffer + 6) & 255) + "." + Str(PeekB(*buffer + 7) & 255)
; port.l = (PeekB(*buffer + 8) & 255) * 256
; port + (PeekB(*buffer + 9) & 255)
; Case 3
; ;Name
; ip.s = ""
; For i = 1 To (PeekB(*buffer + 4) & 255)
; ip + Chr(PeekB(*buffer + 4 + i) & 255)
; Next i
; port.l = (PeekB(*buffer + 4 + i) & 255) * 256
; port + (PeekB(*buffer + 5 + i) & 255)
; Case 4
; ;IPv6
; ip.s = Str(PeekB(*buffer + 4) & 255) + "." + Str(PeekB(*buffer + 5) & 255) + "."
; ip + Str(PeekB(*buffer + 6) & 255) + "." + Str(PeekB(*buffer + 7) & 255) + "."
; ip + Str(PeekB(*buffer + 8) & 255) + "." + Str(PeekB(*buffer + 9) & 255)
; port.l = (PeekB(*buffer + 10) & 255) * 256
; port + (PeekB(*buffer + 11) & 255)
; EndSelect
; ;Following IP and Port is just for information...
; ;this is proxy-internal and not necessarily the same as ip_to_connect/port_to_connect
; Debug "Internal Proxy-IP:" + ip
; Debug "Internal Proxy-Port:" + Str(port)
Break
Else
Select Reply
Case 1
Debug "General_SOCK_ERROR"
Proxy_Last_Error = #Proxy_Error_General_SOCK_ERROR
Case 2
Debug "Connection_Not_Allowed"
Proxy_Last_Error = #Proxy_Error_Connection_Not_Allowed
Case 3
Debug "Network_Unreachable"
Proxy_Last_Error = #Proxy_Error_Network_Unreachable
Case 4
Debug "Host_Unreachable"
Proxy_Last_Error = #Proxy_Error_Host_Unreachable
Case 5
Debug "Connection_Refused"
Proxy_Last_Error = #Proxy_Error_Connection_Refused
Case 6
Debug "TTL_Expired"
Proxy_Last_Error = #Proxy_Error_TTL_Expired
Case 7
Debug "Command_Not_Supported"
Proxy_Last_Error = #Proxy_Error_Command_Not_Supported
Case 8
Debug "AddressType_Not_Supported"
Proxy_Last_Error = #Proxy_Error_AddressType_Not_Supported
Default
Debug "Unknown_SOCK_ERROR"
Proxy_Last_Error = #Proxy_Error_Unknown_SOCK_ERROR
EndSelect
CloseNetworkConnection(NID)
NID = 0
EndIf
EndIf
EndSelect
P = 0
EndIf
ElseIf Rv = -1
Debug WSAGetLastError_()
Proxy_Last_Error = #Proxy_Error_WinAPI_Error
CloseNetworkConnection(NID)
NID = 0
EndIf
EndSelect
If Check_Timeout < ElapsedMilliseconds()
Debug "Timed OUT..."
Proxy_Last_Error = #Proxy_Error_Connection_TimedOUT
CloseNetworkConnection(NID)
NID = 0
EndIf
Until NID = 0
Else
Debug "No Respond from Proxy!"
Proxy_Last_Error = #Proxy_Error_Proxy_Doesnt_Respond
EndIf
FreeMemory(*Buffer)
ProcedureReturn NID
EndProcedure
Procedure.l Proxy_GetLastError()
ProcedureReturn Proxy_Connect(0, 0, 0, 0)
EndProcedure
DataSection
Proxy_HTTP_0:
Data.s "CONNECT "
Proxy_HTTP_1:
Data.s " HTTP/1.0" + #CRLF$
Proxy_HTTP_2:
Data.s "Authorization: Basic "
Proxy_HTTP_3:
Data.s "Proxy-Authorization: Basic "
EndDataSection
Code: Alles auswählen
;/-------------
;|
;| Proxy-Connect-Example
;| (c)HeX0R
;|
;/-------------
IncludeFile "Proxy_Include.pb"
Proxy_IP.s = "127.0.0.1"
Proxy_Port.l = 1080
Proxy_User.s = ""
Proxy_Pass.s = ""
Proxy_Type.l = 0
IP_To_Connect.s = "stargate.de.eu.phat-net.de"
Port_To_Connect.l = 6667
TimeOUT.l = 60 ; Seconds
NetID = Proxy_Connect(@Proxy_IP, Proxy_Port, @IP_To_Connect, Port_To_Connect, Proxy_Type, @Proxy_User, @Proxy_Pass, TimeOUT)
If NetID = 0
Debug Proxy_GetLastError()
Else
Debug "OK [" + Str(NetID) + "]"
CloseNetworkConnection(NetID)
EndIf