Skip to content

Commit

Permalink
initial code impl
Browse files Browse the repository at this point in the history
  • Loading branch information
moodyhunter committed Jun 2, 2021
1 parent b9ca55d commit 138b11e
Show file tree
Hide file tree
Showing 52 changed files with 4,668 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.user
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "plugin-interface"]
path = plugin-interface
url = ../QvPlugin-interface
[submodule "3rdparty/http-parser"]
path = 3rdparty/http-parser
url = https://github.com/nodejs/http-parser.git
1 change: 1 addition & 0 deletions 3rdparty/http-parser
Submodule http-parser added at ec8b5e
62 changes: 62 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
project(RemoteManagement)
cmake_minimum_required(VERSION 3.20)

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)

find_package(Qt6 COMPONENTS Core Network WebSockets RemoteObjects REQUIRED)


set(HTTP_SERVER_SOURCES
3rdparty/http-parser/http_parser.c
3rdparty/http-parser/http_parser.h
src/qthttpserver/qabstracthttpserver.cpp
src/qthttpserver/qabstracthttpserver.h
src/qthttpserver/qabstracthttpserver_p.h
src/qthttpserver/qhttpserver.cpp
src/qthttpserver/qhttpserver.h
src/qthttpserver/qhttpserver_p.h
src/qthttpserver/qhttpserverliterals.cpp
src/qthttpserver/qhttpserverliterals_p.h
src/qthttpserver/qhttpserverrequest.cpp
src/qthttpserver/qhttpserverrequest.h
src/qthttpserver/qhttpserverrequest_p.h
src/qthttpserver/qhttpserverresponder.cpp
src/qthttpserver/qhttpserverresponder.h
src/qthttpserver/qhttpserverresponder_p.h
src/qthttpserver/qhttpserverresponse.cpp
src/qthttpserver/qhttpserverresponse.h
src/qthttpserver/qhttpserverresponse_p.h
src/qthttpserver/qhttpserverrouter.cpp
src/qthttpserver/qhttpserverrouter.h
src/qthttpserver/qhttpserverrouter_p.h
src/qthttpserver/qhttpserverrouterrule.cpp
src/qthttpserver/qhttpserverrouterrule.h
src/qthttpserver/qhttpserverrouterrule_p.h
src/qthttpserver/qhttpserverrouterviewtraits.h
src/qthttpserver/qhttpserverviewtraits.h
src/qthttpserver/qhttpserverviewtraits_impl.h
src/qthttpserver/qthttpserverglobal.h
src/qthttpserver/qsslserver.cpp
src/qthttpserver/qsslserver.h
src/qthttpserver/qsslserver_p.h
src/qthttpserver/qtsslserverglobal.h)

include(plugin-interface/QvPluginInterface.cmake)

qt6_add_library(RemoteManagementPlugin
src/RemotePlugin.hpp
src/RemotePlugin.cpp
src/RESTfulProcessor.cpp
src/RESTfulProcessor.hpp
${HTTP_SERVER_SOURCES})

qv2ray_configure_plugin(RemoteManagementPlugin)

target_include_directories(RemoteManagementPlugin PRIVATE
${CMAKE_CURRENT_LIST_DIR}/3rdparty/
${CMAKE_CURRENT_LIST_DIR}/src/QHttpServer)

target_link_libraries(RemoteManagementPlugin Qt::Core Qt::CorePrivate Qt::Network Qt::NetworkPrivate Qt::WebSockets)
target_include_directories(RemoteManagementPlugin PRIVATE src/qthttpserver)
2 changes: 1 addition & 1 deletion plugin-interface
18 changes: 18 additions & 0 deletions src/RESTfulProcessor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "RESTfulProcessor.hpp"

#include "qhttpserver.h"

RESTfulProcessor::RESTfulProcessor()
{
m_manager = Qv2rayPlugin::PluginInstance->ConnectionManager();
}

RESTfulProcessor::~RESTfulProcessor()
{
}

void RESTfulProcessor::SetServer(QHttpServer *s)
{
this->m_server = s;
s->route("/", []() { return "Hello world"; });
}
17 changes: 17 additions & 0 deletions src/RESTfulProcessor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include "plugin-interface/QvPluginInterface.hpp"
#include "plugin-interface/connections/ConnectionsBase.hpp"
#include "qhttpserver.h"

class RESTfulProcessor
{
public:
RESTfulProcessor();
virtual ~RESTfulProcessor();
void SetServer(QHttpServer *router);

private:
QHttpServer *m_server;
Qv2rayPlugin::connections::IConnectionManager *m_manager;
};
11 changes: 11 additions & 0 deletions src/RemotePlugin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "RemotePlugin.hpp"

bool RemoteManagementPlugin::InitializePlugin()
{
const auto port = m_Settings["listenPort"].toInt(8089);
const auto address = m_Settings["listenAddress"].toString("127.0.0.1");
server = new QHttpServer(this);
server->listen(QHostAddress(address), port);
processor.SetServer(server);
return true;
}
45 changes: 45 additions & 0 deletions src/RemotePlugin.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#pragma once

