blob: e1deb5901ef6a589e8ceb64f3746c79f2b1d4459 [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
14#include <string>
15#include <vector>
16
Artem Titovd15a5752021-02-10 14:31:24 +010017#include "api/sequence_checker.h"
Patrik Höglund56d94522019-11-18 15:53:32 +010018#include "api/transport/stun.h"
Steve Anton10542f22019-01-11 09:11:00 -080019#include "p2p/base/basic_packet_socket_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "p2p/base/turn_server.h"
21#include "rtc_base/async_udp_socket.h"
22#include "rtc_base/ssl_adapter.h"
23#include "rtc_base/ssl_identity.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/thread.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000025
26namespace cricket {
27
28static const char kTestRealm[] = "example.org";
29static const char kTestSoftware[] = "TestTurnServer";
30
31class TestTurnRedirector : public TurnRedirectInterface {
32 public:
33 explicit TestTurnRedirector(const std::vector<rtc::SocketAddress>& addresses)
34 : alternate_server_addresses_(addresses),
Yves Gerey665174f2018-06-19 15:03:05 +020035 iter_(alternate_server_addresses_.begin()) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000036
37 virtual bool ShouldRedirect(const rtc::SocketAddress&,
38 rtc::SocketAddress* out) {
39 if (!out || iter_ == alternate_server_addresses_.end()) {
40 return false;
41 }
42 *out = *iter_++;
43 return true;
44 }
45
46 private:
47 const std::vector<rtc::SocketAddress>& alternate_server_addresses_;
48 std::vector<rtc::SocketAddress>::const_iterator iter_;
49};
50
51class TestTurnServer : public TurnAuthInterface {
52 public:
53 TestTurnServer(rtc::Thread* thread,
Honghai Zhang80f1db92016-01-27 11:54:45 -080054 const rtc::SocketAddress& int_addr,
55 const rtc::SocketAddress& udp_ext_addr,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070056 ProtocolType int_protocol = PROTO_UDP,
57 bool ignore_bad_cert = true,
58 const std::string& common_name = "test turn server")
Taylor Brandstettere5835f52016-09-16 15:07:50 -070059 : server_(thread), thread_(thread) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070060 AddInternalSocket(int_addr, int_protocol, ignore_bad_cert, common_name);
Niels Möller9def9942021-09-07 09:16:49 +020061 // TODO(bugs.webrtc.org/13145): Take a SocketFactory as argument, so we
62 // don't need thread_->socketserver().
63 server_.SetExternalSocketFactory(
64 new rtc::BasicPacketSocketFactory(thread_->socketserver()),
65 udp_ext_addr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000066 server_.set_realm(kTestRealm);
67 server_.set_software(kTestSoftware);
68 server_.set_auth_hook(this);
69 }
70
Sebastian Janssonc01367d2019-04-08 15:20:44 +020071 ~TestTurnServer() { RTC_DCHECK(thread_checker_.IsCurrent()); }
Seth Hampsonaed71642018-06-11 07:41:32 -070072
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000073 void set_enable_otu_nonce(bool enable) {
Sebastian Janssonc01367d2019-04-08 15:20:44 +020074 RTC_DCHECK(thread_checker_.IsCurrent());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000075 server_.set_enable_otu_nonce(enable);
76 }
77
Seth Hampsonaed71642018-06-11 07:41:32 -070078 TurnServer* server() {
Sebastian Janssonc01367d2019-04-08 15:20:44 +020079 RTC_DCHECK(thread_checker_.IsCurrent());
Seth Hampsonaed71642018-06-11 07:41:32 -070080 return &server_;
81 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000082
83 void set_redirect_hook(TurnRedirectInterface* redirect_hook) {
Sebastian Janssonc01367d2019-04-08 15:20:44 +020084 RTC_DCHECK(thread_checker_.IsCurrent());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000085 server_.set_redirect_hook(redirect_hook);
86 }
87
Taylor Brandstetteref184702016-06-23 17:35:47 -070088 void set_enable_permission_checks(bool enable) {
Sebastian Janssonc01367d2019-04-08 15:20:44 +020089 RTC_DCHECK(thread_checker_.IsCurrent());
Taylor Brandstetteref184702016-06-23 17:35:47 -070090 server_.set_enable_permission_checks(enable);
91 }
92
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000093 void AddInternalSocket(const rtc::SocketAddress& int_addr,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070094 ProtocolType proto,
95 bool ignore_bad_cert = true,
96 const std::string& common_name = "test turn server") {
Sebastian Janssonc01367d2019-04-08 15:20:44 +020097 RTC_DCHECK(thread_checker_.IsCurrent());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000098 if (proto == cricket::PROTO_UDP) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -070099 server_.AddInternalSocket(
100 rtc::AsyncUDPSocket::Create(thread_->socketserver(), int_addr),
101 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öllerd0b88792021-08-12 10:32:30 +0200105 rtc::Socket* socket =
106 thread_->socketserver()->CreateSocket(AF_INET, SOCK_STREAM);
Steve Anton786de702017-08-17 15:15:46 -0700107 if (proto == cricket::PROTO_TLS) {
108 // For TLS, wrap the TCP socket with an SSL adapter. The adapter must
109 // be configured with a self-signed certificate for testing.
110 // Additionally, the client will not present a valid certificate, so we
111 // must not fail when checking the peer's identity.
112 rtc::SSLAdapter* adapter = rtc::SSLAdapter::Create(socket);
113 adapter->SetRole(rtc::SSL_SERVER);
114 adapter->SetIdentity(
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100115 rtc::SSLIdentity::Create(common_name, rtc::KeyParams()));
Sergey Silkin9c147dd2018-09-12 10:45:38 +0000116 adapter->SetIgnoreBadCert(ignore_bad_cert);
Steve Anton786de702017-08-17 15:15:46 -0700117 socket = adapter;
118 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000119 socket->Bind(int_addr);
120 socket->Listen(5);
121 server_.AddInternalServerSocket(socket, proto);
Steve Anton786de702017-08-17 15:15:46 -0700122 } else {
123 RTC_NOTREACHED() << "Unknown protocol type: " << proto;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000124 }
125 }
126
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000127 // Finds the first allocation in the server allocation map with a source
128 // ip and port matching the socket address provided.
129 TurnServerAllocation* FindAllocation(const rtc::SocketAddress& src) {
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200130 RTC_DCHECK(thread_checker_.IsCurrent());
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000131 const TurnServer::AllocationMap& map = server_.allocations();
132 for (TurnServer::AllocationMap::const_iterator it = map.begin();
Yves Gerey665174f2018-06-19 15:03:05 +0200133 it != map.end(); ++it) {
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000134 if (src == it->first.src()) {
deadbeef97943662016-07-12 11:04:50 -0700135 return it->second.get();
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000136 }
137 }
138 return NULL;
139 }
140
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000141 private:
142 // For this test server, succeed if the password is the same as the username.
143 // Obviously, do not use this in a production environment.
Yves Gerey665174f2018-06-19 15:03:05 +0200144 virtual bool GetKey(const std::string& username,
145 const std::string& realm,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000146 std::string* key) {
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200147 RTC_DCHECK(thread_checker_.IsCurrent());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000148 return ComputeStunCredentialHash(username, realm, username, key);
149 }
150
151 TurnServer server_;
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700152 rtc::Thread* thread_;
Artem Titovc8421c42021-02-02 10:57:19 +0100153 webrtc::SequenceChecker thread_checker_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000154};
155
156} // namespace cricket
157
Steve Anton10542f22019-01-11 09:11:00 -0800158#endif // P2P_BASE_TEST_TURN_SERVER_H_