OVH Cloud OVH Cloud

Visual Basic 6: WinInet et Proxy Socks

2 réponses
Avatar
Cyril Hunter
Bonjour à toute la communauté,

je viens posté ici car c'est mon dernier recours...et je suis un peu perdu
après avoir farfouillé le net)
Je dois me connecté à un FTP pour récupéré des fichiers...cependant, je dois
passé par un proxy SOCKS.

J'ai pris des infos pour exemple :
proxyname: 'proxyexample"
port: 1080 (port socks)
adresse ftp: 192.168.0.1
port ftp: 21

voici le code:

'''''''''''''''''''''''''''''''''''''''''
declaration
''''''''''''''''''''''''''''''''''''''''''''
'FTP
Public Const FTP_TRANSFER_TYPE_UNKNOWN = &H0
Public Const FTP_TRANSFER_TYPE_ASCII = &H1
Public Const FTP_TRANSFER_TYPE_BINARY = &H2
Public Const INTERNET_DEFAULT_FTP_PORT = 21 ' default for FTP servers
Public Const INTERNET_SERVICE_FTP = 1
Public Const INTERNET_FLAG_PASSIVE = &H8000000 ' used for FTP connections
Public Const INTERNET_OPEN_TYPE_PRECONFIG = 0 ' use registry configuration
Public Const INTERNET_OPEN_TYPE_DIRECT = 1 ' direct to net
Public Const INTERNET_OPEN_TYPE_PROXY = 3 ' via named proxy
Public Const INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY = 4 ' prevent
using java/script/INS
Public Const INTERNET_DEFAULT_SOCKS_PORT = 1080 'PROXY SOCKS

Public Const MAX_PATH = 260
Public Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Public Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type
Public Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet
As Long) As Integer
Public Declare Function InternetConnect Lib "wininet.dll" Alias
"InternetConnectA" (ByVal hInternetSession As Long, ByVal sServerName As
String, ByVal nServerPort As Integer, ByVal sUserName As String, ByVal
sPassword As String, ByVal lService As Long, ByVal lFlags As Long, ByVal
lContext As Long) As Long
Public Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA"
(ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As
String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
Public Declare Function FtpSetCurrentDirectory Lib "wininet.dll" Alias
"FtpSetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As
String) As Boolean
Public Declare Function FtpGetCurrentDirectory Lib "wininet.dll" Alias
"FtpGetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal
lpszCurrentDirectory As String, lpdwCurrentDirectory As Long) As Long
Public Declare Function FtpCreateDirectory Lib "wininet.dll" Alias
"FtpCreateDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As
String) As Boolean
Public Declare Function FtpRemoveDirectory Lib "wininet.dll" Alias
"FtpRemoveDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As
String) As Boolean
Public Declare Function FtpDeleteFile Lib "wininet.dll" Alias
"FtpDeleteFileA" (ByVal hFtpSession As Long, ByVal lpszFileName As String) As
Boolean
Public Declare Function FtpRenameFile Lib "wininet.dll" Alias
"FtpRenameFileA" (ByVal hFtpSession As Long, ByVal lpszExisting As String,
ByVal lpszNew As String) As Boolean
Public Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA"
(ByVal hConnect As Long, ByVal lpszRemoteFile As String, ByVal lpszNewFile As
String, ByVal fFailIfExists As Long, ByVal dwFlagsAndAttributes As Long,
ByVal dwFlags As Long, ByRef dwContext As Long) As Boolean
Public Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA"
(ByVal hConnect As Long, ByVal lpszLocalFile As String, ByVal
lpszNewRemoteFile As String, ByVal dwFlags As Long, ByVal dwContext As Long)
As Boolean
Public Declare Function InternetGetLastResponseInfo Lib "wininet.dll" Alias
"InternetGetLastResponseInfoA" (lpdwError As Long, ByVal lpszBuffer As
String, lpdwBufferLength As Long) As Boolean
Public Declare Function FtpFindFirstFile Lib "wininet.dll" Alias
"FtpFindFirstFileA" (ByVal hFtpSession As Long, ByVal lpszSearchFile As
String, lpFindFileData As WIN32_FIND_DATA, ByVal dwFlags As Long, ByVal
dwContent As Long) As Long
Public Declare Function InternetFindNextFile Lib "wininet.dll" Alias
"InternetFindNextFileA" (ByVal hFind As Long, lpvFindData As WIN32_FIND_DATA)
As Long
Public Const PassiveConnection As Boolean = True

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Code
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

'connexion au net
hOpen = InternetOpen("FTP", INTERNET_OPEN_TYPE_PROXY,
"ftp=ftp://proxytcp:1080", vbNullString, 0)
'connect to the FTP server
hConnection = InternetConnect(hOpen, 192.168.0.1,
INTERNET_DEFAULT_SOCKS_PORT, "cyril", "hunter", INTERNET_SERVICE_FTP,
IIf(PassiveConnection, INTERNET_FLAG_PASSIVE, 0), 0)
MsgBox hConnection
'''''''''''''''''''''''''''''''''''''''''