#include "QvPluginInterface.hpp"
#include "RESTfulProcessor.hpp"
#include "qhttpserver.h"

#include <QObject>
#include <QtPlugin>

using namespace Qv2rayPlugin;

class RemoteManagementPlugin
: public QObject
, Qv2rayInterface
{
Q_INTERFACES(Qv2rayPlugin::Qv2rayInterface)
Q_PLUGIN_METADATA(IID Qv2rayInterface_IID)
Q_OBJECT

public:
const QvPluginMetadata GetMetadata() const override
{
return { "Remote Management", //
"Moody", //
"Qv2ray remote management using RESTful API and QtRemoteObjects over websocket.", //
"qv2ray-remote", //
"https://github.com/moodyhunter/Qv2ray-RemoteManagement", //
{} };
}
~RemoteManagementPlugin(){};
bool InitializePlugin() override;
void SettingsUpdated() override{};
QHttpServer *HttpServer()
{
return server;
}

private:
QHttpServer *server;
RESTfulProcessor processor;

signals:
void PluginLog(QString) const override;
void PluginErrorMessageBox(QString, QString) const override;
};
7 changes: 7 additions & 0 deletions src/qthttpserver/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#Generated from examples.pro.

qt_examples_build_begin()

add_subdirectory(httpserver)

qt_examples_build_end()
3 changes: 3 additions & 0 deletions src/qthttpserver/examples/examples.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
TEMPLATE = subdirs

SUBDIRS = httpserver
3 changes: 3 additions & 0 deletions src/qthttpserver/examples/httpserver/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#Generated from httpserver.pro.

add_subdirectory(afterrequest) add_subdirectory(simple)
16 changes: 16 additions & 0 deletions src/qthttpserver/examples/httpserver/afterrequest/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#Generated from afterrequest.pro.

cmake_minimum_required(VERSION 3.14) project(afterrequest LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) set(CMAKE_AUTOUIC ON)

set(INSTALL_EXAMPLEDIR "examples/httpserver/afterrequest")

find_package(Qt6 COMPONENTS HttpServer)

add_executable(afterrequest main.cpp) target_link_libraries(afterrequest PUBLIC Qt::HttpServer)

install(TARGETS afterrequest RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" LIBRARY DESTINATION
"${INSTALL_EXAMPLEDIR}")
14 changes: 14 additions & 0 deletions src/qthttpserver/examples/httpserver/afterrequest/afterrequest.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
requires(qtHaveModule(httpserver))

TEMPLATE = app

QT = httpserver

SOURCES
+= main.cpp

target.path = $$[QT_INSTALL_EXAMPLES] / httpserver / afterrequest INSTALLS
+= target

CONFIG
+= cmdline
77 changes: 77 additions & 0 deletions src/qthttpserver/examples/httpserver/afterrequest/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/****************************************************************************
**
** Copyright (C) 2020 Mikhail Svetkin <[email protected]>
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtHttpServer module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QtCore>
#include <QtHttpServer>

int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);

QHttpServer httpServer;
httpServer.route("/", []() { return "Hello world"; });

httpServer.afterRequest([](QHttpServerResponse &&resp) {
resp.setHeader("Server", "Super server!");
return std::move(resp);
});

const auto port = httpServer.listen(QHostAddress::Any);
if (!port)
{
qDebug() << QCoreApplication::translate("QHttpServerExample", "Server failed to listen on a port.");
return 0;
}

qDebug() << QCoreApplication::translate("QHttpServerExample", "Running on http://127.0.0.1:%1/ (Press CTRL+C to quit)").arg(port);

return app.exec();
}
3 changes: 3 additions & 0 deletions src/qthttpserver/examples/httpserver/httpserver.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
TEMPLATE = subdirs

SUBDIRS = afterrequest simple
21 changes: 21 additions & 0 deletions src/qthttpserver/examples/httpserver/simple/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#Generated from simple.pro.

cmake_minimum_required(VERSION 3.14) project(simple LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) set(CMAKE_AUTOUIC ON)

set(INSTALL_EXAMPLEDIR "examples/httpserver/simple")

find_package(Qt6 COMPONENTS HttpServer)

add_executable(simple main.cpp) target_link_libraries(simple PUBLIC Qt::HttpServer)

#Resources:
set(assets_resource_files "assets/qt-logo.png")

qt6_add_resources(simple "assets" PREFIX "/" FILES ${ assets_resource_files })

install(TARGETS simple RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" LIBRARY DESTINATION
"${INSTALL_EXAMPLEDIR}")
5 changes: 5 additions & 0 deletions src/qthttpserver/examples/httpserver/simple/assets.qrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/">
<file>assets/qt-logo.png</file>
</qresource>
</RCC>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 138b11e

Please sign in to comment.