Trouble checking virtual server(vps) connection with qt - qt

I want to enter in the program, ip, username and password of a virtual server(vps), and the program checks that the username and password are correct or not.
I used the following code
void MainWindow::test()
{
QLoggingCategory::setFilterRules("qt.network.ssl.warning=false");
QNetworkAccessManager* manager = new QNetworkAccessManager(this);
QUrl url;
url.setHost("xx.xxx.xx.xx");
url.setPort(3389);
url.setUserName("administrator");
url.setPassword("xxxx");
manager->get(QNetworkRequest(url));
connect(manager,&QNetworkAccessManager::finished, this, &MainWindow::connFinished);
}
output:
QNetworkReply::NetworkError(ProtocolUnknownError)
please guide me

If you only want to know, if the host is online:
I would suggest you use connectToHost from QAbstractSocket Class. The RDP-Protocol is based on TCP/UDP according to Wikipedia and listens on 3389 (what you said in the comments.).
If the connection on TCP 3389 is successful, the host is online.
If you want to verify if the host is online and the authentication works you have to implement your own RDP-library such as FreeRDP, because Qt does not provide any core modules for RDP.

Related

Poco HTTPSClient session by IP address

I'm trying to use Poco HTTPS client session to download a site from hostname by specific IP address.
For example, google.com has the following addresses:
173.194.221.113
173.194.221.138
173.194.221.102
173.194.221.139
173.194.221.100
173.194.221.101
I want to get https://google.com via 173.194.221.102
I was trying the following approach:
std::make_unique<HTTPSClientSession>(SecureStreamSocket(SocketAddress(IPAddress("173.194.221.102"), 0), "https://google.com"));
This fails with the exception "Illegal state: Cannot set the port number for an already connected session"
Looking at the Poco source code, the SecureStreamSocket created with this constructor is connected, and the constructor of HTTPSClientSession tries to set the https port(443), and fails to do that with the already connected socket.
Any better way to do that?
Should be something like that:
Poco::Net::initializeSSL();
Poco::Net::HTTPSessionFactory::defaultFactory().registerProtocol("https", new Poco::Net::HTTPSSessionInstantiator);
const Poco::SharedPtr<Poco::Net::InvalidCertificateHandler> certificateHandler(new Poco::Net::AcceptCertificateHandler(false));
const Poco::Net::Context::Ptr context(new Poco::Net::Context(Poco::Net::Context::CLIENT_USE, ""));
Poco::Net::SSLManager::instance().initializeClient(nullptr, certificateHandler, context);
Poco::URI serverUri("your address");
Poco::Net::HTTPClientSession* session = Poco::Net::HTTPSessionFactory::defaultFactory().createClientSession(serverUri);
Hope it helps.

Connect to PostgreSQL server with "cert" authentication method using QSqlDatabase

I need to communicate with PostgreSQL database from my Qt application, but the server uses "cert" authentication method, so I need to pass my certificates to the server.
The only solution I see for now is to obtain PGconn* like this:
QSqlDatabase db;
//.....
PGconn* conn = (PGconn*)db.driver()->handle()->data();
and do some work with it. Or even reject QSqlDatabase and use libpq directly.
Is there any other way to do this without using libpq from my code? For example, something like this:
//hypothetic QSqlDatabase methods:
QSqlDatabase db;
//.....
db.SetSslCert("/path/to/my/cert.crt");
db.SetSslKey("/path/to/my/cert.key");
//.....
I need to pass my certificates to the server.
There's no function in Qt for that because there's no equivalent function in libpq for that either. It happens automatically, as described in Client Certificates inside SSL support from libpq documentation
Excerpt:
If the server requests a trusted client certificate, libpq will send
the certificate stored in file ~/.postgresql/postgresql.crt in the
user's home directory. The certificate must be signed by one of the
certificate authorities (CA) trusted by the server. A matching private
key file ~/.postgresql/postgresql.key must also be present
(in Windows, ~/.postgresql is going to be %APPDATA%\postgresql)
The same will happen for a Qt application since the Qt's QPSQL driver is built on top of libpq. The fact that the connection uses SSL and certificates is essentially transparent even for the driver itself.
EDIT: if ~/.posgresql is not convenient as when they are multiple certificates, alternatives exist:
The location of the certificate and key files can be overridden by the
connection parameters sslcert and sslkey or the environment variables
PGSSLCERT and PGSSLKEY
The connection parameters are set through QSqlDatabase::setConnectOptions. Despite its doc mentioning only an handful of postgresql-specific parameters, it actually will accept any parameter, so anything supported by libpq will work.

