ALPN or Application Layer Protocol Name is a TLS extension that includes the protocol negotiation within the exchange of hello messages. ALPN is able to negotiate which protocol should be handled over a secure connection in a way that is more efficient and avoids additional round trips. The ever-growing in popularity HTTP/2 protocol, makes use of ALPN to further decrease website load times and encrypt connections faster.
Zgodnie ze specyfikacją RFC 7301, dzięki ALPN klient wysyła listę obsługiwanych protokołów aplikacji do serwera jako część wiadomości TLS ClientHello. Serwer wybiera protokół i odsyła go w wiadomości ServerHello. Negocjacja protokołu aplikacji może zatem zostać przeprowadzona w jednym obiegu w ramach handshake TLS. Metoda ta pozwala również serwerowi powiązać różne certyfikaty z różnymi protokołami aplikacji.
Od sgcWebSockets 4.3.2, gdy kompilujesz sgcWebSockets z naszą niestandardową biblioteką Indy, możesz korzystać z protokołu ALPN. Domyślnie Indy nie implementuje tego protokołu.
Client
Utwórz nowego klienta WebSocket, który wymaga "h2" dla ALPN, a po połączeniu sprawdź, który protokół zaakceptował serwer.
oClient := TsgcWebSocketClient.Create(nil);
oClient.Host := '127.0.0.1';
oClient.Port := 443;
oClient.TLS := True;
oClient.TLSOptions.ALPNProtocols.Add('h2');
oClient.Active := True;
procedure OnClientConnect(Connection: TsgcWSConnection);
var
vProtocol: string;
begin
vProtocol := TsgcWSConnectionClient(Connection).ALPNProtocol;
end;
Server
Create a new websocket client which checks if client supports "h2" ALPN connections.
oServer := TsgcWebSocketServer.Create(nil);
oServer.Port := 443;
oServer.SSL := True;
oServer.Active := True;
procedure OnServerSSLALPNSelect(Sender: TObject; aProtocols: TStringList; var aProtocol: string);
begin
if aProtocols.IndexOf('h2') > -1 then
aProtocol := 'h2';
end;
