How to Connect to a VPN Server with Delphi?

Mahmood_N picture Mahmood_N · Apr 10, 2011 · Viewed 8.1k times · Source

I need to connect to a VPN Server , I can`t use windows Connections , My Application should work independently !

I tested some Components using RAS Api , they works ! but by using windows connections .

how can i do that without any dependency to windows connections ?

Answer

Cosmin Prund picture Cosmin Prund · Apr 11, 2011

The problem with this question

"VPN" stands for "Virtual Private Network". It's a way to make a private network available to your computer, possibly in a secure way, so your computer can use standard IP protocols as if it were physically connected to the private network.

The operating system needs to know about that network, so of course all VPN implementations use "windows connections". From a different perspective: When you're connected to a VPN you can open a TCP connection to an IP on the private network as if it were on your local network. Since it's the operating system's job to set up your TCP connection and route your TCP/IP packets, of course it needs to know about the VPN! If it doesn't, it'll simply forward all your requests for the given IP to it's default router and fail with a "no route to destination" message (or a "time out", if your router is not kind enough to tell your system it has no idea what the private IP is).

Can it be done?

From a theoretical point of view, of course, you can bypass Windows completely, but then you'll have to "roll your own" everything. You can't use the Windows IP services, you'll have to implement your own TCP. I'm sure there are about a million other little things that need re-implementing.

For a starting point I'd look at the Open VPN: it's Open Source and available for Windows. It uses the UDP protocol as the bases for the VPN implementation, unlike the Windows VPN (that one uses GRE - General Routing Encapsulation, protocol 47). Open VPN itself, of course, uses a "windows connection" to do it's job, because it aims to provide a useful service, but you can use the source code as the bases for your own implementation.

I personally wouldn't even think about doing this, I'm just showing you the way and proving it's possible.

What should be done

I assume you want some kind of secure communication channel to your own service. Look into simple secure connections, tunneling protocols and proxies.

If this needs to be done for one service on one server, I'd look into a simple SSL implementation. Even better, look into using HTTPS.

If you need to access many different services on possibly different servers on the given private network I'd look into proxies.