TsgcTURNServer is the server that implements the TURN protocol and allows to process requests from TURN clients. The component inherits from TsgcSTUNServer, so all methods and properties are available on TsgcTURNServer.
TURN Server supports Long-Term Authentication, Allocation, Permissions, Channel Data and more.
Usually TURN servers runs on UDP port 3478 and require Long-Term credentials, so in order to configure a TURN server, set the listening port (by default 3478) and start the server.
Configure the server
Set the property Active = True to start the STUN server.
oTURN := TsgcTURNServer.Create(nil);
oTURN.Port := 3478;
oTURN.TURNOptions.Authentication.Enabled := True;
oTURN.TURNOptions.Authentication.LongTermCredentials.Enabled := True;
oTURN.TURNOptions.Authentication.LongTermCredentials.Realm := 'esegece.com';
oTURN.Active := True;
procedure OnSTUNRequestAuthorization(Sender: TObject; const aRequest: TsgcSTUN_Message;
const aUsername, aRealm: string; var Password: string);
begin
if (aUsername = 'user') and (aRealm = 'esegece.com') then
Password := 'password';
end;
Configurations
Allocations
Active: set the property to True to Start the TURN server and set to False to Stop the Server.
Host: it's the IP Address or DNS name of TURN server.
Port: it's the listening port of TURN server, by default 3478.
IPVersion: it's the Family Address, by default IPv4.
STUNOptions: here are defined the specific options for STUN Requests
Fingerprint: if enabled, the message includes a fingerprint that aids to identify STUN messages from packets of other protocols when the two are multiplexed on the same transport address.
Software: if enabled, sends an attribute with the name of the software being used by the server.
Authentication: here you can configure if the server requires Authentication requests to send binding responses.
Enabled: set to True if the server requires Authentication requests, by default false.
LongTermCredentials: Enable if the server supports Long-Term credentials. The long-term credential mechanism relies on a long-term credential, in the form of a username and password that are shared between client and server.
Enabled: set to True if the server requires Long-Term credentials.
Realm: the string of the realm sent to client.
StaleNonce: time in seconds after the nonce is no longer valid.
BindingAttributes: when the server sends a successful response after a binding request, here you can customize which attributes will be sent to the client.
OtherAddress: if enabled and the server binds to more than one address, this attribute will be sent with all other addresses except the default one.
ResponseOrigin: is the Local IP of the server to send the response.
SourceAddress: is the Local IP of the server to send the response.
TURNOptions: here are defined the specific options for TURN Requests
Fingerprint: if enabled, the message includes a fingerprint that aids to identify TURN messages from packets of other protocols when the two are multiplexed on the same transport address.
Software: if enabled, sends an attribute with the name of the software being used by the server.
Allocation: when a new allocation is created, the server takes from this property the default values.
DefaultLifeTime: value in seconds of default LifeTime.
MaxLifeTime: max value of LifeTime, if a TURN client requests a value greater of this value, the value returned will be the MaxLifeTime.
MaxUserAllocations: max number of allocations.
MinPort: Minimum range port of allocations.
MaxPort: Maximum range port of allocations.
RelayIP: if defined, this will be the Relayed IP Address.
Authentication: usually TURN servers require Long-Term Credentials authentication.
Enabled: set to True if the server requires Authentication requests, by default false.
LongTermCredentials: Enable if the server supports Long-Term credentials. The long-term credential mechanism relies on a long-term credential, in the form of a username and password that are shared between client and server.
Enabled: set to True if the server requires Long-Term credentials.
Realm: the string of the realm sent to client.
StaleNonce: time in seconds after the nonce is no longer valid.
LogFile: if enabled save stun messages to a specified log file, useful for debugging.
Enabled: if enabled every time a message is received and sent by server it will be saved on a file.
FileName: full path to the filename.
NotifyEvents: defines which mode to notify the events.
neAsynchronous: this is the default mode, notify threaded events on asynchronous mode, adds events to a queue that are synchronized with the main thread asynchronously.
neSynchronous: if this mode is selected, notify threaded events on synchronous mode, needs to synchronize with the main thread to notify these events.
neNoSync: there is no synchronization with the main thread, if you need to access to controls that are not thread-safe, you need to implement your own synchronization methods.
The TURN server inherits from STUN Server the events: OnSTUNRequestAuthorization, OnSTUNRequestSuccess, OnSTUNRequestError and OnSTUNException.
Additionally, includes the following events to handle all TURN messages.
OnTURNBeforeAllocate
The event is called before create a new Allocation. It provides the IP Address and Port used to Relay Data, you can reject if don't want to accept the Allocation.
OnTURNCreateAllocation
The event is called after creating successfully an Allocation.
OnTURNDeleteAllocation
The event is called after remove an already created Allocation.
OnTURNMessageDiscarded
The event is called when a message received by server is discarded.
OnTURNChannelDataDiscarded
The event is called when a Channel Data message is discarded.
OnTURNBeforeRelayIndication
Event fired when the server receives an indication that must be relayed to other peer, you can use this method to intercept the bytes sent to the peer (to capture audio/video for example).
OnTURNBeforeRelayChannelData
Event fired when the server receives a channel data message that must be relayed to other peer, you can use this method to intercept the bytes sent to the peer (to capture audio/video for example).