blob: bbdc5663b04c63aa0f7d49ac672b5657c764875e [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
Artem Titovd15a5752021-02-10 14:31:24 +010019#include "api/sequence_checker.h"
Patrik Höglund56d94522019-11-18 15:53:32 +010020#include "api/transport/stun.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "p2p/base/basic_packet_socket_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "p2p/base/turn_server.h"
23#include "rtc_base/async_udp_socket.h"
24#include "rtc_base/ssl_adapter.h"
25#include "rtc_base/ssl_identity.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "rtc_base/thread.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000027
28namespace cricket {
29
30static const char kTestRealm[] = "example.org";
31static const char kTestSoftware[] = "TestTurnServer";
32
33class TestTurnRedirector : public TurnRedirectInterface {
34 public:
35 explicit TestTurnRedirector(const std::vector<rtc::SocketAddress>& addresses)
36 : alternate_server_addresses_(addresses),
Yves Gerey665174f2018-06-19 15:03:05 +020037 iter_(alternate_server_addresses_.begin()) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000038
39 virtual bool ShouldRedirect(const rtc::SocketAddress&,
40 rtc::SocketAddress* out) {
41 if (!out || iter_ == alternate_server_addresses_.end()) {
42 return false;
43 }
44 *out = *iter_++;
45 return true;
46 }
47
48 private:
49 const std::vector<rtc::SocketAddress>& alternate_server_addresses_;
50 std::vector<rtc::SocketAddress>::const_iterator iter_;
51};
52
53class TestTurnServer : public TurnAuthInterface {
54 public:
55 TestTurnServer(rtc::Thread* thread,
Honghai Zhang80f1db92016-01-27 11:54:45 -080056 const rtc::SocketAddress& int_addr,
57 const rtc::SocketAddress& udp_ext_addr,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070058 ProtocolType int_protocol = PROTO_UDP,
59 bool ignore_bad_cert = true,
60 const std::string& common_name = "test turn server")
Taylor Brandstettere5835f52016-09-16 15:07:50 -070061 : server_(thread), thread_(thread) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070062 AddInternalSocket(int_addr, int_protocol, ignore_bad_cert, common_name);
Niels Möller9def9942021-09-07 09:16:49 +020063 // TODO(bugs.webrtc.org/13145): Take a SocketFactory as argument, so we
64 // don't need thread_->socketserver().
65 server_.SetExternalSocketFactory(
66 new rtc::BasicPacketSocketFactory(thread_->socketserver()),
67 udp_ext_addr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000068 server_.set_realm(kTestRealm);
69 server_.set_software(kTestSoftware);
70 server_.set_auth_hook(this);
71 }
72
Sebastian Janssonc01367d2019-04-08 15:20:44 +020073 ~TestTurnServer() { RTC_DCHECK(thread_checker_.IsCurrent()); }
Seth Hampsonaed71642018-06-11 07:41:32 -070074
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000075 void set_enable_otu_nonce(bool enable) {
Sebastian Janssonc01367d2019-04-08 15:20:44 +020076 RTC_DCHECK(thread_checker_.IsCurrent());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000077 server_.set_enable_otu_nonce(enable);
78 }
79
Seth Hampsonaed71642018-06-11 07:41:32 -070080 TurnServer* server() {
Sebastian Janssonc01367d2019-04-08 15:20:44 +020081 RTC_DCHECK(thread_checker_.IsCurrent());
Seth Hampsonaed71642018-06-11 07:41:32 -070082 return &server_;
83 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000084
85 void set_redirect_hook(TurnRedirectInterface* redirect_hook) {
Sebastian Janssonc01367d2019-04-08 15:20:44 +020086 RTC_DCHECK(thread_checker_.IsCurrent());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000087 server_.set_redirect_hook(redirect_hook);
88 }
89
Taylor Brandstetteref184702016-06-23 17:35:47 -070090 void set_enable_permission_checks(bool enable) {
Sebastian Janssonc01367d2019-04-08 15:20:44 +020091 RTC_DCHECK(thread_checker_.IsCurrent());
Taylor Brandstetteref184702016-06-23 17:35:47 -070092 server_.set_enable_permission_checks(enable);
93 }
94
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000095 void AddInternalSocket(const rtc::SocketAddress& int_addr,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070096 ProtocolType proto,
97 bool ignore_bad_cert = true,
98 const std::string& common_name = "test turn server") {
Sebastian Janssonc01367d2019-04-08 15:20:44 +020099 RTC_DCHECK(thread_checker_.IsCurrent());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000100 if (proto == cricket::PROTO_UDP) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700101 server_.AddInternalSocket(
102 rtc::AsyncUDPSocket::Create(thread_->socketserver(), int_addr),
103 proto);
Steve Anton786de702017-08-17 15:15:46 -0700104 } else if (proto == cricket::PROTO_TCP || proto == cricket::PROTO_TLS) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000105 // For TCP we need to create a server socket which can listen for incoming
106 // new connections.
Niels Möllerd0b88792021-08-12 10:32:30 +0200107 rtc::Socket* socket =
108 thread_->socketserver()->CreateSocket(AF_INET, SOCK_STREAM);
Niels Möllerac9a2882021-10-20 15:25:09 +0200109 socket->Bind(int_addr);
110 socket->Listen(5);
Steve Anton786de702017-08-17 15:15:46 -0700111 if (proto == cricket::PROTO_TLS) {
112 // For TLS, wrap the TCP socket with an SSL adapter. The adapter must
113 // be configured with a self-signed certificate for testing.
114 // Additionally, the client will not present a valid certificate, so we
115 // must not fail when checking the peer's identity.
Niels Möllerac9a2882021-10-20 15:25:09 +0200116 std::unique_ptr<rtc::SSLAdapterFactory> ssl_adapter_factory =
117 rtc::SSLAdapterFactory::Create();
118 ssl_adapter_factory->SetRole(rtc::SSL_SERVER);
119 ssl_adapter_factory->SetIdentity(
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100120 rtc::SSLIdentity::Create(common_name, rtc::KeyParams()));
Niels Möllerac9a2882021-10-20 15:25:09 +0200121 ssl_adapter_factory->SetIgnoreBadCert(ignore_bad_cert);
122 server_.AddInternalServerSocket(socket, proto,
123 std::move(ssl_adapter_factory));
124 } else {
125 server_.AddInternalServerSocket(socket, proto);
Steve Anton786de702017-08-17 15:15:46 -0700126 }
Steve Anton786de702017-08-17 15:15:46 -0700127 } else {
Artem Titovd3251962021-11-15 16:57:07 +0100128 RTC_DCHECK_NOTREACHED() << "Unknown protocol type: " << proto;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000129 }
130 }
131
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000132 // Finds the first allocation in the server allocation map with a source
133 // ip and port matching the socket address provided.
134 TurnServerAllocation* FindAllocation(const rtc::SocketAddress& src) {
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200135 RTC_DCHECK(thread_checker_.IsCurrent());
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000136 const TurnServer::AllocationMap& map = server_.allocations();
137 for (TurnServer::AllocationMap::const_iterator it = map.begin();
Yves Gerey665174f2018-06-19 15:03:05 +0200138 it != map.end(); ++it) {
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000139 if (src == it->first.src()) {
deadbeef97943662016-07-12 11:04:50 -0700140 return it->second.get();
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000141 }
142 }
143 return NULL;
144 }
145
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000146 private:
147 // For this test server, succeed if the password is the same as the username.
148 // Obviously, do not use this in a production environment.
Yves Gerey665174f2018-06-19 15:03:05 +0200149 virtual bool GetKey(const std::string& username,
150 const std::string& realm,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000151 std::string* key) {
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200152 RTC_DCHECK(thread_checker_.IsCurrent());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000153 return ComputeStunCredentialHash(username, realm, username, key);
154 }
155
156 TurnServer server_;
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700157 rtc::Thread* thread_;
Artem Titovc8421c42021-02-02 10:57:19 +0100158 webrtc::SequenceChecker thread_checker_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000159};
160
161} // namespace cricket
162
Steve Anton10542f22019-01-11 09:11:00 -0800163#endif // P2P_BASE_TEST_TURN_SERVER_H_