Add proxy support for S3 upload

This commit is contained in:
Yuriy Puchkov
2020-07-16 17:55:42 +03:00
parent 4ef48aec32
commit b2dbf522d9
5 changed files with 97 additions and 26 deletions

View File

@@ -239,6 +239,37 @@ There are packages available for a few distros:
</a>
</details>
### S3 bucket configuration
S3 bucket credentials are placed in the file `config.ini` and cannot be configured with UI.
This file is not included into installation and should be placed into the installation directory manually.
On `linux` systems it can be placed at `/etc/flameshot/config.ini`.
You can also add `proxy` server settings. If you don't need a proxy server just remove or comment lines with a proxy settings.
Configuration file example:
```
[General]
HTTP_PROXY_HOST=10.0.0.1
HTTP_PROXY_PORT=3128
; No authentification USER and PASSWORD should be empty
HTTP_PROXY_USER=
HTTP_PROXY_PASSWORD=
HTTP_PROXY_TYPE=0
; Proxy Types (0 is default):
; 0 Proxy is determined based on the application proxy set using setApplicationProxy()
; 1 Socks5 proxying is used
; 3 HTTP transparent proxying is used
; 4 Proxying for HTTP requests only
; 5 Proxying for FTP requests only
[S3]
S3_CREDS_URL=https://api.img.example.com/
S3_X_API_KEY=seckret_key
```
## Compilation
To build the application in your system, you'll need to install the dependencies needed for it and Package names might be different for each distribution, see [Dependencies](#dependencies) below for more information.

View File

@@ -41,15 +41,25 @@
#include <QJsonDocument>
#include <QJsonObject>
#include <QHttpMultiPart>
#include <QNetworkProxy>
#include <QDir>
ImgS3Uploader::ImgS3Uploader(const QPixmap &capture,
const QString &s3CredsUrl,
const QString &s3XApiKey,
QWidget *parent) :
ImgS3Uploader::ImgS3Uploader(const QPixmap &capture, QWidget *parent) :
QWidget(parent), m_pixmap(capture)
{
m_s3CredsUrl = s3CredsUrl;
m_s3XApiKey = s3XApiKey;
QSettings *pSettings = nullptr;
QString configIniPath = QDir(QDir::currentPath()).filePath("config.ini");
#if defined(Q_OS_LINUX) || defined(Q_OS_UNIX)
if(!(QFileInfo::exists(configIniPath) && QFileInfo(configIniPath).isFile())) {
configIniPath = "/etc/flameshot/config.ini";
}
#endif
pSettings = new QSettings(configIniPath, QSettings::IniFormat);
pSettings->beginGroup("S3");
m_s3CredsUrl = pSettings->value("S3_CREDS_URL").toString();
m_s3XApiKey = pSettings->value("S3_X_API_KEY").toString();
pSettings->endGroup();
setWindowTitle(tr("Upload to ImgS3"));
setWindowIcon(QIcon(":img/app/flameshot.svg"));
@@ -73,6 +83,52 @@ ImgS3Uploader::ImgS3Uploader(const QPixmap &capture,
setAttribute(Qt::WA_DeleteOnClose);
QString httpProxyHost = pSettings->value("HTTP_PROXY_HOST").toString();
if(httpProxyHost.length() > 0) {
qDebug() << "Using proxy server";
m_proxy = new QNetworkProxy();
if(pSettings->contains("HTTP_PROXY_TYPE")) {
switch (pSettings->value("HTTP_PROXY_TYPE").toInt()) {
case 0:
m_proxy->setType(QNetworkProxy::DefaultProxy);
break;
case 1:
m_proxy->setType(QNetworkProxy::Socks5Proxy);
break;
case 2:
m_proxy->setType(QNetworkProxy::NoProxy);
break;
case 4:
m_proxy->setType(QNetworkProxy::HttpCachingProxy);
break;
case 5:
m_proxy->setType(QNetworkProxy::FtpCachingProxy);
break;
case 3:
default:
m_proxy->setType(QNetworkProxy::HttpProxy);
break;
}
}
m_proxy->setHostName(httpProxyHost);
if(pSettings->contains("HTTP_PROXY_PORT")) {
m_proxy->setPort(pSettings->value("HTTP_PROXY_PORT").toInt());
} else {
m_proxy->setPort(3128);
}
if(pSettings->contains("HTTP_PROXY_USER")) {
m_proxy->setUser(pSettings->value("HTTP_PROXY_USER").toString());
}
if(pSettings->contains("HTTP_PROXY_PASSWORD")) {
m_proxy->setPassword(pSettings->value("HTTP_PROXY_PASSWORD").toString());
}
QNetworkProxy::setApplicationProxy(*m_proxy);
m_NetworkAM->setProxy(*m_proxy);
}
upload();
}

View File

@@ -21,6 +21,7 @@
#include <QUrl>
class QNetworkReply;
class QNetworkProxy;
class QNetworkAccessManager;
class QHBoxLayout;
class QVBoxLayout;
@@ -33,11 +34,7 @@ class NotificationWidget;
class ImgS3Uploader : public QWidget {
Q_OBJECT
public:
explicit ImgS3Uploader(const QPixmap &capture,
const QString &s3CredsUrl,
const QString &s3XApiKey,
QWidget *parent = nullptr
);
explicit ImgS3Uploader(const QPixmap &capture, QWidget *parent = nullptr);
private slots:
void handleReply(QNetworkReply *reply);
@@ -57,6 +54,7 @@ private:
QString m_hostName;
QPixmap m_pixmap;
QNetworkProxy *m_proxy;
QNetworkAccessManager *m_NetworkAM;
QNetworkAccessManager *m_NetworkAMCreds;

View File

@@ -17,24 +17,12 @@
#include "imgs3uploadertool.h"
#include "imgs3uploader.h"
#include <QDir>
#include <QPainter>
#include <QSettings>
#include <QFileInfo>
ImgS3UploaderTool::ImgS3UploaderTool(QObject *parent) : AbstractActionTool(parent) {
QSettings *pSettings = nullptr;
QString configIniPath = QDir::currentPath() + "/config.ini";
#if defined(Q_OS_LINUX) || defined(Q_OS_UNIX)
if(!(QFileInfo::exists(configIniPath) && QFileInfo(configIniPath).isFile())) {
configIniPath = "/etc/flameshot/config.ini";
}
#endif
pSettings = new QSettings(configIniPath, QSettings::IniFormat);
pSettings->beginGroup("S3");
m_s3CredsUrl = pSettings->value("S3_CREDS_URL").toString();
m_s3XApiKey = pSettings->value("S3_X_API_KEY").toString();
}
bool ImgS3UploaderTool::closeOnButtonPressed() const {
@@ -59,7 +47,7 @@ QString ImgS3UploaderTool::description() const {
}
QWidget *ImgS3UploaderTool::widget() {
return new ImgS3Uploader(capture, m_s3CredsUrl, m_s3XApiKey);
return new ImgS3Uploader(capture);
}
void ImgS3UploaderTool::setCapture(const QPixmap &pixmap) {

View File

@@ -41,6 +41,4 @@ public slots:
private:
QPixmap capture;
QString m_s3CredsUrl;
QString m_s3XApiKey;
};