HTTPS Login retrieve source problem

Just starting out? Need help? Post your questions and find answers here.
infratec
Always Here
Always Here
Posts: 7619
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: HTTPS Login retrieve source problem

Post by infratec »

Modified to work with the new libcurl.pbi version and PB 6.12:

Code: Select all

EnableExplicit

#NoMansSkyModsURL$ = "https://nomansskymods.com"

#User$ = "blabla"
#Passwd$ = "blublu"

Structure DownloadInfoStructure
  FileURL$
  _wpnonce$
  id$
  shortcodeId$
  backurl$
EndStructure


Global ReceivedData.s

IncludeFile "libcurl.pbi"


Procedure Dump(Text$, *Data, Size.i)
  
  Protected.i File, i, c, Byte
  Protected Line$
  
  
  File = OpenFile(#PB_Any, "c:\tmp\libcurl_debug.txt", #PB_File_Append)
  If File
    
    WriteStringN(File, Text$)
    WriteStringN(File, "")
    
    For i = 0 To size - 1 Step 16
      
      Line$ = ""
      
      For c = 0 To 15
        If i + c < size
          Line$ + RSet(Hex(PeekA(*Data + i + c), #PB_Byte), 2, "0") + " "
        Else
          Line$ + "   "
        EndIf
      Next c
      
      For c = 0 To 15
        If i + c < size
          Byte = PeekA(*Data + i + c)
          If Byte < ' '
            Line$ + "."
          Else
            Line$ + Chr(Byte)
          EndIf
        Else
          Line$ + "   "
        EndIf
      Next c
      
      WriteStringN(File, Line$)
      
    Next i
    
    WriteStringN(File, "")
    
    CloseFile(File)
  EndIf
  
EndProcedure




ProcedureC.l my_trace(*handle, type.l, *Data, size.l, *userp)
  
  Protected Result.l
  
  
  Select type
    Case #CURLINFO_TEXT
      Debug "== Info: " + PeekS(*Data, size, #PB_UTF8)
      
    Case #CURLINFO_HEADER_OUT
      Dump("Send header", *Data, size)
      
    Case #CURLINFO_DATA_OUT
      Dump("Send Data", *Data, size)
      
    Case #CURLINFO_SSL_DATA_OUT
      Dump("Send SSL Data", *Data, size)
      
    Case #CURLINFO_HEADER_IN
      Dump("Recv header", *Data, size)
      
    Case #CURLINFO_DATA_IN
      Dump("Recv data", *Data, size)
      
    Case #CURLINFO_SSL_DATA_IN
      Dump("Recv SSL data", *Data, size)
      
  EndSelect
  
  ProcedureReturn Result
  
EndProcedure


Procedure.i NoMansSkyModsLogin(User$, Passwd$, CookieFilename$)
  
  Protected Result.i, curl.i, Post$, Result$, *Post
  Protected UserData.libcurl_userdata_structure
  
  
  curl  = curl_easy_init()
  If curl
    
    Post$ = "log=" + #User$ + "&"
    Post$ + "pwd=" + #Passwd$ + "&"
    Post$ + "wp-submit=Log+In&"
    Post$ + "redirect_to=" + #NoMansSkyModsURL$ + "/login/&"
    Post$ + "wppb_login=true&"
    Post$ + "wppb_form_location=page&"
    Post$ + "wppb_request_url=" + #NoMansSkyModsURL$ + ":433/login/&"
    Post$ + "wppb_lostpassword_url=&"
    Post$ + "wppb_redirect_priority=&"
    Post$ + "wppb_referer_url="
    
    Debug Post$
    Debug ""
    
    
    
    curl_easy_setopt_str(curl, #CURLOPT_URL, #NoMansSkyModsURL$ + "/wp-login.php")
    curl_easy_setopt(curl, #CURLOPT_POST, 1)
    ;curl_easy_setopt(curl, #CURLOPT_POSTFIELDS, str2curl(Post$))
    
    *Post = UTF8(Post$) ; needed because it has to be available over the complete time
    curl_easy_setopt(curl, #CURLOPT_POSTFIELDS, *Post)
    
    curl_easy_setopt(curl, #CURLOPT_SSL_VERIFYPEER, 0)
    curl_easy_setopt(curl, #CURLOPT_SSL_VERIFYHOST, 0)
    curl_easy_setopt(curl, #CURLOPT_HEADER, @"")
    curl_easy_setopt(curl, #CURLOPT_WRITEFUNCTION, @LibCurl_WriteFunction())
    curl_easy_setopt(curl, #CURLOPT_WRITEDATA, @UserData)
    
    curl_easy_setopt_str(curl, #CURLOPT_COOKIEJAR, CookieFilename$)
    
    curl_easy_setopt_str(curl, #CURLOPT_REFERER, #NoMansSkyModsURL$ + "/login/")
    
    ; the following 2 lines are needed for debugging
    ;curl_easy_setopt(curl, #CURLOPT_DEBUGFUNCTION, @my_trace())
    ;curl_easy_setopt(curl, #CURLOPT_VERBOSE, 1)
    
    Result = curl_easy_perform(curl)
    If Result = #CURLE_OK
      Result$ = PeekS(UserData\Memory, MemorySize(UserData\Memory), #PB_UTF8|#PB_ByteLength)
      
      ;Debug Result$
      
      If FindString(Result$, "user_login")
        Result = #False
      Else
        Result = #True
      EndIf
    Else
      Result = #False
    EndIf
    
    If UserData\Memory
      FreeMemory(UserData\Memory)
    EndIf
    
    curl_easy_cleanup(curl)
    
    FreeMemory(*Post)
  EndIf
  
  ProcedureReturn Result
  
EndProcedure




Procedure.s GetNoMansSkyModsDownloadPage(Url$, CookieFilename$, *DownloadInfo.DownloadInfoStructure)
  
  Protected Result$, curl.i, Result.i, Pos1.i, Pos2.i, Pos3.i, Pos4.i
  Protected UserData.libcurl_userdata_structure
  
  
  curl  = curl_easy_init()
  If curl
    
    curl_easy_setopt_str(curl, #CURLOPT_URL, Url$)
    
    curl_easy_setopt(curl, #CURLOPT_SSL_VERIFYPEER, 0)
    curl_easy_setopt(curl, #CURLOPT_SSL_VERIFYHOST, 0)
    curl_easy_setopt(curl, #CURLOPT_HEADER, @"")
    curl_easy_setopt(curl, #CURLOPT_WRITEFUNCTION, @LibCurl_WriteFunction())
    curl_easy_setopt(curl, #CURLOPT_WRITEDATA, @UserData)
    
    curl_easy_setopt_str(curl, #CURLOPT_COOKIEFILE, CookieFilename$)
        
    Result = curl_easy_perform(curl)
    If Result = #CURLE_OK
      Result$ = PeekS(UserData\Memory, MemorySize(UserData\Memory), #PB_UTF8|#PB_ByteLength)
      If FindString(Result$, " 200 OK")
        Pos2 = FindString(Result$, ".zip")
        If Pos2
          Pos1 = Pos2
          Pos2 + 4
          While Mid(Result$, Pos1, 1) <> #DQUOTE$ And Pos1 > 0
            Pos1 - 1
          Wend
          If Pos1
            Pos1 + 1
            *DownloadInfo\FileURL$ = Mid(Result$, Pos1, Pos2 - Pos1)
            
            Pos3 = FindString(Result$, "_wpnonce", Pos2)
            If Pos3
              Pos3 + 17
              Pos4 = FindString(Result$, #DQUOTE$, Pos3)
              *DownloadInfo\_wpnonce$ = Mid(Result$, Pos3, Pos4 - Pos3)
            EndIf
            
            Pos3 = FindString(Result$, #DQUOTE$ + "id" + #DQUOTE$, Pos2)
            If Pos3
              Pos3 + 12
              Pos4 = FindString(Result$, #DQUOTE$, Pos3)
              *DownloadInfo\id$ = Mid(Result$, Pos3, Pos4 - Pos3)
            EndIf
            
            Pos3 = FindString(Result$, "shortcodeId", Pos2)
            If Pos3
              Pos3 + 20
              Pos4 = FindString(Result$, #DQUOTE$, Pos3)
              *DownloadInfo\shortcodeId$ = Mid(Result$, Pos3, Pos4 - Pos3)
            EndIf
            
            Pos3 = FindString(Result$, "backurl", Pos2)
            If Pos3
              Pos3 + 16
              Pos4 = FindString(Result$, #DQUOTE$, Pos3)
              *DownloadInfo\backurl$ = Mid(Result$, Pos3, Pos4 - Pos3)
            EndIf
            
          Else
            Result$ = ""
          EndIf
        Else
          Result$ = ""
        EndIf
      Else
        Result$ = ""
      EndIf
    Else
      Debug "BKK"
    EndIf
    
    If UserData\Memory
      FreeMemory(UserData\Memory)
    EndIf
    
    curl_easy_cleanup(curl)
  EndIf  
  
  ProcedureReturn *DownloadInfo\FileURL$
  
EndProcedure




ProcedureC.l curlWriteFile(*ptr, Size, NMemB, *Stream)
  
  ProcedureReturn WriteData(*Stream, *ptr, Size * NMemB)
  
EndProcedure




Procedure.i GetNoMansSkyModsFile(URL$, SaveAs$, CookieFilename$, *DownloadInfo.DownloadInfoStructure)
  
  Protected Result.i, Curl.i, File.i, Post$, *Post
  
  File = CreateFile(#PB_Any, SaveAs$)
  If File
    curl  = curl_easy_init()
    If curl
      
      curl_easy_setopt_str(curl, #CURLOPT_URL, Url$)
      
      Post$ = "_wpnonce=" + *DownloadInfo\_wpnonce$ + "&"
      Post$ + "id=" + *DownloadInfo\id$ + "&"
      Post$ + "shortcodeID=" + *DownloadInfo\shortcodeId$ + "&"
      Post$ + "backrl=" + *DownloadInfo\backurl$
      
      *Post = UTF8(Post$) ; needed because it has to be available over the complete time
      curl_easy_setopt(curl, #CURLOPT_POSTFIELDS, *Post)
      
      curl_easy_setopt(curl, #CURLOPT_SSL_VERIFYPEER, 0)
      curl_easy_setopt(curl, #CURLOPT_SSL_VERIFYHOST, 0)
      curl_easy_setopt(curl, #CURLOPT_HEADER, @"")
      curl_easy_setopt(curl, #CURLOPT_WRITEFUNCTION, @curlWriteFile())
      
      curl_easy_setopt_str(curl, #CURLOPT_COOKIEFILE, CookieFilename$)
      
      curl_easy_setopt(curl, #CURLOPT_WRITEDATA, File)
      
      Result = curl_easy_perform(curl)
      If Result = #CURLE_OK
        Result = #True
      Else
        Result = #False
      EndIf
      
      curl_easy_cleanup(curl)
      
      FreeMemory(*Post)
    EndIf
    
    CloseFile(File)
  EndIf
  
  ProcedureReturn Result
  
EndProcedure



;-Main
Define CookieFilename$, FileURL$
Define DownloadInfo.DownloadInfoStructure



CookieFilename$ = GetTemporaryDirectory() + "NoMansSkyModsCookies.txt"
Debug "Cookiefile:" + CookieFilename$
Debug ""

If NoMansSkyModsLogin(#User$, #Passwd$, CookieFilename$)
  
  Debug ""
  Debug "Login Ok"
  Debug ""
  
  FileURL$ = GetNoMansSkyModsDownloadPage(#NoMansSkyModsURL$ + "/mods/no-mans-sky-save-editor/", CookieFilename$, @DownloadInfo)
  If FileURL$ <> ""
;     Debug "c:\tmp\" + GetFilePart(FileURL$)
;     
;     Debug "_wpnonce: " + DownloadInfo\_wpnonce$
;     Debug "id: " + DownloadInfo\id$
;     Debug "ShortCodeID: " + DownloadInfo\shortcodeId$
;     Debug "BackURL: " + DownloadInfo\backurl$
    If GetNoMansSkyModsFile(FileURL$, "c:\tmp\" + GetFilePart(FileURL$), CookieFilename$, @DownloadInfo)
      Debug "File receive Ok"
    EndIf
  EndIf
  
EndIf

If FileSize(CookieFilename$) > 0
  If DeleteFile(CookieFilename$)
    Debug "Cookies deleted"
  EndIf
EndIf
User avatar
le_magn
Enthusiast
Enthusiast
Posts: 281
Joined: Wed Aug 24, 2005 12:11 pm
Location: Italia

Re: HTTPS Login retrieve source problem

Post by le_magn »

Thank you very much Infratek, login OK, start to download file, but resulting file is 0byte, probably by different data send by site(is not the same of example), any suggestion on what to edit based on the response the site gives me with the browser debugger?
I have modified file type from .zip to .rar(before it not start download and not create any file, now file of 0bytes)

SOLVED:
File url return only file name not full link, if i give it the full download link it work, LOGIN and Download ok!!!
Image
Post Reply