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 "core/logging.h"
|
|
|
|
#include "covers/currentartloader.h"
|
|
|
|
#include "playlist/playlistmanager.h"
|
|
|
|
|
|
|
|
#include "networkremote.h"
|
|
|
|
|
2013-01-03 21:40:47 +01:00
|
|
|
#include <QDataStream>
|
2012-12-31 23:37:39 +01:00
|
|
|
#include <QSettings>
|
|
|
|
|
|
|
|
const char* NetworkRemote::kSettingsGroup = "NetworkRemote";
|
|
|
|
const int NetworkRemote::kDefaultServerPort = 5500;
|
2013-01-10 21:21:55 +01:00
|
|
|
const int NetworkRemote::kProtocolBufferVersion = 1;
|
2012-12-31 23:37:39 +01:00
|
|
|
|
|
|
|
NetworkRemote::NetworkRemote(Application* app)
|
|
|
|
: app_(app)
|
|
|
|
{
|
2013-01-09 20:07:28 +01:00
|
|
|
signals_connected_ = false;
|
2012-12-31 23:37:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NetworkRemote::~NetworkRemote() {
|
2013-01-09 20:07:28 +01:00
|
|
|
StopServer();
|
2013-01-03 21:40:47 +01:00
|
|
|
delete incoming_data_parser_;
|
|
|
|
delete outgoing_data_creator_;
|
2012-12-31 23:37:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkRemote::ReadSettings() {
|
|
|
|
QSettings s;
|
|
|
|
|
|
|
|
s.beginGroup(NetworkRemote::kSettingsGroup);
|
|
|
|
use_remote_ = s.value("use_remote").toBool();
|
|
|
|
port_ = s.value("port").toInt();
|
2013-01-10 21:21:55 +01:00
|
|
|
|
|
|
|
// Use only non public ips must be true be default
|
|
|
|
if (s.contains("only_non_public_ip")) {
|
|
|
|
only_non_public_ip_ = s.value("only_non_public_ip").toBool();
|
|
|
|
} else {
|
|
|
|
only_non_public_ip_ = true;
|
|
|
|
}
|
|
|
|
|
2012-12-31 23:37:39 +01:00
|
|
|
if (port_ == 0) {
|
|
|
|
port_ = kDefaultServerPort;
|
|
|
|
}
|
|
|
|
s.endGroup();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkRemote::SetupServer() {
|
|
|
|
server_ = new QTcpServer();
|
2013-01-10 21:21:55 +01:00
|
|
|
server_ipv6_ = new QTcpServer();
|
2013-01-03 21:40:47 +01:00
|
|
|
incoming_data_parser_ = new IncomingDataParser(app_);
|
|
|
|
outgoing_data_creator_ = new OutgoingDataCreator(app_);
|
2012-12-31 23:37:39 +01:00
|
|
|
|
2013-01-09 20:07:28 +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&)),
|
2013-01-03 21:40:47 +01:00
|
|
|
outgoing_data_creator_,
|
2012-12-31 23:37:39 +01:00
|
|
|
SLOT(CurrentSongChanged(const Song&, const QString&, const QImage&)));
|
|
|
|
}
|
|
|
|
|
|
|
|
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";
|
|
|
|
|
|
|
|
connect(server_, SIGNAL(newConnection()), this, SLOT(AcceptConnection()));
|
2013-01-10 21:21:55 +01:00
|
|
|
connect(server_ipv6_, SIGNAL(newConnection()), this, SLOT(AcceptConnection()));
|
2012-12-31 23:37:39 +01:00
|
|
|
|
|
|
|
server_->listen(QHostAddress::Any, port_);
|
2013-01-10 21:21:55 +01:00
|
|
|
server_ipv6_->listen(QHostAddress::AnyIPv6, port_);
|
2012-12-31 23:37:39 +01:00
|
|
|
|
|
|
|
qLog(Info) << "Listening on port " << port_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkRemote::StopServer() {
|
|
|
|
if (server_->isListening()) {
|
|
|
|
server_->close();
|
2013-01-10 21:21:55 +01:00
|
|
|
server_ipv6_->close();
|
2013-01-09 20:07:28 +01:00
|
|
|
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() {
|
2013-01-09 20:07:28 +01:00
|
|
|
if (!signals_connected_) {
|
|
|
|
signals_connected_ = true;
|
2012-12-31 23:37:39 +01:00
|
|
|
|
|
|
|
// Setting up the signals, but only once
|
2013-01-03 21:40:47 +01:00
|
|
|
connect(incoming_data_parser_, SIGNAL(SendClementineInfos()),
|
|
|
|
outgoing_data_creator_, SLOT(SendClementineInfos()));
|
|
|
|
connect(incoming_data_parser_, SIGNAL(SendFirstData()),
|
|
|
|
outgoing_data_creator_, SLOT(SendFirstData()));
|
|
|
|
connect(incoming_data_parser_, SIGNAL(SendAllPlaylists()),
|
|
|
|
outgoing_data_creator_, SLOT(SendAllPlaylists()));
|
|
|
|
connect(incoming_data_parser_, SIGNAL(SendPlaylistSongs(int)),
|
|
|
|
outgoing_data_creator_, SLOT(SendPlaylistSongs(int)));
|
2012-12-31 23:37:39 +01:00
|
|
|
|
|
|
|
connect(app_->playlist_manager(), SIGNAL(ActiveChanged(Playlist*)),
|
2013-01-03 21:40:47 +01:00
|
|
|
outgoing_data_creator_, SLOT(ActiveChanged(Playlist*)));
|
2012-12-31 23:37:39 +01:00
|
|
|
connect(app_->playlist_manager(), SIGNAL(PlaylistChanged(Playlist*)),
|
2013-01-03 21:40:47 +01:00
|
|
|
outgoing_data_creator_, SLOT(PlaylistChanged(Playlist*)));
|
2012-12-31 23:37:39 +01:00
|
|
|
|
2013-01-03 21:40:47 +01:00
|
|
|
connect(app_->player(), SIGNAL(VolumeChanged(int)), outgoing_data_creator_,
|
2012-12-31 23:37:39 +01:00
|
|
|
SLOT(VolumeChanged(int)));
|
|
|
|
connect(app_->player()->engine(), SIGNAL(StateChanged(Engine::State)),
|
2013-01-03 21:40:47 +01:00
|
|
|
outgoing_data_creator_, SLOT(StateChanged(Engine::State)));
|
2012-12-31 23:37:39 +01:00
|
|
|
}
|
|
|
|
|
2013-01-10 21:21:55 +01:00
|
|
|
if (server_->hasPendingConnections()) {
|
|
|
|
QTcpSocket* client_socket = server_->nextPendingConnection();
|
|
|
|
// Check if our ip is in private scope
|
|
|
|
if (only_non_public_ip_
|
2013-01-11 13:55:09 +01:00
|
|
|
&& !IpIsPrivate(client_socket->peerAddress())) {
|
2013-01-10 21:21:55 +01:00
|
|
|
qLog(Info) << "Got a connection from public ip" <<
|
|
|
|
client_socket->peerAddress().toString();
|
|
|
|
} else {
|
|
|
|
CreateRemoteClient(client_socket);
|
|
|
|
// TODO: Check private ips for ipv6
|
2013-01-09 17:38:24 +01:00
|
|
|
}
|
|
|
|
|
2013-01-10 21:21:55 +01:00
|
|
|
} else {
|
|
|
|
// No checks on ipv6
|
|
|
|
CreateRemoteClient(server_ipv6_->nextPendingConnection());
|
2013-01-09 17:38:24 +01:00
|
|
|
}
|
2013-01-09 20:07:28 +01:00
|
|
|
}
|
2013-01-09 17:38:24 +01:00
|
|
|
|
2013-01-11 13:55:09 +01:00
|
|
|
bool NetworkRemote::IpIsPrivate(const QHostAddress& address) {
|
|
|
|
return
|
|
|
|
address.isInSubnet(QHostAddress::parseSubnet("127.0.0.1/8")) ||
|
|
|
|
// Link Local v6
|
|
|
|
address.isInSubnet(QHostAddress::parseSubnet("::1/128")) ||
|
|
|
|
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 v6 range
|
|
|
|
address.isInSubnet(QHostAddress::parseSubnet("fc00::/7"));
|
2013-01-10 21:21:55 +01:00
|
|
|
}
|
2013-01-09 17:38:24 +01:00
|
|
|
|
2013-01-10 21:21:55 +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
|
|
|
|
2013-01-10 21:21:55 +01:00
|
|
|
// Connect the signal to parse data
|
|
|
|
connect(client, SIGNAL(Parse(QByteArray)),
|
|
|
|
incoming_data_parser_, SLOT(Parse(QByteArray)));
|
|
|
|
}
|
2012-12-31 23:37:39 +01:00
|
|
|
}
|