2013-07-10 02:45:36 +02:00
|
|
|
/*
|
|
|
|
* libjingle
|
|
|
|
* Copyright 2012, Google Inc.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
|
|
|
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
|
|
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
|
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream> // NOLINT
|
|
|
|
|
2014-10-18 00:03:39 +02:00
|
|
|
#include "talk/p2p/base/basicpacketsocketfactory.h"
|
|
|
|
#include "talk/p2p/base/turnserver.h"
|
2014-07-29 19:36:52 +02:00
|
|
|
#include "webrtc/base/asyncudpsocket.h"
|
|
|
|
#include "webrtc/base/optionsfile.h"
|
|
|
|
#include "webrtc/base/stringencode.h"
|
2014-08-13 19:26:08 +02:00
|
|
|
#include "webrtc/base/thread.h"
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
static const char kSoftware[] = "libjingle TurnServer";
|
|
|
|
|
|
|
|
class TurnFileAuth : public cricket::TurnAuthInterface {
|
|
|
|
public:
|
|
|
|
explicit TurnFileAuth(const std::string& path) : file_(path) {
|
|
|
|
file_.Load();
|
|
|
|
}
|
|
|
|
virtual bool GetKey(const std::string& username, const std::string& realm,
|
|
|
|
std::string* key) {
|
|
|
|
// File is stored as lines of <username>=<HA1>.
|
|
|
|
// Generate HA1 via "echo -n "<username>:<realm>:<password>" | md5sum"
|
|
|
|
std::string hex;
|
|
|
|
bool ret = file_.GetStringValue(username, &hex);
|
|
|
|
if (ret) {
|
|
|
|
char buf[32];
|
2014-07-29 19:36:52 +02:00
|
|
|
size_t len = rtc::hex_decode(buf, sizeof(buf), hex);
|
2013-07-10 02:45:36 +02:00
|
|
|
*key = std::string(buf, len);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
private:
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::OptionsFile file_;
|
2013-07-10 02:45:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
if (argc != 5) {
|
|
|
|
std::cerr << "usage: turnserver int-addr ext-ip realm auth-file"
|
|
|
|
<< std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::SocketAddress int_addr;
|
2013-07-10 02:45:36 +02:00
|
|
|
if (!int_addr.FromString(argv[1])) {
|
|
|
|
std::cerr << "Unable to parse IP address: " << argv[1] << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::IPAddress ext_addr;
|
2013-07-10 02:45:36 +02:00
|
|
|
if (!IPFromString(argv[2], &ext_addr)) {
|
|
|
|
std::cerr << "Unable to parse IP address: " << argv[2] << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::Thread* main = rtc::Thread::Current();
|
|
|
|
rtc::AsyncUDPSocket* int_socket =
|
|
|
|
rtc::AsyncUDPSocket::Create(main->socketserver(), int_addr);
|
2013-07-10 02:45:36 +02:00
|
|
|
if (!int_socket) {
|
|
|
|
std::cerr << "Failed to create a UDP socket bound at"
|
|
|
|
<< int_addr.ToString() << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
cricket::TurnServer server(main);
|
|
|
|
TurnFileAuth auth(argv[4]);
|
|
|
|
server.set_realm(argv[3]);
|
|
|
|
server.set_software(kSoftware);
|
|
|
|
server.set_auth_hook(&auth);
|
|
|
|
server.AddInternalSocket(int_socket, cricket::PROTO_UDP);
|
2014-07-29 19:36:52 +02:00
|
|
|
server.SetExternalSocketFactory(new rtc::BasicPacketSocketFactory(),
|
|
|
|
rtc::SocketAddress(ext_addr, 0));
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
std::cout << "Listening internally at " << int_addr.ToString() << std::endl;
|
|
|
|
|
|
|
|
main->Run();
|
|
|
|
return 0;
|
|
|
|
}
|