1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
// Copyright (c) 2011-2016 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "guiutil.h"
#if defined(HAVE_CONFIG_H)
#include "config/bitcoin-config.h"
#endif
#include <iostream>
#include <string>
#include "peerdialog.h"
#include "ui_addpeerdialog.h"
#include "ui_testpeerdialog.h"
#include "net.h"
#include "net_processing.h"
#include "netbase.h"
#include "protocol.h"
#include "chainparams.h"
#include "util.h"
#include <stdio.h>
#include <QMessageBox>
#include <QHostAddress>
#include <QAbstractSocket>
#include <QUrl>
/** Function to manage peers */
QString PeerTools::ManagePeer(QString type, QString peer)
{
std::string peerAddress = peer.toStdString();
if(!g_connman)
return tr("Error: Peer-to-peer functionality missing or disabled");
if (type == "onetry")
{
CAddress addr;
g_connman->OpenNetworkConnection(addr, false, NULL, peerAddress.c_str());
return tr("Attempted to one try node.");
}
if (type == "add")
{
if(!g_connman->AddNode(peerAddress))
return tr("Error: Node already added");
}
else if(type == "remove")
{
if(!g_connman->RemoveAddedNode(peerAddress))
{
if(!g_connman->DisconnectNode(peerAddress))
return tr("Node not found in connected nodes");
return tr("Disconnected the node: ") + peer;
}
else
{
if(!g_connman->DisconnectNode(peerAddress))
return tr("Node not found in connected nodes");
}
}
return tr("Returned OK.");
}
/** Check if Peer is valid */
bool PeerTools::CheckPeerAddress(QString address)
{
CNetAddr addr;
return LookupHost(address.toStdString().c_str(), addr, true);
}
/** Get port based on current chain */
QString PeerTools::GetPort()
{
return QString::number(Params().GetDefaultPort());
}
/** Add Peer Dialog */
AddPeerDialog::AddPeerDialog(QWidget *parent) :
QWidget(parent),
ui(new Ui::AddPeerDialog)
{
ui->setupUi(this);
ui->peerPort->setValidator( new QIntValidator(1, 65535, this) );
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(on_addPeer_clicked()));
}
AddPeerDialog::~AddPeerDialog()
{
delete ui;
}
void AddPeerDialog::on_addPeer_clicked()
{
QString address = ui->peerAddress->text();
QString port = ui->peerPort->text();
QString data = "";
if(address.isEmpty())
{
QMessageBox::critical(this, tr("Add Peer"), tr("Please enter an address."), QMessageBox::Ok, QMessageBox::Ok);
return;
}
if(port.isEmpty())
{
port = PeerTools::GetPort();
ui->peerPort->setText(port);
}
if(!PeerTools::CheckPeerAddress(address))
{
QMessageBox::critical(this, tr("Add Peer"), tr("Please enter a valid peer address."), QMessageBox::Ok, QMessageBox::Ok);
return;
}
data = address + ":" + port;
if(QMessageBox::Ok == QMessageBox::information(this, tr("Add Peer"), PeerTools::ManagePeer("add", data), QMessageBox::Ok, QMessageBox::Ok))
this->close();
}
/** Add Test Peer Dialog */
TestPeerDialog::TestPeerDialog(QWidget *parent) :
QWidget(parent),
ui(new Ui::TestPeerDialog)
{
ui->setupUi(this);
ui->peerPort->setValidator( new QIntValidator(1, 65535, this) );
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(on_testPeer_clicked()));
}
TestPeerDialog::~TestPeerDialog()
{
delete ui;
}
void TestPeerDialog::on_testPeer_clicked()
{
QString address = ui->peerAddress->text();
QString port = ui->peerPort->text();
QString data = "";
if(address.isEmpty())
{
QMessageBox::critical(this, tr("Test Peer"), tr("Please enter an address."), QMessageBox::Ok, QMessageBox::Ok);
return;
}
if(port.isEmpty())
{
port = PeerTools::GetPort();
ui->peerPort->setText(port);
}
if(!PeerTools::CheckPeerAddress(address))
{
QMessageBox::critical(this, tr("Test Peer"), tr("Please enter a valid peer address."), QMessageBox::Ok, QMessageBox::Ok);
return;
}
data = address + ":" + port;
if(QMessageBox::Ok == QMessageBox::information(this, tr("Try Peer"), PeerTools::ManagePeer("onetry", data), QMessageBox::Ok, QMessageBox::Ok))
this->close();
}
|