Delphi TURN Server
Run your own TURN (RFC 8656) server in Delphi. Allocate relayed transport addresses for clients that cannot establish a peer-to-peer path, with long-term credentials and per-allocation quotas.
Run your own TURN (RFC 8656) server in Delphi. Allocate relayed transport addresses for clients that cannot establish a peer-to-peer path, with long-term credentials and per-allocation quotas.
A self-hosted TURN server — processes Allocate / CreatePermission / ChannelBind / Send / Refresh / Data, manages allocations and their lifetime, validates long-term credentials.
TsgcTURNServer
Windows, macOS, Linux, iOS, Android
Enterprise
Set Port and the realm on TURNOptions.Authentication.LongTermCredentials, supply per-user passwords via OnSTUNRequestAuthorization, Active := True — your server now allocates relays for any RFC 8656 client.
uses
sgcP2P, sgcP2P_STUN_Classes;
// TFormTURNServer holds FServer: TsgcTURNServer and the handler
procedure TFormTURNServer.StartServer;
begin
FServer := TsgcTURNServer.Create(nil);
FServer.Port := 3478;
FServer.TURNOptions.Authentication.Enabled := True;
FServer.TURNOptions.Authentication.LongTermCredentials.Enabled := True;
FServer.TURNOptions.Authentication.LongTermCredentials.Realm :=
'turn.example.com';
// bound the abuse: allocations per user and maximum lifetime
FServer.TURNOptions.Allocation.MaxUserAllocations := 4;
FServer.TURNOptions.Allocation.MaxLifeTime := 3600;
FServer.OnSTUNRequestAuthorization := OnSTUNRequestAuthorization;
FServer.Active := True;
end;
procedure TFormTURNServer.OnSTUNRequestAuthorization(Sender: TObject;
const aRequest: TsgcSTUN_Message;
const aUsername, aRealm: string;
var Password: string);
begin
// return the password of that user, empty rejects the request
Password := LookupPassword(aUsername);
end;
// includes: sgcP2P.hpp, sgcP2P_STUN_Classes.hpp
TsgcTURNServer *Server = new TsgcTURNServer(this);
Server->Port = 3478;
Server->TURNOptions->Authentication->Enabled = true;
Server->TURNOptions->Authentication->LongTermCredentials->Enabled = true;
Server->TURNOptions->Authentication->LongTermCredentials->Realm = "turn.example.com";
Server->TURNOptions->Allocation->MaxUserAllocations = 4;
Server->OnSTUNRequestAuthorization = OnSTUNRequestAuthorization;
Server->Active = true;
A self-hosted relay — lets your WebRTC and ICE deployments stop depending on third-party TURN providers.
Honours Allocate requests with REQUESTED-TRANSPORT, picks a free relay port and tracks lifetime. Refresh extends the allocation; the server tears it down on expiry.
Tracks per-allocation peer permissions and channel bindings. Drops Send/Data frames for peers that have not been permissioned per RFC 8656 §9.
Both 36-byte Send / Data envelopes and 4-byte ChannelData frames are supported. The relay forwards datagrams between the relayed-transport-address and the bound peer.
TURNOptions.Authentication.LongTermCredentials carries the realm, and OnSTUNRequestAuthorization hands you the username and that realm so you return the password. The server validates MESSAGE-INTEGRITY and rotates nonces itself.
TURNOptions.Allocation.MaxUserAllocations bounds how many relays one user gets, DefaultLifeTime and MaxLifeTime bound how long each lives, and MinPort / MaxPort / RelayIP pin the relay range. A user over the limit gets 486 Allocation Quota Reached.
OnTURNBeforeAllocate gives you the requesting address with a var Reject, and OnTURNBeforeRelayIndication and OnTURNBeforeRelayChannelData do the same for every relayed datagram. OnTURNCreateAllocation and OnTURNDeleteAllocation track the ones that live.
Deep-link to the component reference, grab the ready-to-run demo project, and download the trial.
| Online Help — TsgcTURNServer Full property, method and event reference for this component. | Open | |
| Demo Project — Demos\35.P2P\03.TURN Ready-to-run example project. Ships inside the sgcWebSockets package — download the trial below. | Open | |
| Technical Document (PDF) Features, quick start, code samples for Delphi & C++ Builder and primary-source references — this component only. | Open | |
| User Manual (PDF) Comprehensive manual covering every component in the library. | Open |
Common questions about running a self-hosted TURN relay in Delphi and C++ Builder.
TsgcTURNServer component, set Port and Realm, supply per-user passwords from the OnSTUNRequestAuthorization event, then set Active := True. The server then handles Allocate, CreatePermission, ChannelBind, Send, Refresh and Data for any RFC 8656 client, relaying datagrams between the relayed transport address and the bound peer.TsgcTURNServer is a self-hosted relay you embed in your own Delphi or C++ Builder application, so your WebRTC and ICE deployments can stop depending on third-party TURN providers or a separate coturn install. Per-allocation quotas (max allocations per user, bandwidth and lifetime) bound abuse, with OnQuotaExceeded reporting rejections.