Nancy cannot load with wildcard url reservations

I have a desktop application that is self hosting a NancyFX web server. Being a desktop application there is a requirement that we allow dynamic IP addresses so we have registered the url using the wildcard option with netsh, as so:
netsh http add urlacl url=http://+:1234/ user=Everyone
However when this application is run under a non-administrator account the following exception is thrown.
The Nancy self host was unable to start, as no namespace reservation existed for the provided url(s).
Please either enable UrlReservations.CreateAutomatically on the HostConfiguration provided to
the NancyHost, or create the reservations manually with the (elevated) command(s):
netsh http add urlacl url=http://192.168.1.90:1234/ user=Everyone
I have tried many combinations of wildcard registrations, all with the same result. I also looked at registering the wildcard when loading Nancy, but due to Nancy using Uri types this was not valid.
I was under the assumption that by using the wildcard registration I had registered any ip address to be used. But Nancy seems to need the specific ip address registered.
I would really appreciate if someone could tell my why the wildcard registration will not work with Nancy, or even better, how to make it work with Nancy.
An old question but if anyone runs into this, Nancy SelfHost allows you to create Url reservations automatically using the HostConfiguration object.
The Url is then reserved automatically on startup.
//Nancy configuration
HostConfiguration hostConfig = new HostConfiguration()
{
UrlReservations = new UrlReservations()
{
//create URL reservations automatically
CreateAutomatically = true
}
};
//Uri
Uri uri = new Uri("http://localhost:9999");
using (var host = new NancyHost(hostConfig, uri))
{
host.Start();
Console.WriteLine("Running self-hosted server ...");
Console.WriteLine("Press [Enter] to close the application.");
Console.ReadLine();
}

Qt creator SSL enable

I have a program built with Qt creator. I need to send an https post to Google's C2DM servers.
When I try using QSslSocket, it says that my SSL is not working.
The Qt documentation states that it doesn't ship with SSL support for legal reasons, and QSslSocket is just a wrapper and that I need to install OpenSSL myself. I've tried copying the DLLs but it still doesn't work. Can someone walk me through the steps of installing OpenSSL and getting it to work with Qt Creator?
Why not use QNetworkAccessManager? I use something like this to post JSON to a web service:
void HttpPoster::post(){
if(!manager)
manager = new QNetworkAccessManager(this);
QSslConfiguration config = QSslConfiguration::defaultConfiguration();
config.setPeerVerifyMode(QSslSocket::VerifyNone);
//config.setProtocol(QSsl::TlsV1);
QNetworkRequest request ;
request.setUrl(QUrl("https://somehost.somedomain"));
request.setRawHeader("User-Agent", "MyApp");
request.setRawHeader("Content-type", "text/json");
request.setSslConfiguration(config);
if(!reply){
reply = manager->post(request,m_Data);
}
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(handleErrors(QNetworkReply::NetworkError)));
connect(reply,SIGNAL(sslErrors(QList<QSslError>)),this,SLOT(handleSSLErrors(QList<QSslError>)));
connect(reply,SIGNAL(finished()),this,SLOT(replyFinished()));
}
There's some methods omitted (handle errors, handle replies, handle ssl errors, etc). You can find much better examples in the docs.

WCF Certificate Authentication without installing on the Client

Our setup includes a WCF service and a number of clients written by us. Some of the clients include Silverlight applications, whereas others include Web and Windows applications.
I (think) I would like to authenticate clients based on X.509 certificates. Typically you would install a private key on the client to encrypt (aka digitaly sign) the messages. The server can the use the clients public key to de-crypt it to ensure the message has not been changed and prove the message is from who we expect (aka authenticated).
I dont want to install a certificate on a client machine. Its a hassel to deploy, and we cant really ask our clients to do it. I was speaking to someone the other day who sugested embeding the cert in a client assembly, reading it and using that. Is that possible?
It would be great if someone could point me to an example.
Thanks in advance,
David
Yes, you can load X509certificate2 by passing a certificate byte array with a password like
var certificate = new X509Certificate2(theByteArrary, "password");
To get the certificate byte array, you can simply copy paste the contents in .pfx file, which is a combination of .cer (public key) and .pvk (private key)
and then you can load this certificate on your client by doing:
var channelFactory = new ChannelFactory<IYourService>();
channelFactory.Credentials.ClientCertificate.Certificate =
clientCertificate;
If you use auto-generated client proxy, or you prefer configure the certificate via .config file then you might want to have a look at this from codeproject
Here is a suggestion. Could also be tweaked to use an embedded certificate.
http://www.codeproject.com/KB/WCF/wcfcertificates.aspx

Resources