blob: 4070372db267080974656188b6fc3e4aa7d84b1a [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
2 * Copyright 2012 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef P2P_BASE_TEST_TURN_SERVER_H_
12#define P2P_BASE_TEST_TURN_SERVER_H_
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000013
Niels Möllerac9a2882021-10-20 15:25:09 +020014#include <memory>
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000015#include <string>
Niels Möllerac9a2882021-10-20 15:25:09 +020016#include <utility>
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000017#include <vector>
18
Ali Tofighde2ac5a2022-06-30 11:58:26 +020019#include "absl/strings/string_view.h"
Artem Titovd15a5752021-02-10 14:31:24 +010020#include "api/sequence_checker.h"
Patrik Höglund56d94522019-11-18 15:53:32 +010021#include "api/transport/stun.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "p2p/base/basic_packet_socket_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "p2p/base/turn_server.h"
24#include "rtc_base/async_udp_socket.h"
25#include "rtc_base/ssl_adapter.h"
26#include "rtc_base/ssl_identity.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "rtc_base/thread.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000028
29namespace cricket {
30
31static const char kTestRealm[] = "example.org";
32static const char kTestSoftware[] = "TestTurnServer";
33
34class TestTurnRedirector : public TurnRedirectInterface {
35 public:
36 explicit TestTurnRedirector(const std::vector<rtc::SocketAddress>& addresses)
37 : alternate_server_addresses_(addresses),
Yves Gerey665174f2018-06-19 15:03:05 +020038 iter_(alternate_server_addresses_.begin()) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000039
40 virtual bool ShouldRedirect(const rtc::SocketAddress&,
41 rtc::SocketAddress* out) {
42 if (!out || iter_ == alternate_server_addresses_.end()) {
43 return false;
44 }
45 *out = *iter_++;
46 return true;
47 }
48
49 private:
50 const std::vector<rtc::SocketAddress>& alternate_server_addresses_;
51 std::vector<rtc::SocketAddress>::const_iterator iter_;
52};
53
54class TestTurnServer : public TurnAuthInterface {
55 public:
56 TestTurnServer(rtc::Thread* thread,
Niels Möller6dd49972021-11-24 14:05:55 +010057 rtc::SocketFactory* socket_factory,
Honghai Zhang80f1db92016-01-27 11:54:45 -080058 const rtc::SocketAddress& int_addr,
59 const rtc::SocketAddress& udp_ext_addr,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070060 ProtocolType int_protocol = PROTO_UDP,
61 bool ignore_bad_cert = true,
Ali Tofighde2ac5a2022-06-30 11:58:26 +020062 absl::string_view common_name = "test turn server")
Niels Möller6dd49972021-11-24 14:05:55 +010063 : server_(thread), socket_factory_(socket_factory) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070064 AddInternalSocket(int_addr, int_protocol, ignore_bad_cert, common_name);
Niels Möller9def9942021-09-07 09:16:49 +020065 server_.SetExternalSocketFactory(
Niels Möller6dd49972021-11-24 14:05:55 +010066 new rtc::BasicPacketSocketFactory(socket_factory), udp_ext_addr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000067 server_.set_realm(kTestRealm);
68 server_.set_software(kTestSoftware);
69 server_.set_auth_hook(this);
70 }
71
Sebastian Janssonc01367d2019-04-08 15:20:44 +020072 ~TestTurnServer() { RTC_DCHECK(thread_checker_.IsCurrent()); }
Seth Hampsonaed71642018-06-11 07:41:32 -070073
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000074 void set_enable_otu_nonce(bool enable) {
Sebastian Janssonc01367d2019-04-08 15:20:44 +020075 RTC_DCHECK(thread_checker_.IsCurrent());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000076 server_.set_enable_otu_nonce(enable);
77 }
78
Seth Hampsonaed71642018-06-11 07:41:32 -070079 TurnServer* server() {
Sebastian Janssonc01367d2019-04-08 15:20:44 +020080 RTC_DCHECK(thread_checker_.IsCurrent());
Seth Hampsonaed71642018-06-11 07:41:32 -070081 return &server_;
82 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000083
84 void set_redirect_hook(TurnRedirectInterface* redirect_hook) {
Sebastian Janssonc01367d2019-04-08 15:20:44 +020085 RTC_DCHECK(thread_checker_.IsCurrent());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000086 server_.set_redirect_hook(redirect_hook);
87 }
88
Taylor Brandstetteref184702016-06-23 17:35:47 -070089 void set_enable_permission_checks(bool enable) {
Sebastian Janssonc01367d2019-04-08 15:20:44 +020090 RTC_DCHECK(thread_checker_.IsCurrent());
Taylor Brandstetteref184702016-06-23 17:35:47 -070091 server_.set_enable_permission_checks(enable);
92 }
93
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000094 void AddInternalSocket(const rtc::SocketAddress& int_addr,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070095 ProtocolType proto,
96 bool ignore_bad_cert = true,
Ali Tofighde2ac5a2022-06-30 11:58:26 +020097 absl::string_view common_name = "test turn server") {
Sebastian Janssonc01367d2019-04-08 15:20:44 +020098 RTC_DCHECK(thread_checker_.IsCurrent());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000099 if (proto == cricket::PROTO_UDP) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700100 server_.AddInternalSocket(
Niels Möller6dd49972021-11-24 14:05:55 +0100101 rtc::AsyncUDPSocket::Create(socket_factory_, int_addr), proto);
Steve Anton786de702017-08-17 15:15:46 -0700102 } else if (proto == cricket::PROTO_TCP || proto == cricket::PROTO_TLS) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000103 // For TCP we need to create a server socket which can listen for incoming
104 // new connections.
Niels Möller6dd49972021-11-24 14:05:55 +0100105 rtc::Socket* socket = socket_factory_->CreateSocket(AF_INET, SOCK_STREAM);
Niels Möllerac9a2882021-10-20 15:25:09 +0200106 socket->Bind(int_addr);
107 socket->Listen(5);
Steve Anton786de702017-08-17 15:15:46 -0700108 if (proto == cricket::PROTO_TLS) {
109 // For TLS, wrap the TCP socket with an SSL adapter. The adapter must
110 // be configured with a self-signed certificate for testing.
111 // Additionally, the client will not present a valid certificate, so we
112 // must not fail when checking the peer's identity.
Niels Möllerac9a2882021-10-20 15:25:09 +0200113 std::unique_ptr<rtc::SSLAdapterFactory> ssl_adapter_factory =
114 rtc::SSLAdapterFactory::Create();
115 ssl_adapter_factory->SetRole(rtc::SSL_SERVER);
116 ssl_adapter_factory->SetIdentity(
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100117 rtc::SSLIdentity::Create(common_name, rtc::KeyParams()));
Niels Möllerac9a2882021-10-20 15:25:09 +0200118 ssl_adapter_factory->SetIgnoreBadCert(ignore_bad_cert);
119 server_.AddInternalServerSocket(socket, proto,
120 std::move(ssl_adapter_factory));
121 } else {
122 server_.AddInternalServerSocket(socket, proto);
Steve Anton786de702017-08-17 15:15:46 -0700123 }
Steve Anton786de702017-08-17 15:15:46 -0700124 } else {
Artem Titovd3251962021-11-15 16:57:07 +0100125 RTC_DCHECK_NOTREACHED() << "Unknown protocol type: " << proto;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000126 }
127 }
128
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000129 // Finds the first allocation in the server allocation map with a source
130 // ip and port matching the socket address provided.
131 TurnServerAllocation* FindAllocation(const rtc::SocketAddress& src) {
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200132 RTC_DCHECK(thread_checker_.IsCurrent());
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000133 const TurnServer::AllocationMap& map = server_.allocations();
134 for (TurnServer::AllocationMap::const_iterator it = map.begin();
Yves Gerey665174f2018-06-19 15:03:05 +0200135 it != map.end(); ++it) {
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000136 if (src == it->first.src()) {
deadbeef97943662016-07-12 11:04:50 -0700137 return it->second.get();
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000138 }
139 }
140 return NULL;
141 }
142
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000143 private:
144 // For this test server, succeed if the password is the same as the username.
145 // Obviously, do not use this in a production environment.
Ali Tofighde2ac5a2022-06-30 11:58:26 +0200146 virtual bool GetKey(absl::string_view username,
147 absl::string_view realm,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000148 std::string* key) {
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200149 RTC_DCHECK(thread_checker_.IsCurrent());
Ali Tofighde2ac5a2022-06-30 11:58:26 +0200150 return ComputeStunCredentialHash(std::string(username), std::string(realm),
151 std::string(username), key);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000152 }
153
154 TurnServer server_;
Niels Möller6dd49972021-11-24 14:05:55 +0100155 rtc::SocketFactory* socket_factory_;
Artem Titovc8421c42021-02-02 10:57:19 +0100156 webrtc::SequenceChecker thread_checker_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000157};
158
159} // namespace cricket
160
Steve Anton10542f22019-01-11 09:11:00 -0800161#endif // P2P_BASE_TEST_TURN_SERVER_H_