le hConnection est à 0, impossible de m'y connecter, meme en remplaçant
INTERNET_DEFAULT_SOCKS_PORT par INTERNET_DEFAULT_FTP_PORT

Je ne sais vraiment plus quoi faire, c'est un travail urgent mais je bloque
la dessus depuis des jours...
Je vous remercie sincèrment de votre aide

Cyril

2 réponses

Avatar
Clive Lumb
"Cyril Hunter" a écrit dans le message de
news:
Bonjour à toute la communauté,

je viens posté ici car c'est mon dernier recours...et je suis un peu perdu
après avoir farfouillé le net)
Je dois me connecté à un FTP pour récupéré des fichiers...cependant, je


dois
passé par un proxy SOCKS.

J'ai pris des infos pour exemple :
proxyname: 'proxyexample"
port: 1080 (port socks)
adresse ftp: 192.168.0.1
port ftp: 21



Je ne suis jamais arrivé à le faire marcher comme ça...

Par contre, si tu configures Internet Explorer pour utiliser le proxy, alors
ça marche avec INTERNET_OPEN_TYPE_PRECONFIG
Voici un bout de code:
(FTPSym est un form, EVLog est une classe pour écrire dans l'observateur
d'evenements - tu pourras les scratcher)

Bon courage
Clive




Option Explicit

Public Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA"
(ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As
String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
Public Declare Function InternetOpenUrl Lib "wininet.dll" Alias
"InternetOpenUrlA" (ByVal hOpen As Long, ByVal sUrl As String, ByVal
sHeaders As String, ByVal lLength As Long, ByVal lFlags As Long, ByVal
lContext As Long) As Long
Public Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile As
Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long,
lNumberOfBytesRead As Long) As Integer
Public Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet
As Long) As Integer

Public Const INTERNET_OPEN_TYPE_PRECONFIG = 0
Public Const INTERNET_OPEN_TYPE_DIRECT = 1
Public Const INTERNET_OPEN_TYPE_PROXY = 3

Public Const scUserAgent = "VB OpenUrl"
Public Const INTERNET_FLAG_RELOAD = &H80000000
Public ErrorFlag As Boolean
Public CloseRequest As Boolean
Public Downloading As Boolean
Public dlSize As Long


Sub Download(URL As String, SaveAs As String)

