henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1 | /* |
| 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 Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #ifndef P2P_BASE_TEST_TURN_SERVER_H_ |
| 12 | #define P2P_BASE_TEST_TURN_SERVER_H_ |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 13 | |
| 14 | #include <string> |
| 15 | #include <vector> |
| 16 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 17 | #include "p2p/base/basic_packet_socket_factory.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "p2p/base/stun.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 19 | #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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "rtc_base/thread.h" |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 24 | #include "rtc_base/thread_checker.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 25 | |
| 26 | namespace cricket { |
| 27 | |
| 28 | static const char kTestRealm[] = "example.org"; |
| 29 | static const char kTestSoftware[] = "TestTurnServer"; |
| 30 | |
| 31 | class TestTurnRedirector : public TurnRedirectInterface { |
| 32 | public: |
| 33 | explicit TestTurnRedirector(const std::vector<rtc::SocketAddress>& addresses) |
| 34 | : alternate_server_addresses_(addresses), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 35 | iter_(alternate_server_addresses_.begin()) {} |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 36 | |
| 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 | |
| 51 | class TestTurnServer : public TurnAuthInterface { |
| 52 | public: |
| 53 | TestTurnServer(rtc::Thread* thread, |
Honghai Zhang | 80f1db9 | 2016-01-27 11:54:45 -0800 | [diff] [blame] | 54 | const rtc::SocketAddress& int_addr, |
| 55 | const rtc::SocketAddress& udp_ext_addr, |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 56 | ProtocolType int_protocol = PROTO_UDP, |
| 57 | bool ignore_bad_cert = true, |
| 58 | const std::string& common_name = "test turn server") |
Taylor Brandstetter | e5835f5 | 2016-09-16 15:07:50 -0700 | [diff] [blame] | 59 | : server_(thread), thread_(thread) { |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 60 | AddInternalSocket(int_addr, int_protocol, ignore_bad_cert, common_name); |
Taylor Brandstetter | e5835f5 | 2016-09-16 15:07:50 -0700 | [diff] [blame] | 61 | server_.SetExternalSocketFactory(new rtc::BasicPacketSocketFactory(thread), |
| 62 | udp_ext_addr); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 63 | server_.set_realm(kTestRealm); |
| 64 | server_.set_software(kTestSoftware); |
| 65 | server_.set_auth_hook(this); |
| 66 | } |
| 67 | |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 68 | ~TestTurnServer() { RTC_DCHECK(thread_checker_.IsCurrent()); } |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 69 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 70 | void set_enable_otu_nonce(bool enable) { |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 71 | RTC_DCHECK(thread_checker_.IsCurrent()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 72 | server_.set_enable_otu_nonce(enable); |
| 73 | } |
| 74 | |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 75 | TurnServer* server() { |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 76 | RTC_DCHECK(thread_checker_.IsCurrent()); |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 77 | return &server_; |
| 78 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 79 | |
| 80 | void set_redirect_hook(TurnRedirectInterface* redirect_hook) { |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 81 | RTC_DCHECK(thread_checker_.IsCurrent()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 82 | server_.set_redirect_hook(redirect_hook); |
| 83 | } |
| 84 | |
Taylor Brandstetter | ef18470 | 2016-06-23 17:35:47 -0700 | [diff] [blame] | 85 | void set_enable_permission_checks(bool enable) { |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 86 | RTC_DCHECK(thread_checker_.IsCurrent()); |
Taylor Brandstetter | ef18470 | 2016-06-23 17:35:47 -0700 | [diff] [blame] | 87 | server_.set_enable_permission_checks(enable); |
| 88 | } |
| 89 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 90 | void AddInternalSocket(const rtc::SocketAddress& int_addr, |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 91 | ProtocolType proto, |
| 92 | bool ignore_bad_cert = true, |
| 93 | const std::string& common_name = "test turn server") { |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 94 | RTC_DCHECK(thread_checker_.IsCurrent()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 95 | if (proto == cricket::PROTO_UDP) { |
Taylor Brandstetter | e5835f5 | 2016-09-16 15:07:50 -0700 | [diff] [blame] | 96 | server_.AddInternalSocket( |
| 97 | rtc::AsyncUDPSocket::Create(thread_->socketserver(), int_addr), |
| 98 | proto); |
Steve Anton | 786de70 | 2017-08-17 15:15:46 -0700 | [diff] [blame] | 99 | } else if (proto == cricket::PROTO_TCP || proto == cricket::PROTO_TLS) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 100 | // For TCP we need to create a server socket which can listen for incoming |
| 101 | // new connections. |
| 102 | rtc::AsyncSocket* socket = |
Steve Anton | 31e5bf5 | 2018-05-07 10:42:55 -0700 | [diff] [blame] | 103 | thread_->socketserver()->CreateAsyncSocket(AF_INET, SOCK_STREAM); |
Steve Anton | 786de70 | 2017-08-17 15:15:46 -0700 | [diff] [blame] | 104 | 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 Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 112 | rtc::SSLIdentity::Generate(common_name, rtc::KeyParams())); |
Sergey Silkin | 9c147dd | 2018-09-12 10:45:38 +0000 | [diff] [blame] | 113 | adapter->SetIgnoreBadCert(ignore_bad_cert); |
Steve Anton | 786de70 | 2017-08-17 15:15:46 -0700 | [diff] [blame] | 114 | socket = adapter; |
| 115 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 116 | socket->Bind(int_addr); |
| 117 | socket->Listen(5); |
| 118 | server_.AddInternalServerSocket(socket, proto); |
Steve Anton | 786de70 | 2017-08-17 15:15:46 -0700 | [diff] [blame] | 119 | } else { |
| 120 | RTC_NOTREACHED() << "Unknown protocol type: " << proto; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 124 | // 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 Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 127 | RTC_DCHECK(thread_checker_.IsCurrent()); |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 128 | const TurnServer::AllocationMap& map = server_.allocations(); |
| 129 | for (TurnServer::AllocationMap::const_iterator it = map.begin(); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 130 | it != map.end(); ++it) { |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 131 | if (src == it->first.src()) { |
deadbeef | 9794366 | 2016-07-12 11:04:50 -0700 | [diff] [blame] | 132 | return it->second.get(); |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | return NULL; |
| 136 | } |
| 137 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 138 | 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 141 | virtual bool GetKey(const std::string& username, |
| 142 | const std::string& realm, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 143 | std::string* key) { |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 144 | RTC_DCHECK(thread_checker_.IsCurrent()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 145 | return ComputeStunCredentialHash(username, realm, username, key); |
| 146 | } |
| 147 | |
| 148 | TurnServer server_; |
Taylor Brandstetter | e5835f5 | 2016-09-16 15:07:50 -0700 | [diff] [blame] | 149 | rtc::Thread* thread_; |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 150 | rtc::ThreadChecker thread_checker_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | } // namespace cricket |
| 154 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 155 | #endif // P2P_BASE_TEST_TURN_SERVER_H_ |