Skip to content

Commit

Permalink
parse hysteria share link
Browse files Browse the repository at this point in the history
  • Loading branch information
arm64v8a committed Aug 13, 2022
1 parent 6931b4c commit 38e51be
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 20 deletions.
17 changes: 17 additions & 0 deletions fmt/Preset.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

namespace Preset {
namespace Hysteria {
inline const char *command = "--no-check -c %config%";
inline const char *config = "{\n"
" \"server\": \"127.0.0.1:%mapping_port%\",\n"
" \"obfs\": \"fuck me till the daylight\",\n"
" \"up_mbps\": 10,\n"
" \"down_mbps\": 50,\n"
" \"server_name\": \"real.name.com\",\n"
" \"socks5\": {\n"
" \"listen\": \"127.0.0.1:%socks_port%\"\n"
" }\n"
"}";
}
}
1 change: 0 additions & 1 deletion main/Const.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,5 @@ namespace NekoRay {
VPN,
};
}

}

27 changes: 27 additions & 0 deletions sub/GroupUpdater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
#include "db/Database.hpp"
#include "db/ProfileFilter.hpp"
#include "fmt/includes.h"
#include "fmt/Preset.hpp"

#include "GroupUpdater.hpp"

#include <QInputDialog>
#include <QUrlQuery>

#ifndef NKR_NO_EXTERNAL

Expand Down Expand Up @@ -105,6 +107,31 @@ namespace NekoRay::sub {
if (!ok) return;
}

// Hysteria
if (str.startsWith("hysteria://")) {
// https://github.com/HyNetwork/hysteria/wiki/URI-Scheme
ent = ProfileManager::NewProxyEntity("custom");
auto bean = ent->CustomBean();
auto url = QUrl(str);
auto query = QUrlQuery(url.query());
if (url.host().isEmpty() || url.port() == -1 || !query.hasQueryItem("upmbps")) return;
bean->name = url.fragment();
bean->serverAddress = url.host();
bean->serverPort = url.port();
bean->core = "hysteria";
bean->command = QString(Preset::Hysteria::command).split(" ");
auto result = QString2QJsonObject(Preset::Hysteria::config);
result["obfs"] = query.queryItemValue("obfsParam");
result["insecure"] = query.queryItemValue("insecure") == "1";
result["up_mbps"] = query.queryItemValue("upmbps").toInt();
result["down_mbps"] = query.queryItemValue("downmbps").toInt();
result["protocol"] = query.hasQueryItem("protocol") ? query.queryItemValue("protocol") : "udp";
if (query.hasQueryItem("auth")) result["auth_str"] = query.queryItemValue("auth");
if (query.hasQueryItem("alpn")) result["alpn"] = query.queryItemValue("alpn");
if (query.hasQueryItem("peer")) result["server_name"] = query.queryItemValue("peer");
bean->config_simple = QJsonObject2QString(result, false);
}

// End
if (ent == nullptr) return;
profileManager->AddProfile(ent, gid_add_to);
Expand Down
14 changes: 3 additions & 11 deletions ui/edit/dialog_edit_profile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "ui/edit/edit_custom.h"

#include "fmt/includes.h"
#include "fmt/Preset.hpp"

#include "qv2ray/v2/ui/widgets/editors/w_JsonEditor.hpp"
#include "main/GuiUtils.hpp"
Expand Down Expand Up @@ -129,17 +130,8 @@ void DialogEditProfile::typeSelected(const QString &newType) {
innerEditor = _innerWidget;
if (type == "hysteria" || ent->CustomBean()->core == "hysteria") {
_innerWidget->preset_core = type;
_innerWidget->preset_command = "-c %config%";
_innerWidget->preset_config = "{\n"
" \"server\": \"127.0.0.1:%mapping_port%\",\n"
" \"obfs\": \"fuck me till the daylight\",\n"
" \"up_mbps\": 10,\n"
" \"down_mbps\": 50,\n"
" \"server_name\": \"real.name.com\",\n"
" \"socks5\": {\n"
" \"listen\": \"127.0.0.1:%socks_port%\"\n"
" }\n"
"}";
_innerWidget->preset_command = Preset::Hysteria::command;
_innerWidget->preset_config = Preset::Hysteria::config;
}
type = "custom";
} else {
Expand Down
2 changes: 1 addition & 1 deletion ui/edit/edit_custom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void EditCustom::onStart(QSharedPointer<NekoRay::ProxyEntity> _ent) {
// Generators
if (bean->core == "hysteria") {
ui->generator->setVisible(true);
auto genHy = new GenHysteria(ent, preset_config);
auto genHy = new GenHysteria(ent);
ui->generator->layout()->addWidget(genHy);
connect(genHy, &GenHysteria::config_generated, this, [=](const QString &result) {
ui->config_simple->setText(result);
Expand Down
7 changes: 3 additions & 4 deletions ui/edit/gen_hysteria.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@
#include "ui_gen_hysteria.h"

#include "fmt/CustomBean.hpp"
#include "fmt/Preset.hpp"

GenHysteria::GenHysteria(const QSharedPointer<NekoRay::ProxyEntity> &ent, const QString &preset_config,
QWidget *parent) :
GenHysteria::GenHysteria(const QSharedPointer<NekoRay::ProxyEntity> &ent, QWidget *parent) :
QWidget(parent), ui(new Ui::GenHysteria) {
ui->setupUi(this);
this->ent = ent;
this->preset_config = preset_config;
}

GenHysteria::~GenHysteria() {
delete ui;
}

void GenHysteria::on_gen_clicked() {
auto result = QString2QJsonObject(preset_config);
auto result = QString2QJsonObject(Preset::Hysteria::config);
result["obfs"] = ui->obfs_password->text();
result["insecure"] = ui->allow_insecure->isChecked();
result["protocol"] = ui->protocol->currentText();
Expand Down
4 changes: 1 addition & 3 deletions ui/edit/gen_hysteria.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ class GenHysteria : public QWidget {
Q_OBJECT

public:
explicit GenHysteria(const QSharedPointer<NekoRay::ProxyEntity> &ent, const QString &preset_config,
QWidget *parent = nullptr);
explicit GenHysteria(const QSharedPointer<NekoRay::ProxyEntity> &ent, QWidget *parent = nullptr);

~GenHysteria() override;

private:
Ui::GenHysteria *ui;
QSharedPointer<NekoRay::ProxyEntity> ent;
QString preset_config;

signals:

Expand Down

0 comments on commit 38e51be

Please sign in to comment.