Dim hOpen As Long
Dim hOpenUrl As Long
Dim bDoLoop As Boolean
Dim bRet As Boolean
Dim sReadBuffer As String * 2048
Dim lNumberOfBytesRead As Long
Dim sBuffer As String
Dim KbCounter As Long
Dim KbCounterGranularity As Integer
Dim OriginalText As String
Dim GranularityCounter As Integer
Dim EVLog As New NTEvent
Dim Msg As String
ErrorFlag = False
KbCounterGranularity = 5
OriginalText = FTPSym.Text1.Text
On Error GoTo Oops
Downloading = True
FTPSym.CLProgressBar1.Visible = True
FTPSym.CLProgressBar1.Progress 0
DoEvents
hOpen = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_PRECONFIG,
vbNullString, vbNullString, 0)
hOpenUrl = InternetOpenUrl(hOpen, URL, vbNullString, 0,
INTERNET_FLAG_RELOAD, 0)
KbCounter = 0
bDoLoop = True
GranularityCounter = 0
While bDoLoop
sReadBuffer = vbNullString
bRet = InternetReadFile(hOpenUrl, sReadBuffer, Len(sReadBuffer),
lNumberOfBytesRead)
sBuffer = sBuffer & Left$(sReadBuffer, lNumberOfBytesRead)
GranularityCounter = GranularityCounter + 1
KbCounter = KbCounter + CLng((lNumberOfBytesRead / 1024))
If GranularityCounter = KbCounterGranularity Then
FTPSym.Text1.Text = OriginalText & Format(Now(), "dd/mm/yy
hh:mm:ss") & " " & Str$(KbCounter) & "/" & Trim$(Str$(dlSize)) & " Ko lus" &
vbCrLf
FTPSym.CLProgressBar1.Progress 100 * KbCounter / dlSize
GranularityCounter = 0
DoEvents
End If
If (Not CBool(lNumberOfBytesRead)) Or CloseRequest Then bDoLoop False
Wend

FTPSym.Text1.Text = OriginalText & Format(Now(), "dd/mm/yy hh:mm:ss") &
" " & Str$(KbCounter) & " Ko lus" & vbCrLf
FTPSym.CLProgressBar1.Visible = False

On Error GoTo 0
If Not CloseRequest Then
Open SaveAs For Binary Access Write As #1
Put #1, , sBuffer
Close #1
Else
Msg = vbCrLf & vbCrLf & " Download aborted"
EVLog.LogEvent "FTP Symantec", Msg, vbLogEventTypeWarning, 6

End If
If hOpenUrl <> 0 Then InternetCloseHandle (hOpenUrl)
If hOpen <> 0 Then InternetCloseHandle (hOpen)
Downloading = False
Exit Sub

Oops:
MsgBox Err.Description
EVLog.LogEvent "FTP Symantec", Err.Description, vbLogEventTypeError, 5

Set EVLog = Nothing
If hOpenUrl <> 0 Then InternetCloseHandle (hOpenUrl)
If hOpen <> 0 Then InternetCloseHandle (hOpen)

End Sub
Avatar
Cyril Hunter
effectivement, ca fonctionne , merci infiiiiniment :D

Cordialement,

Cyril

"Clive Lumb" a écrit :


"Cyril Hunter" a écrit dans le message de
news:
> Bonjour à toute la communauté,
>
> je viens posté ici car c'est mon dernier recours...et je suis un peu perdu
> après avoir farfouillé le net)
> Je dois me connecté à un FTP pour récupéré des fichiers...cependant, je
dois
> passé par un proxy SOCKS.
>
> J'ai pris des infos pour exemple :
> proxyname: 'proxyexample"
> port: 1080 (port socks)
> adresse ftp: 192.168.0.1
> port ftp: 21
>
Je ne suis jamais arrivé à le faire marcher comme ça...

Par contre, si tu configures Internet Explorer pour utiliser le proxy, alors
ça marche avec INTERNET_OPEN_TYPE_PRECONFIG
Voici un bout de code:
(FTPSym est un form, EVLog est une classe pour écrire dans l'observateur
d'evenements - tu pourras les scratcher)

Bon courage
Clive




Option Explicit

Public Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA"
(ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As
String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
Public Declare Function InternetOpenUrl Lib "wininet.dll" Alias
"InternetOpenUrlA" (ByVal hOpen As Long, ByVal sUrl As String, ByVal
sHeaders As String, ByVal lLength As Long, ByVal lFlags As Long, ByVal
lContext As Long) As Long
Public Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile As
Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long,
lNumberOfBytesRead As Long) As Integer
Public Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet
As Long) As Integer

