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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 11 | #ifndef P2P_BASE_TESTTURNSERVER_H_ |
| 12 | #define P2P_BASE_TESTTURNSERVER_H_ |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 13 | |
| 14 | #include <string> |
| 15 | #include <vector> |
| 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 17 | #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" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 24 | |
| 25 | namespace cricket { |
| 26 | |
| 27 | static const char kTestRealm[] = "example.org"; |
| 28 | static const char kTestSoftware[] = "TestTurnServer"; |
| 29 | |
| 30 | class TestTurnRedirector : public TurnRedirectInterface { |
| 31 | public: |
| 32 | explicit TestTurnRedirector(const std::vector<rtc::SocketAddress>& addresses) |
| 33 | : alternate_server_addresses_(addresses), |
| 34 | iter_(alternate_server_addresses_.begin()) { |
| 35 | } |
| 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, |
| 56 | ProtocolType int_protocol = PROTO_UDP) |
Taylor Brandstetter | e5835f5 | 2016-09-16 15:07:50 -0700 | [diff] [blame] | 57 | : server_(thread), thread_(thread) { |
Honghai Zhang | 80f1db9 | 2016-01-27 11:54:45 -0800 | [diff] [blame] | 58 | AddInternalSocket(int_addr, int_protocol); |
Taylor Brandstetter | e5835f5 | 2016-09-16 15:07:50 -0700 | [diff] [blame] | 59 | server_.SetExternalSocketFactory(new rtc::BasicPacketSocketFactory(thread), |
| 60 | udp_ext_addr); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 61 | server_.set_realm(kTestRealm); |
| 62 | server_.set_software(kTestSoftware); |
| 63 | server_.set_auth_hook(this); |
| 64 | } |
| 65 | |
| 66 | void set_enable_otu_nonce(bool enable) { |
| 67 | server_.set_enable_otu_nonce(enable); |
| 68 | } |
| 69 | |
| 70 | TurnServer* server() { return &server_; } |
| 71 | |
| 72 | void set_redirect_hook(TurnRedirectInterface* redirect_hook) { |
| 73 | server_.set_redirect_hook(redirect_hook); |
| 74 | } |
| 75 | |
Taylor Brandstetter | ef18470 | 2016-06-23 17:35:47 -0700 | [diff] [blame] | 76 | void set_enable_permission_checks(bool enable) { |
| 77 | server_.set_enable_permission_checks(enable); |
| 78 | } |
| 79 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 80 | void AddInternalSocket(const rtc::SocketAddress& int_addr, |
| 81 | ProtocolType proto) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 82 | if (proto == cricket::PROTO_UDP) { |
Taylor Brandstetter | e5835f5 | 2016-09-16 15:07:50 -0700 | [diff] [blame] | 83 | server_.AddInternalSocket( |
| 84 | rtc::AsyncUDPSocket::Create(thread_->socketserver(), int_addr), |
| 85 | proto); |
Steve Anton | 786de70 | 2017-08-17 15:15:46 -0700 | [diff] [blame] | 86 | } else if (proto == cricket::PROTO_TCP || proto == cricket::PROTO_TLS) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 87 | // For TCP we need to create a server socket which can listen for incoming |
| 88 | // new connections. |
| 89 | rtc::AsyncSocket* socket = |
Taylor Brandstetter | e5835f5 | 2016-09-16 15:07:50 -0700 | [diff] [blame] | 90 | thread_->socketserver()->CreateAsyncSocket(SOCK_STREAM); |
Steve Anton | 786de70 | 2017-08-17 15:15:46 -0700 | [diff] [blame] | 91 | if (proto == cricket::PROTO_TLS) { |
| 92 | // For TLS, wrap the TCP socket with an SSL adapter. The adapter must |
| 93 | // be configured with a self-signed certificate for testing. |
| 94 | // Additionally, the client will not present a valid certificate, so we |
| 95 | // must not fail when checking the peer's identity. |
| 96 | rtc::SSLAdapter* adapter = rtc::SSLAdapter::Create(socket); |
| 97 | adapter->SetRole(rtc::SSL_SERVER); |
| 98 | adapter->SetIdentity( |
| 99 | rtc::SSLIdentity::Generate("test turn server", rtc::KeyParams())); |
Diogo Real | 1dca9d5 | 2017-08-29 12:18:32 -0700 | [diff] [blame] | 100 | adapter->SetIgnoreBadCert(true); |
Steve Anton | 786de70 | 2017-08-17 15:15:46 -0700 | [diff] [blame] | 101 | socket = adapter; |
| 102 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 103 | socket->Bind(int_addr); |
| 104 | socket->Listen(5); |
| 105 | server_.AddInternalServerSocket(socket, proto); |
Steve Anton | 786de70 | 2017-08-17 15:15:46 -0700 | [diff] [blame] | 106 | } else { |
| 107 | RTC_NOTREACHED() << "Unknown protocol type: " << proto; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 111 | // Finds the first allocation in the server allocation map with a source |
| 112 | // ip and port matching the socket address provided. |
| 113 | TurnServerAllocation* FindAllocation(const rtc::SocketAddress& src) { |
| 114 | const TurnServer::AllocationMap& map = server_.allocations(); |
| 115 | for (TurnServer::AllocationMap::const_iterator it = map.begin(); |
| 116 | it != map.end(); ++it) { |
| 117 | if (src == it->first.src()) { |
deadbeef | 9794366 | 2016-07-12 11:04:50 -0700 | [diff] [blame] | 118 | return it->second.get(); |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | return NULL; |
| 122 | } |
| 123 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 124 | private: |
| 125 | // For this test server, succeed if the password is the same as the username. |
| 126 | // Obviously, do not use this in a production environment. |
| 127 | virtual bool GetKey(const std::string& username, const std::string& realm, |
| 128 | std::string* key) { |
| 129 | return ComputeStunCredentialHash(username, realm, username, key); |
| 130 | } |
| 131 | |
| 132 | TurnServer server_; |
Taylor Brandstetter | e5835f5 | 2016-09-16 15:07:50 -0700 | [diff] [blame] | 133 | rtc::Thread* thread_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | } // namespace cricket |
| 137 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 138 | #endif // P2P_BASE_TESTTURNSERVER_H_ |