Recent

Author Topic: Detecting a vpn  (Read 1472 times)

madref

  • Hero Member
  • *****
  • Posts: 1082
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Detecting a vpn
« on: September 24, 2023, 04:26:34 pm »
I using the following code to send an email.
Code: Pascal  [Select][+][-]
  1. function VerzendMail (Const MailAan, Onderwerp: String; Inhoud: TStrings): Boolean;
  2. Var Mime : TMimeMess;
  3.     P : TMimePart;
  4.     B : Boolean;
  5. begin
  6.   Mime:=TMimeMess.Create;
  7.   try
  8.     // Zet enkele headers van het bericht.
  9.     Mime.Header.ToList.Text := MailAan;
  10.     Mime.Header.ReplyTo := SenderName + '<' + SenderEMail + '>';
  11.     Mime.Header.Subject := Onderwerp;
  12.     Mime.Header.From := SenderName + '<' + SenderEMail + '>';
  13.     // Maak een multipart deel aan.
  14.     P := Mime.AddPartMultipart('mixed',Nil);
  15.     // Als eerste deel de tekst van de mail toevoegen.
  16.     Mime.AddPartHTML (Inhoud,P);
  17.     // Alle bijlagen toevoegen.
  18. //    Mime.AddPartBinaryFromFile (Bijlage,P);
  19.     // Na deze code is het MIME bericht klaar. Het bericht dat verzonden
  20.     // moet worden, kan nu samengesteld worden door EncodeMessage op te roepen,
  21.     // en met SendToRaw te versturen:
  22.     // Bericht samenstellen
  23.     Mime.EncodeMessage;
  24.     // En verzenden met SendToRaw
  25.     B := SendToRaw(SenderName + '<' + SenderEMail + '>', MailAan,
  26.               smtpHost, Mime.Lines, smtpUser, smtpPassword);
  27.     Result := B;
  28.   finally
  29.     Mime.Free;
  30.   end;
  31. end;     // VerzendMail
  32.  


But when I am using a vpn the mail is not send and te app stops


How can I detect the vpn and created an 'error message' OR how can I send the email while using the vpn?
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Main Platform:
--------------
Mac OS X Sonoma 14.7
Lazarus 3.99 (Lazarus 3.99 (rev main_3_99-2668-g6b352d830e) FPC 3.3.1 x86_64-darwin-cocoa)

Windows 10 Pro
Lazarus 3.99 (rev cbfd80ce39)

alpine

  • Hero Member
  • *****
  • Posts: 1303
Re: Detecting a vpn
« Reply #1 on: September 24, 2023, 04:44:24 pm »
*snip*
But when I am using a vpn the mail is not send and te app stops


How can I detect the vpn and created an 'error message' OR how can I send the email while using the vpn?
You should provide more details what is exactly the error and why the app stops.
Perhaps your smtp host just can't be resolved?
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Leledumbo

  • Hero Member
  • *****
  • Posts: 8776
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Detecting a vpn
« Reply #2 on: September 25, 2023, 06:00:07 am »
There's no straightforward way. After all, VPN is designed to be an unknown middleman between the client and the server. It will defeat the purpose of a VPN if it can be easily detected. There are some escapes, but they have no guarantee.

One relatively easy way is to check your own public IP, as VPN will try to disguise, it's very likely to have an uncommon IP than the one your ISP assigns. You can hit https://ifconfig.me/ip to get it, which will give you a text based response like this:
Code: Text  [Select][+][-]
  1. 103.28.116.101
this method is agnostic to the VPN service, but ISPs like to use dynamic public IP and if the VPN server is in the same country, it might be tough to check.

The second method is easier, but requires knowledge of specific VPN service. NordVPN, for example, can be easily queried:
Code: Bash  [Select][+][-]
  1. $ nordvpn status
  2. Status: Disconnected
A single line of SysUtils.ExecuteProcess is sufficient. But then again, it has to be adapted to whatever VPN service you use and ensure it has a tool that can give similar output.

There are more ways, but they're all more difficult to do, so I suggest just the two above.
« Last Edit: September 25, 2023, 06:04:29 am by Leledumbo »

rvk

  • Hero Member
  • *****
  • Posts: 6591
Re: Detecting a vpn
« Reply #3 on: September 25, 2023, 07:47:08 am »
How can I detect the vpn and created an 'error message' OR how can I send the email while using the vpn?
You are focusing on the wrong thing.
There is no problem sending an e-mail when on a vpn (at least not for me).

It just happens that you have a problem sending e-mail when you are an a vpn but that's not the main cause. So you can program in detection but that's not going to help you (it wil in your specific case but it will also prevent sending mail when on a vpn where it does work).

So focus on the real reason your sending fails and handle that. It will make your program more stable because then it will also handle other exceptions where mail might fail.

So give more info about the crash. If it doesn't crash but just stops try to run the app in the IDE. It will give you a more clear error when debugging (and on what place in your program).

af0815

  • Hero Member
  • *****
  • Posts: 1379
Re: Detecting a vpn
« Reply #4 on: September 25, 2023, 07:57:34 am »
A simple answer - store the mail, check if the mailserver is reachable before you send, if not say this problem happens to the user. And ask him for resend, if the mailserver is reachable.
regards
Andreas

madref

  • Hero Member
  • *****
  • Posts: 1082
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Detecting a vpn
« Reply #5 on: September 27, 2023, 12:31:51 pm »
I let the program run for 60 seconds and I finally got an error code and I already was so smart to generate a fault message.
Which error I got I don't know because the SendToRaw generates it. But I will investigate it further
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Main Platform:
--------------
Mac OS X Sonoma 14.7
Lazarus 3.99 (Lazarus 3.99 (rev main_3_99-2668-g6b352d830e) FPC 3.3.1 x86_64-darwin-cocoa)

Windows 10 Pro
Lazarus 3.99 (rev cbfd80ce39)

 

TinyPortal © 2005-2018