Clementine-audio-player-Mac.../src/networkremote/networkremote.cpp

249 lines
9.7 KiB
C++
Raw Normal View History

2012-12-31 23:37:39 +01:00
/* This file is part of Clementine.
Copyright 2012, Andreas Muttscheller <asfa194@gmail.com>
Clementine is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Clementine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
*/
#include "networkremote.h"
2013-01-03 21:40:47 +01:00
#include <QDataStream>
#include <QHostInfo>
2013-07-31 17:28:11 +02:00
#include <QNetworkProxy>
2020-09-18 16:15:19 +02:00
#include <QSettings>
#include <QTcpServer>
#include "core/logging.h"
#include "covers/currentartloader.h"
#include "networkremote/incomingdataparser.h"
#include "networkremote/outgoingdatacreator.h"
#include "networkremote/zeroconf.h"
#include "playlist/playlistmanager.h"
2012-12-31 23:37:39 +01:00
const char* NetworkRemote::kSettingsGroup = "NetworkRemote";
const quint16 NetworkRemote::kDefaultServerPort = 5500;
const char* NetworkRemote::kTranscoderSettingPostfix = "/NetworkRemote";
2012-12-31 23:37:39 +01:00
NetworkRemote::NetworkRemote(Application* app, QObject* parent)
: QObject(parent), signals_connected_(false), app_(app) {
setObjectName("Network remote");
}
2012-12-31 23:37:39 +01:00
NetworkRemote::~NetworkRemote() { StopServer(); }
2012-12-31 23:37:39 +01:00
void NetworkRemote::ReadSettings() {
QSettings s;
s.beginGroup(NetworkRemote::kSettingsGroup);
use_remote_ = s.value("use_remote", false).toBool();
port_ = s.value("port", kDefaultServerPort).toInt();
// Use only non public ips must be true be default
only_non_public_ip_ = s.value("only_non_public_ip", true).toBool();
2012-12-31 23:37:39 +01:00
s.endGroup();
}
void NetworkRemote::SetupServer() {
server_.reset(new QTcpServer());
server_ipv6_.reset(new QTcpServer());
incoming_data_parser_.reset(new IncomingDataParser(app_));
outgoing_data_creator_.reset(new OutgoingDataCreator(app_));
2012-12-31 23:37:39 +01:00
outgoing_data_creator_->SetClients(&clients_);
2012-12-31 23:37:39 +01:00
connect(app_->current_art_loader(),
SIGNAL(ArtLoaded(const Song&, const QString&, const QImage&)),
outgoing_data_creator_.get(),
2012-12-31 23:37:39 +01:00
SLOT(CurrentSongChanged(const Song&, const QString&, const QImage&)));
// Only connect the signals once
connect(server_.get(), SIGNAL(newConnection()), this,
SLOT(AcceptConnection()));
connect(server_ipv6_.get(), SIGNAL(newConnection()), this,
SLOT(AcceptConnection()));
connect(incoming_data_parser_.get(), SIGNAL(AddToPlaylistSignal(QMimeData*)),
SIGNAL(AddToPlaylistSignal(QMimeData*)));
connect(incoming_data_parser_.get(), SIGNAL(SetCurrentPlaylist(int)),
SIGNAL(SetCurrentPlaylist(int)));
2012-12-31 23:37:39 +01:00
}
void NetworkRemote::StartServer() {
if (!app_) {
2013-01-03 21:40:47 +01:00
qLog(Error) << "Start Server called without having an application!";
2012-12-31 23:37:39 +01:00
return;
}
// Check if user desires to start a network remote server
ReadSettings();
if (!use_remote_) {
qLog(Info) << "Network Remote deactivated";
return;
}
qLog(Info) << "Starting network remote";
2013-07-31 17:28:11 +02:00
server_->setProxy(QNetworkProxy::NoProxy);
server_ipv6_->setProxy(QNetworkProxy::NoProxy);
2012-12-31 23:37:39 +01:00
server_->listen(QHostAddress::Any, port_);
server_ipv6_->listen(QHostAddress::AnyIPv6, port_);
2012-12-31 23:37:39 +01:00
qLog(Info) << "Listening on port " << port_;
if (Zeroconf::GetZeroconf()) {
QString name = QString("Clementine on %1").arg(QHostInfo::localHostName());
Zeroconf::GetZeroconf()->Publish("local", "_clementine._tcp", name, port_);
}
2012-12-31 23:37:39 +01:00
}
void NetworkRemote::StopServer() {
if (server_->isListening()) {
outgoing_data_creator_.get()->DisconnectAllClients();
2012-12-31 23:37:39 +01:00
server_->close();
server_ipv6_->close();
2013-02-18 14:53:37 +01:00
qDeleteAll(clients_);
clients_.clear();
2012-12-31 23:37:39 +01:00
}
}
void NetworkRemote::ReloadSettings() {
2013-01-03 21:40:47 +01:00
StopServer();
2012-12-31 23:37:39 +01:00
StartServer();
}
void NetworkRemote::AcceptConnection() {
if (!signals_connected_) {
signals_connected_ = true;
2012-12-31 23:37:39 +01:00
// Setting up the signals, but only once
2013-01-15 14:24:59 +01:00
connect(incoming_data_parser_.get(), SIGNAL(SendClementineInfo()),
outgoing_data_creator_.get(), SLOT(SendClementineInfo()));
2013-03-27 16:54:02 +01:00
connect(incoming_data_parser_.get(), SIGNAL(SendFirstData(bool)),
outgoing_data_creator_.get(), SLOT(SendFirstData(bool)));
connect(incoming_data_parser_.get(), SIGNAL(SendAllPlaylists()),
outgoing_data_creator_.get(), SLOT(SendAllPlaylists()));
connect(incoming_data_parser_.get(), SIGNAL(SendAllActivePlaylists()),
outgoing_data_creator_.get(), SLOT(SendAllActivePlaylists()));
connect(incoming_data_parser_.get(), SIGNAL(SendPlaylistSongs(int)),
outgoing_data_creator_.get(), SLOT(SendPlaylistSongs(int)));
2012-12-31 23:37:39 +01:00
connect(app_->playlist_manager(), SIGNAL(ActiveChanged(Playlist*)),
outgoing_data_creator_.get(), SLOT(ActiveChanged(Playlist*)));
2012-12-31 23:37:39 +01:00
connect(app_->playlist_manager(), SIGNAL(PlaylistChanged(Playlist*)),
outgoing_data_creator_.get(), SLOT(PlaylistChanged(Playlist*)));
connect(app_->playlist_manager(), SIGNAL(PlaylistAdded(int, QString, bool)),
outgoing_data_creator_.get(),
SLOT(PlaylistAdded(int, QString, bool)));
connect(app_->playlist_manager(), SIGNAL(PlaylistRenamed(int, QString)),
outgoing_data_creator_.get(), SLOT(PlaylistRenamed(int, QString)));
connect(app_->playlist_manager(), SIGNAL(PlaylistClosed(int)),
outgoing_data_creator_.get(), SLOT(PlaylistClosed(int)));
connect(app_->playlist_manager(), SIGNAL(PlaylistDeleted(int)),
outgoing_data_creator_.get(), SLOT(PlaylistDeleted(int)));
2012-12-31 23:37:39 +01:00
connect(app_->player(), SIGNAL(VolumeChanged(int)),
outgoing_data_creator_.get(), SLOT(VolumeChanged(int)));
2012-12-31 23:37:39 +01:00
connect(app_->player()->engine(), SIGNAL(StateChanged(Engine::State)),
outgoing_data_creator_.get(), SLOT(StateChanged(Engine::State)));
2013-01-15 14:43:02 +01:00
connect(app_->playlist_manager()->sequence(),
SIGNAL(RepeatModeChanged(PlaylistSequence::RepeatMode)),
outgoing_data_creator_.get(),
SLOT(SendRepeatMode(PlaylistSequence::RepeatMode)));
connect(app_->playlist_manager()->sequence(),
SIGNAL(ShuffleModeChanged(PlaylistSequence::ShuffleMode)),
outgoing_data_creator_.get(),
SLOT(SendShuffleMode(PlaylistSequence::ShuffleMode)));
connect(incoming_data_parser_.get(), SIGNAL(GetLyrics()),
outgoing_data_creator_.get(), SLOT(GetLyrics()));
connect(incoming_data_parser_.get(), SIGNAL(SendLibrary(RemoteClient*)),
outgoing_data_creator_.get(), SLOT(SendLibrary(RemoteClient*)));
2014-10-21 17:59:02 +02:00
connect(incoming_data_parser_.get(),
SIGNAL(DoGlobalSearch(QString, RemoteClient*)),
outgoing_data_creator_.get(),
SLOT(DoGlobalSearch(QString, RemoteClient*)));
connect(incoming_data_parser_.get(),
SIGNAL(SendListFiles(QString, RemoteClient*)),
outgoing_data_creator_.get(),
SLOT(SendListFiles(QString, RemoteClient*)));
connect(incoming_data_parser_.get(), SIGNAL(SendSavedRadios(RemoteClient*)),
outgoing_data_creator_.get(), SLOT(SendSavedRadios(RemoteClient*)));
2012-12-31 23:37:39 +01:00
}
QTcpServer* server = qobject_cast<QTcpServer*>(sender());
QTcpSocket* client_socket = server->nextPendingConnection();
// Check if our ip is in private scope
if (only_non_public_ip_ && !IpIsPrivate(client_socket->peerAddress())) {
qLog(Info) << "Got a connection from public ip"
<< client_socket->peerAddress().toString();
client_socket->close();
2015-03-24 10:18:20 +01:00
client_socket->deleteLater();
} else {
CreateRemoteClient(client_socket);
2013-01-09 17:38:24 +01:00
}
}
2013-01-09 17:38:24 +01:00
2013-01-12 17:22:40 +01:00
bool NetworkRemote::IpIsPrivate(const QHostAddress& address) {
return
// Localhost v4
address.isInSubnet(QHostAddress::parseSubnet("127.0.0.0/8")) ||
// Link Local v4
address.isInSubnet(QHostAddress::parseSubnet("169.254.1.0/16")) ||
2013-01-12 17:22:40 +01:00
// Link Local v6
address.isInSubnet(QHostAddress::parseSubnet("::1/128")) ||
address.isInSubnet(QHostAddress::parseSubnet("fe80::/10")) ||
// Private v4 range
2013-01-12 17:22:40 +01:00
address.isInSubnet(QHostAddress::parseSubnet("192.168.0.0/16")) ||
address.isInSubnet(QHostAddress::parseSubnet("172.16.0.0/12")) ||
address.isInSubnet(QHostAddress::parseSubnet("10.0.0.0/8")) ||
// Private v4 range translated to v6
address.isInSubnet(QHostAddress::parseSubnet("::ffff:192.168.0.0/112")) ||
address.isInSubnet(QHostAddress::parseSubnet("::ffff:172.16.0.0/108")) ||
address.isInSubnet(QHostAddress::parseSubnet("::ffff:10.0.0.0/104")) ||
2013-01-12 17:22:40 +01:00
// Private v6 range
address.isInSubnet(QHostAddress::parseSubnet("fc00::/7"));
}
2013-01-09 17:38:24 +01:00
void NetworkRemote::CreateRemoteClient(QTcpSocket* client_socket) {
if (client_socket) {
// Add the client to the list
RemoteClient* client = new RemoteClient(app_, client_socket);
clients_.push_back(client);
2013-01-09 17:38:24 +01:00
// Update the Remote Root Files for the latest Client
outgoing_data_creator_->SetMusicExtensions(
client->files_music_extensions());
outgoing_data_creator_->SetRemoteRootFiles(client->files_root_folder());
incoming_data_parser_->SetRemoteRootFiles(client->files_root_folder());
// update OutgoingDataCreator with latest allow_downloads setting
outgoing_data_creator_->SetAllowDownloads(client->allow_downloads());
// Connect the signal to parse data
connect(client, SIGNAL(Parse(cpb::remote::Message)),
incoming_data_parser_.get(), SLOT(Parse(cpb::remote::Message)));
}
2012-12-31 23:37:39 +01:00
}
2013-12-22 15:16:42 +01:00
void NetworkRemote::EnableKittens(bool aww) {
if (outgoing_data_creator_.get()) outgoing_data_creator_->EnableKittens(aww);
2013-12-22 15:16:42 +01:00
}
void NetworkRemote::SendKitten(quint64 id, const QImage& kitten) {
if (outgoing_data_creator_.get()) outgoing_data_creator_->SendKitten(kitten);
2013-12-22 15:16:42 +01:00
}