Synapse and Javascript... lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- lazzaro2023 路 2025-01-03 08:30 Hi guys, I鈥檇 like to use Synapse to integrate REST APIs on a free hosting service. However, when I try to call a PHP file on the server, I get an error because the system requires JavaScript to be enabled in the browser. Is there a way to work around this issue? Thanks in advance for your help! 馃槉 ---------------------------------------------------------------------- egsuh 路 2025-01-03 08:51 It seems that the response PHP file contains javascript codes. If your Lazarus application is receiving the response from webserver I think there are no way to run the page correctly. ---------------------------------------------------------------------- rvk 路 2025-01-03 09:34 BTW. This has nothing to do with PHP itself. You, as a client, are not even aware the page is created by PHP. It could even have been created by FPC (The extension does not guarantee the language behind it) But yes, you need to dive into the javascript to see what it does to see if you can simulate that with FPC. Without knowing which service we can't do that for you. Normally the javascript (on client side) can talk to an API of that service so you need to use the Developer tools (F12 or CTRL+SHIFT+I) of your browser to see what URLS are actually called during execution of that javascript. ---------------------------------------------------------------------- lazzaro2023 路 2025-01-08 11:00 Thank you for your response. I understand that the page could be created by different languages and that the extension doesn't guarantee the language behind it. I have inspected the JavaScript using the Developer tools and found that the server responds with the following code: Quote 聽 聽 聽 聽 聽 聽 聽 聽 How can I circumvent this response to access the data I need? ---------------------------------------------------------------------- rvk 路 2025-01-08 11:13 Quote from: lazzaro2023 on January 08, 2025, 12:00:19 pmHow can I circumvent this response to access the data I need? The javascript is relatively simple. You see that there that it creates a cookie named __test: Code: Pascal 聽[Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 聽document.cookie = "__test=" + toHex(slowAES.decrypt(c, 2, a, b)) + "; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/";after that it simply redirects to http://example.rf.gd/phpcode.php?i=1 So all you need to do is recreate that javascript to create a cookie and pass it with you get of the HTTP page. BTW. I see the cookie expires in 2037 so you might even get away with just copying the value of an existing cookie (which you can find in the developer tools (CTRL+SHIFT+I) and pass that to http://example.rf.gd/phpcode.php?i=1 ---------------------------------------------------------------------- lazzaro2023 路 2025-01-08 11:31 I tried following your advice, but I'm still getting the same response. Here is the code I used: Quoteprogram SynapseExample; {$mode objfpc}{$H+} uses 聽 Classes, SysUtils, httpsend, ssl_openssl, synacode; var 聽 HTTPSender: THTTPSend; 聽 URL: string; 聽 Cookie: string; 聽 UserAgent: string; 聽 Response: TStringList; begin 聽 // URL of the page you want to access 聽 URL := 'http://example.rf.gd/?i=1'; 聽 // Cookie to set 聽 Cookie := '__test=a0b750cece14afcfc3e9484b2479758d'; 聽 // User-Agent to simulate a browser with JavaScript enabled 聽 UserAgent := 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'; 聽 // Create the HTTPSender object 聽 HTTPSender := THTTPSend.Create; 聽 try 聽 聽 // Set the User-Agent header 聽 聽 HTTPSender.Headers.Add('User-Agent: ' + UserAgent); 聽 聽 // Set the cookie 聽 聽 HTTPSender.Cookies.Add(Cookie); 聽 聽 // Send the HTTP GET request 聽 聽 if HTTPSender.HTTPMethod('GET', URL) then 聽 聽 begin 聽 聽 聽 // Read the response 聽 聽 聽 Response := TStringList.Create; 聽 聽 聽 try 聽 聽 聽 聽 Response.LoadFromStream(HTTPSender.Document); 聽 聽 聽 聽 WriteLn('Response: ', Response.Text); 聽 聽 聽 finally 聽 聽 聽 聽 Response.Free; 聽 聽 聽 end; 聽 聽 end 聽 聽 else 聽 聽 begin 聽 聽 聽 WriteLn('HTTP request error: ', HTTPSender.ResultCode, ' ', HTTPSender.ResultString); 聽 聽 end; 聽 finally 聽 聽 HTTPSender.Free; 聽 end; end. ---------------------------------------------------------------------- rvk 路 2025-01-08 11:32 Quote from: lazzaro2023 on January 08, 2025, 12:31:01 pmI tried following your advice, but I'm still getting the same response. Here is the code I used: You still used the wrong URL http://example.rf.gd/?i=1%27; In the javascript code you see there is a redirect to http://example.rf.gd/phpcode.php?i=1 So you need to get that page with the cookie set. ---------------------------------------------------------------------- lazzaro2023 路 2025-01-08 12:12 I corrected it, but the response is still the same. ---------------------------------------------------------------------- rvk 路 2025-01-08 12:15 Quote from: lazzaro2023 on January 08, 2025, 01:12:21 pmI corrected it, but the response is still the same. Well, you didn't even mention what the actual result was so I don't know. Without actual URL we can't even test this ourselves. So there you are on your own. My only advice is to use the Developer tools and look if there is something else passed on when doing this in the browser. You need to mimicks this exactly. It's also possible the cookie is only valid 1 time. In that case you would need to create the cookie yourself with exactly the same code (converted to FPC). ---------------------------------------------------------------------- rvk 路 2025-01-08 12:17 BTW looking at that javascript it seems to be similar to this: https://github.com/HakkanR/slowAES So maybe you have some use with that page. ---------------------------------------------------------------------- lazzaro2023 路 2025-01-09 06:44 Thanks for the heads-up!聽 I checked out the link you shared, but the library is written in C.聽 I tried following the suggestions on GitHub, but I'm not getting the expected values from the slowAes decrypt.聽 I used DCPcrypt, but the decrypted value doesn't match.聽 If anyone wants to help, here's the link: http://synapse.rf.gd/test.php.聽 test.php outputs "Hello world." ---------------------------------------------------------------------- egsuh 路 2025-01-09 07:15 The server seems to check whether your browser, in this case, HTTPSender, can process javascript first. When you type in http://example.rf.gd/?i=1 at browser, you'll see the normal page. But when I called the link from my synapse example, I got the response that you posted. Don't know how the (PHP) server operates. ---------------------------------------------------------------------- rvk 路 2025-01-09 08:05 Quote from: lazzaro2023 on January 09, 2025, 07:44:24 amIf anyone wants to help, here's the link: http://synapse.rf.gd/test.php.聽 test.php outputs "Hello world." Eye... that was a tricky basterd聽 I tried a few things. Ended up seeing if it would work with curl and indeed that worked. Code: Bash 聽[Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i curl -v -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36" -H "Cookie: __test=9755fad6fdef4cf5dec0f7c231a4e4b1" 聽http://synapse.rf.gd/test.php Eventually I found out that site really really really doesn't like any variation in User-Agent. So if you give it an exactly correct user-agent... it should work: BTW. Once you have a cookie value, you don't need the ?i=1 anymore. I'm not sure if that same cookie can be used with different variations of that site and if it keep being valid. Code: Pascal 聽[Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program synapseexample;聽{$mode objfpc}{$H+}聽uses聽 Classes, SysUtils, httpsend, synacode;聽var聽 HTTPSender: THTTPSend;聽 URL: string;聽 Response: TStringList;begin聽 URL := 'http://synapse.rf.gd/test.php';聽聽 // URL := 'http://httpbin.org/get';聽聽 HTTPSender := THTTPSend.Create;聽 try聽 聽 HTTPSender.UserAgent := 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36';聽 聽 HTTPSender.Headers.Add('Cookie: __test=9755fad6fdef4cf5dec0f7c231a4e4b1');聽聽 聽 if HTTPSender.HTTPMethod('GET', URL) then聽 聽 begin聽 聽 聽 Response := TStringList.Create;聽 聽 聽 try聽 聽 聽 聽 Response.LoadFromStream(HTTPSender.Document);聽 聽 聽 聽 WriteLn('Response: ', Response.Text);聽 聽 聽 finally聽 聽 聽 聽 Response.Free;聽 聽 聽 end;聽 聽 end聽 聽 else聽 聽 begin聽 聽 聽 WriteLn('HTTP request error: ', HTTPSender.ResultCode, ' ', HTTPSender.ResultString);聽 聽 end;聽 finally聽 聽 HTTPSender.Free;聽 end;聽 readln;end. ---------------------------------------------------------------------- egsuh 路 2025-01-09 11:59 Quote聽 聽 HTTPSender.UserAgent := 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36'; 聽 聽 HTTPSender.Headers.Add('Cookie: __test=9755fad6fdef4cf5dec0f7c231a4e4b1'); Wow. This is a great finding. ---------------------------------------------------------------------- lazzaro2023 路 2025-01-09 14:18 Thanks for the tip. Awesome!!!"