Skip to content

Commit

Permalink
WebSocket での接続時に User-Agent ヘッダーを追加する
Browse files Browse the repository at this point in the history
  • Loading branch information
melpon committed Jul 23, 2024
1 parent 524d49b commit 1843312
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
- @melpon
- [ADD] Ubuntu 24.04 に対応する
- @melpon
- [ADD] WebSocket での接続時に User-Agent ヘッダーを追加する
- @melpon

## 2024.6.1 (2024-04-16)

Expand Down
7 changes: 5 additions & 2 deletions include/sora/sora_signaling.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
#include <api/peer_connection_interface.h>
#include <api/scoped_refptr.h>

#include "data_channel.h"
#include "websocket.h"
#include "sora/data_channel.h"
#include "sora/version.h"
#include "sora/websocket.h"

namespace sora {

Expand Down Expand Up @@ -134,6 +135,8 @@ struct SoraSignalingConfig {
rtc::PacketSocketFactory* socket_factory = nullptr;

bool disable_signaling_url_randomization = false;

boost::optional<http_header_value> user_agent;
};

class SoraSignaling : public std::enable_shared_from_this<SoraSignaling>,
Expand Down
6 changes: 6 additions & 0 deletions include/sora/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@

#include <string>

// Boost
#include <boost/optional.hpp>

namespace sora {

typedef boost::optional<std::string> http_header_value;

class Version {
public:
static std::string GetClientName();
static std::string GetLibwebrtcName();
static std::string GetEnvironmentName();
static http_header_value GetDefaultUserAgent();
};

} // namespace sora
Expand Down
5 changes: 5 additions & 0 deletions include/sora/websocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <boost/beast/websocket/stream.hpp>

#include "url_parts.h"
#include "version.h"

namespace sora {

Expand Down Expand Up @@ -67,6 +68,8 @@ class Websocket {
Websocket(boost::asio::ip::tcp::socket socket);
~Websocket();

void SetUserAgent(http_header_value user_agent);

// WebSocket クライアントの接続確立
void Connect(const std::string& url, connect_callback_t on_connect);

Expand Down Expand Up @@ -144,6 +147,8 @@ class Websocket {

boost::asio::deadline_timer close_timeout_timer_;

http_header_value user_agent_;

bool https_proxy_ = false;
std::string proxy_url_;
std::string proxy_username_;
Expand Down
3 changes: 3 additions & 0 deletions src/sora_signaling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1178,6 +1178,9 @@ void SoraSignaling::DoConnect() {
} else {
ws.reset(new Websocket(*config_.io_context));
}
if (config_.user_agent != boost::none) {
ws->SetUserAgent(*config_.user_agent);
}
ws->Connect(url, std::bind(&SoraSignaling::OnConnect, shared_from_this(),
std::placeholders::_1, url, ws));
connecting_wss_.push_back(ws);
Expand Down
6 changes: 6 additions & 0 deletions src/version.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@

#endif

#define DEFAULT_USER_AGENT "Mozilla/5.0 (Sora C++ SDK/" SORA_CPP_SDK_VERSION ")"

namespace sora {

std::string Version::GetClientName() {
Expand Down Expand Up @@ -186,4 +188,8 @@ std::string Version::GetEnvironmentName() {
return environment;
}

http_header_value Version::GetDefaultUserAgent() {
return std::string(DEFAULT_USER_AGENT);
}

} // namespace sora
31 changes: 27 additions & 4 deletions src/websocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <boost/beast/websocket/stream.hpp>

#include "sora/ssl_verifier.h"
#include "sora/version.h"

namespace sora {

Expand Down Expand Up @@ -47,7 +48,8 @@ Websocket::Websocket(boost::asio::io_context& ioc)
: ws_(new websocket_t(ioc)),
resolver_(new boost::asio::ip::tcp::resolver(ioc)),
strand_(ws_->get_executor()),
close_timeout_timer_(ioc) {
close_timeout_timer_(ioc),
user_agent_(Version::GetDefaultUserAgent()) {
ws_->write_buffer_bytes(8192);
}
Websocket::Websocket(Websocket::ssl_tag,
Expand All @@ -58,15 +60,17 @@ Websocket::Websocket(Websocket::ssl_tag,
: resolver_(new boost::asio::ip::tcp::resolver(ioc)),
strand_(ioc.get_executor()),
close_timeout_timer_(ioc),
insecure_(insecure) {
insecure_(insecure),
user_agent_(Version::GetDefaultUserAgent()) {
ssl_ctx_ = CreateSSLContext(client_cert, client_key);
wss_.reset(new ssl_websocket_t(ioc, *ssl_ctx_));
InitWss(wss_.get(), insecure);
}
Websocket::Websocket(boost::asio::ip::tcp::socket socket)
: ws_(new websocket_t(std::move(socket))),
strand_(ws_->get_executor()),
close_timeout_timer_(ws_->get_executor()) {
close_timeout_timer_(ws_->get_executor()),
user_agent_(Version::GetDefaultUserAgent()) {
ws_->write_buffer_bytes(8192);
}
Websocket::Websocket(https_proxy_tag,
Expand All @@ -84,14 +88,19 @@ Websocket::Websocket(https_proxy_tag,
proxy_socket_(new boost::asio::ip::tcp::socket(ioc)),
proxy_url_(std::move(proxy_url)),
proxy_username_(std::move(proxy_username)),
proxy_password_(std::move(proxy_password)) {
proxy_password_(std::move(proxy_password)),
user_agent_(Version::GetDefaultUserAgent()) {
ssl_ctx_ = CreateSSLContext(client_cert, client_key);
}

Websocket::~Websocket() {
RTC_LOG(LS_INFO) << "Websocket::~Websocket this=" << (void*)this;
}

void Websocket::SetUserAgent(http_header_value user_agent) {
user_agent_ = user_agent;
}

bool Websocket::IsSSL() const {
return https_proxy_ || wss_ != nullptr;
}
Expand Down Expand Up @@ -161,6 +170,20 @@ void Websocket::Connect(const std::string& url, connect_callback_t on_connect) {
}
}

// ヘッダーの設定
auto set_headers = [this](boost::beast::websocket::request_type& req) {
if (user_agent_ != boost::none) {
req.set(boost::beast::http::field::user_agent, *user_agent_);
}
};
if (IsSSL()) {
wss_->set_option(
boost::beast::websocket::stream_base::decorator(set_headers));
} else {
ws_->set_option(
boost::beast::websocket::stream_base::decorator(set_headers));
}

on_connect_ = std::move(on_connect);

// DNS ルックアップ
Expand Down

0 comments on commit 1843312

Please sign in to comment.