Public Const INTERNET_OPEN_TYPE_PRECONFIG = 0
Public Const INTERNET_OPEN_TYPE_DIRECT = 1
Public Const INTERNET_OPEN_TYPE_PROXY = 3

Public Const scUserAgent = "VB OpenUrl"
Public Const INTERNET_FLAG_RELOAD = &H80000000
Public ErrorFlag As Boolean
Public CloseRequest As Boolean
Public Downloading As Boolean
Public dlSize As Long


Sub Download(URL As String, SaveAs As String)

Dim hOpen As Long
Dim hOpenUrl As Long
Dim bDoLoop As Boolean
Dim bRet As Boolean
Dim sReadBuffer As String * 2048
Dim lNumberOfBytesRead As Long
Dim sBuffer As String
Dim KbCounter As Long
Dim KbCounterGranularity As Integer
Dim OriginalText As String
Dim GranularityCounter As Integer
Dim EVLog As New NTEvent
Dim Msg As String
ErrorFlag = False
KbCounterGranularity = 5
OriginalText = FTPSym.Text1.Text
On Error GoTo Oops
Downloading = True
FTPSym.CLProgressBar1.Visible = True
FTPSym.CLProgressBar1.Progress 0
DoEvents
hOpen = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_PRECONFIG,
vbNullString, vbNullString, 0)
hOpenUrl = InternetOpenUrl(hOpen, URL, vbNullString, 0,
INTERNET_FLAG_RELOAD, 0)
KbCounter = 0
bDoLoop = True
GranularityCounter = 0
While bDoLoop
sReadBuffer = vbNullString
bRet = InternetReadFile(hOpenUrl, sReadBuffer, Len(sReadBuffer),
lNumberOfBytesRead)
sBuffer = sBuffer & Left$(sReadBuffer, lNumberOfBytesRead)
GranularityCounter = GranularityCounter + 1
KbCounter = KbCounter + CLng((lNumberOfBytesRead / 1024))
If GranularityCounter = KbCounterGranularity Then
FTPSym.Text1.Text = OriginalText & Format(Now(), "dd/mm/yy
hh:mm:ss") & " " & Str$(KbCounter) & "/" & Trim$(Str$(dlSize)) & " Ko lus" &
vbCrLf
FTPSym.CLProgressBar1.Progress 100 * KbCounter / dlSize
GranularityCounter = 0
DoEvents
End If
If (Not CBool(lNumberOfBytesRead)) Or CloseRequest Then bDoLoop > False
Wend

FTPSym.Text1.Text = OriginalText & Format(Now(), "dd/mm/yy hh:mm:ss") &
" " & Str$(KbCounter) & " Ko lus" & vbCrLf
FTPSym.CLProgressBar1.Visible = False

On Error GoTo 0
If Not CloseRequest Then
Open SaveAs For Binary Access Write As #1
Put #1, , sBuffer
Close #1
Else
Msg = vbCrLf & vbCrLf & " Download aborted"
EVLog.LogEvent "FTP Symantec", Msg, vbLogEventTypeWarning, 6

End If
If hOpenUrl <> 0 Then InternetCloseHandle (hOpenUrl)
If hOpen <> 0 Then InternetCloseHandle (hOpen)
Downloading = False
Exit Sub

Oops:
MsgBox Err.Description
EVLog.LogEvent "FTP Symantec", Err.Description, vbLogEventTypeError, 5

Set EVLog = Nothing
If hOpenUrl <> 0 Then InternetCloseHandle (hOpenUrl)
If hOpen <> 0 Then InternetCloseHandle (hOpen)

End Sub