blob: d130560ff202473d715777369dbaf3ed89c42aae [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef P2P_BASE_TESTTURNSERVER_H_
12#define P2P_BASE_TESTTURNSERVER_H_
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000013
14#include <string>
15#include <vector>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "p2p/base/basicpacketsocketfactory.h"
18#include "p2p/base/stun.h"
19#include "p2p/base/turnserver.h"
20#include "rtc_base/asyncudpsocket.h"
21#include "rtc_base/ssladapter.h"
22#include "rtc_base/sslidentity.h"
23#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),
35 iter_(alternate_server_addresses_.begin()) {
36 }
37
38 virtual bool ShouldRedirect(const rtc::SocketAddress&,
39 rtc::SocketAddress* out) {
40 if (!out || iter_ == alternate_server_addresses_.end()) {
41 return false;
42 }
43 *out = *iter_++;
44 return true;
45 }
46
47 private:
48 const std::vector<rtc::SocketAddress>& alternate_server_addresses_;
49 std::vector<rtc::SocketAddress>::const_iterator iter_;
50};
51
52class TestTurnServer : public TurnAuthInterface {
53 public:
54 TestTurnServer(rtc::Thread* thread,
Honghai Zhang80f1db92016-01-27 11:54:45 -080055 const rtc::SocketAddress& int_addr,
56 const rtc::SocketAddress& udp_ext_addr,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070057 ProtocolType int_protocol = PROTO_UDP,
58 bool ignore_bad_cert = true,
59 const std::string& common_name = "test turn server")
Taylor Brandstettere5835f52016-09-16 15:07:50 -070060 : server_(thread), thread_(thread) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070061 AddInternalSocket(int_addr, int_protocol, ignore_bad_cert, common_name);
Taylor Brandstettere5835f52016-09-16 15:07:50 -070062 server_.SetExternalSocketFactory(new rtc::BasicPacketSocketFactory(thread),
63 udp_ext_addr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000064 server_.set_realm(kTestRealm);
65 server_.set_software(kTestSoftware);
66 server_.set_auth_hook(this);
67 }
68
Seth Hampsonaed71642018-06-11 07:41:32 -070069 ~TestTurnServer() { RTC_DCHECK(thread_checker_.CalledOnValidThread()); }
70
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000071 void set_enable_otu_nonce(bool enable) {
Seth Hampsonaed71642018-06-11 07:41:32 -070072 RTC_DCHECK(thread_checker_.CalledOnValidThread());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000073 server_.set_enable_otu_nonce(enable);
74 }
75
Seth Hampsonaed71642018-06-11 07:41:32 -070076 TurnServer* server() {
77 RTC_DCHECK(thread_checker_.CalledOnValidThread());
78 return &server_;
79 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000080
81 void set_redirect_hook(TurnRedirectInterface* redirect_hook) {
Seth Hampsonaed71642018-06-11 07:41:32 -070082 RTC_DCHECK(thread_checker_.CalledOnValidThread());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000083 server_.set_redirect_hook(redirect_hook);
84 }
85
Taylor Brandstetteref184702016-06-23 17:35:47 -070086 void set_enable_permission_checks(bool enable) {
Seth Hampsonaed71642018-06-11 07:41:32 -070087 RTC_DCHECK(thread_checker_.CalledOnValidThread());
Taylor Brandstetteref184702016-06-23 17:35:47 -070088 server_.set_enable_permission_checks(enable);
89 }
90
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000091 void AddInternalSocket(const rtc::SocketAddress& int_addr,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070092 ProtocolType proto,
93 bool ignore_bad_cert = true,
94 const std::string& common_name = "test turn server") {
Seth Hampsonaed71642018-06-11 07:41:32 -070095 RTC_DCHECK(thread_checker_.CalledOnValidThread());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000096 if (proto == cricket::PROTO_UDP) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -070097 server_.AddInternalSocket(
98 rtc::AsyncUDPSocket::Create(thread_->socketserver(), int_addr),
99 proto);
Steve Anton786de702017-08-17 15:15:46 -0700100 } else if (proto == cricket::PROTO_TCP || proto == cricket::PROTO_TLS) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000101 // For TCP we need to create a server socket which can listen for incoming
102 // new connections.
103 rtc::AsyncSocket* socket =
Steve Anton31e5bf52018-05-07 10:42:55 -0700104 thread_->socketserver()->CreateAsyncSocket(AF_INET, SOCK_STREAM);
Steve Anton786de702017-08-17 15:15:46 -0700105 if (proto == cricket::PROTO_TLS) {
106 // For TLS, wrap the TCP socket with an SSL adapter. The adapter must
107 // be configured with a self-signed certificate for testing.
108 // Additionally, the client will not present a valid certificate, so we
109 // must not fail when checking the peer's identity.
110 rtc::SSLAdapter* adapter = rtc::SSLAdapter::Create(socket);
111 adapter->SetRole(rtc::SSL_SERVER);
112 adapter->SetIdentity(
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700113 rtc::SSLIdentity::Generate(common_name, rtc::KeyParams()));
114 adapter->SetIgnoreBadCert(ignore_bad_cert);
Steve Anton786de702017-08-17 15:15:46 -0700115 socket = adapter;
116 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000117 socket->Bind(int_addr);
118 socket->Listen(5);
119 server_.AddInternalServerSocket(socket, proto);
Steve Anton786de702017-08-17 15:15:46 -0700120 } else {
121 RTC_NOTREACHED() << "Unknown protocol type: " << proto;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000122 }
123 }
124
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000125 // Finds the first allocation in the server allocation map with a source
126 // ip and port matching the socket address provided.
127 TurnServerAllocation* FindAllocation(const rtc::SocketAddress& src) {
Seth Hampsonaed71642018-06-11 07:41:32 -0700128 RTC_DCHECK(thread_checker_.CalledOnValidThread());
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000129 const TurnServer::AllocationMap& map = server_.allocations();
130 for (TurnServer::AllocationMap::const_iterator it = map.begin();
131 it != map.end(); ++it) {
132 if (src == it->first.src()) {
deadbeef97943662016-07-12 11:04:50 -0700133 return it->second.get();
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000134 }
135 }
136 return NULL;
137 }
138
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000139 private:
140 // For this test server, succeed if the password is the same as the username.
141 // Obviously, do not use this in a production environment.
142 virtual bool GetKey(const std::string& username, const std::string& realm,
143 std::string* key) {
Seth Hampsonaed71642018-06-11 07:41:32 -0700144 RTC_DCHECK(thread_checker_.CalledOnValidThread());
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200155#endif // P2P_BASE_TESTTURNSERVER_H_