blob: 3a4067bb86dd839a94be8ba61b969577e26d74e8 [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
Steve Anton10542f22019-01-11 09:11:00 -080017#include "p2p/base/basic_packet_socket_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "p2p/base/stun.h"
Steve Anton10542f22019-01-11 09:11:00 -080019#include "p2p/base/turn_server.h"
20#include "rtc_base/async_udp_socket.h"
21#include "rtc_base/ssl_adapter.h"
22#include "rtc_base/ssl_identity.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "rtc_base/thread.h"
Seth Hampsonaed71642018-06-11 07:41:32 -070024#include "rtc_base/thread_checker.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);
Taylor Brandstettere5835f52016-09-16 15:07:50 -070061 server_.SetExternalSocketFactory(new rtc::BasicPacketSocketFactory(thread),
62 udp_ext_addr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000063 server_.set_realm(kTestRealm);
64 server_.set_software(kTestSoftware);
65 server_.set_auth_hook(this);
66 }
67
Sebastian Janssonc01367d2019-04-08 15:20:44 +020068 ~TestTurnServer() { RTC_DCHECK(thread_checker_.IsCurrent()); }
Seth Hampsonaed71642018-06-11 07:41:32 -070069
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000070 void set_enable_otu_nonce(bool enable) {
Sebastian Janssonc01367d2019-04-08 15:20:44 +020071 RTC_DCHECK(thread_checker_.IsCurrent());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000072 server_.set_enable_otu_nonce(enable);
73 }
74
Seth Hampsonaed71642018-06-11 07:41:32 -070075 TurnServer* server() {
Sebastian Janssonc01367d2019-04-08 15:20:44 +020076 RTC_DCHECK(thread_checker_.IsCurrent());
Seth Hampsonaed71642018-06-11 07:41:32 -070077 return &server_;
78 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000079
80 void set_redirect_hook(TurnRedirectInterface* redirect_hook) {
Sebastian Janssonc01367d2019-04-08 15:20:44 +020081 RTC_DCHECK(thread_checker_.IsCurrent());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000082 server_.set_redirect_hook(redirect_hook);
83 }
84
Taylor Brandstetteref184702016-06-23 17:35:47 -070085 void set_enable_permission_checks(bool enable) {
Sebastian Janssonc01367d2019-04-08 15:20:44 +020086 RTC_DCHECK(thread_checker_.IsCurrent());
Taylor Brandstetteref184702016-06-23 17:35:47 -070087 server_.set_enable_permission_checks(enable);
88 }
89
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000090 void AddInternalSocket(const rtc::SocketAddress& int_addr,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070091 ProtocolType proto,
92 bool ignore_bad_cert = true,
93 const std::string& common_name = "test turn server") {
Sebastian Janssonc01367d2019-04-08 15:20:44 +020094 RTC_DCHECK(thread_checker_.IsCurrent());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000095 if (proto == cricket::PROTO_UDP) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -070096 server_.AddInternalSocket(
97 rtc::AsyncUDPSocket::Create(thread_->socketserver(), int_addr),
98 proto);
Steve Anton786de702017-08-17 15:15:46 -070099 } else if (proto == cricket::PROTO_TCP || proto == cricket::PROTO_TLS) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000100 // For TCP we need to create a server socket which can listen for incoming
101 // new connections.
102 rtc::AsyncSocket* socket =
Steve Anton31e5bf52018-05-07 10:42:55 -0700103 thread_->socketserver()->CreateAsyncSocket(AF_INET, SOCK_STREAM);
Steve Anton786de702017-08-17 15:15:46 -0700104 if (proto == cricket::PROTO_TLS) {
105 // For TLS, wrap the TCP socket with an SSL adapter. The adapter must
106 // be configured with a self-signed certificate for testing.
107 // Additionally, the client will not present a valid certificate, so we
108 // must not fail when checking the peer's identity.
109 rtc::SSLAdapter* adapter = rtc::SSLAdapter::Create(socket);
110 adapter->SetRole(rtc::SSL_SERVER);
111 adapter->SetIdentity(
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700112 rtc::SSLIdentity::Generate(common_name, rtc::KeyParams()));
Sergey Silkin9c147dd2018-09-12 10:45:38 +0000113 adapter->SetIgnoreBadCert(ignore_bad_cert);
Steve Anton786de702017-08-17 15:15:46 -0700114 socket = adapter;
115 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000116 socket->Bind(int_addr);
117 socket->Listen(5);
118 server_.AddInternalServerSocket(socket, proto);
Steve Anton786de702017-08-17 15:15:46 -0700119 } else {
120 RTC_NOTREACHED() << "Unknown protocol type: " << proto;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000121 }
122 }
123
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000124 // Finds the first allocation in the server allocation map with a source
125 // ip and port matching the socket address provided.
126 TurnServerAllocation* FindAllocation(const rtc::SocketAddress& src) {
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200127 RTC_DCHECK(thread_checker_.IsCurrent());
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000128 const TurnServer::AllocationMap& map = server_.allocations();
129 for (TurnServer::AllocationMap::const_iterator it = map.begin();
Yves Gerey665174f2018-06-19 15:03:05 +0200130 it != map.end(); ++it) {
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000131 if (src == it->first.src()) {
deadbeef97943662016-07-12 11:04:50 -0700132 return it->second.get();
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000133 }
134 }
135 return NULL;
136 }
137
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000138 private:
139 // For this test server, succeed if the password is the same as the username.
140 // Obviously, do not use this in a production environment.
Yves Gerey665174f2018-06-19 15:03:05 +0200141 virtual bool GetKey(const std::string& username,
142 const std::string& realm,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000143 std::string* key) {
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200144 RTC_DCHECK(thread_checker_.IsCurrent());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000145 return ComputeStunCredentialHash(username, realm, username, key);
146 }
147
148 TurnServer server_;
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700149 rtc::Thread* thread_;
Seth Hampsonaed71642018-06-11 07:41:32 -0700150 rtc::ThreadChecker thread_checker_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000151};
152
153} // namespace cricket
154
Steve Anton10542f22019-01-11 09:11:00 -0800155#endif // P2P_BASE_TEST_TURN_SERVER_H_