add: add global option to disable API system

This commit is contained in:
Qv2ray Bot 2020-01-21 14:09:41 +08:00
parent c6075b1dc6
commit 90fd2d4f32
11 changed files with 160 additions and 68 deletions

View File

@ -1 +1 @@
3005 3011

View File

@ -9,7 +9,7 @@
#include "QvTinyLog.hpp" #include "QvTinyLog.hpp"
#include "QvCoreConfigObjects.hpp" #include "QvCoreConfigObjects.hpp"
const int QV2RAY_CONFIG_VERSION = 6; const int QV2RAY_CONFIG_VERSION = 7;
// Linux users and DEs should handle the darkMode UI themselves. // Linux users and DEs should handle the darkMode UI themselves.
#ifndef QV2RAY_USE_BUILTIN_DARKTHEME #ifndef QV2RAY_USE_BUILTIN_DARKTHEME
@ -189,11 +189,17 @@ namespace Qv2ray
bool enableProxy; bool enableProxy;
bool withLocalDNS; bool withLocalDNS;
QList<QString> dnsList; QList<QString> dnsList;
int statsPort;
Qv2rayForwardProxyConfig forwardProxyConfig; Qv2rayForwardProxyConfig forwardProxyConfig;
Qv2rayConnectionConfig() : bypassCN(true), enableProxy(true), withLocalDNS(false), dnsList(QStringList() << "8.8.4.4" << "1.1.1.1"), statsPort(15490) { } Qv2rayConnectionConfig() : bypassCN(true), enableProxy(true), withLocalDNS(false), dnsList(QStringList() << "8.8.4.4" << "1.1.1.1") { }
XTOSTRUCT(O(bypassCN, enableProxy, withLocalDNS, dnsList, statsPort, forwardProxyConfig)) XTOSTRUCT(O(bypassCN, enableProxy, withLocalDNS, dnsList, forwardProxyConfig))
};
struct Qv2rayAPIConfig {
bool enableAPI;
int statsPort;
Qv2rayAPIConfig(): enableAPI(true), statsPort(15490) { }
XTOSTRUCT(O(enableAPI, statsPort))
}; };
struct Qv2rayConfig { struct Qv2rayConfig {
@ -210,6 +216,7 @@ namespace Qv2ray
QMap<QString, Qv2raySubscriptionConfig> subscriptions; QMap<QString, Qv2raySubscriptionConfig> subscriptions;
// //
Qv2rayUIConfig uiConfig; Qv2rayUIConfig uiConfig;
Qv2rayAPIConfig apiConfig;
Qv2rayInboundsConfig inboundConfig; Qv2rayInboundsConfig inboundConfig;
Qv2rayConnectionConfig connectionConfig; Qv2rayConnectionConfig connectionConfig;
Qv2rayToolBarConfig toolBarConfig; Qv2rayToolBarConfig toolBarConfig;

View File

@ -123,6 +123,16 @@ namespace Qv2ray
root["subscriptions"] = newSubscriptions; root["subscriptions"] = newSubscriptions;
break; break;
} }
// Qv2ray version 2, RC 4
case 6: {
// Moved API Stats port from connectionConfig to apiConfig
QJsonObject apiConfig;
apiConfig["enableAPI"] = true;
apiConfig["statsPort"] = root["connectionConfig"].toObject()["statsPort"].toInt();
root["apiConfig"] = apiConfig;
break;
}
} }
root["config_version"] = root["config_version"].toInt() + 1; root["config_version"] = root["config_version"].toInt() + 1;

View File

@ -408,7 +408,7 @@ namespace Qv2ray
INBOUNDS inbounds = INBOUNDS(root["inbounds"].toArray()); INBOUNDS inbounds = INBOUNDS(root["inbounds"].toArray());
INBOUNDSETTING fakeDocodemoDoor; INBOUNDSETTING fakeDocodemoDoor;
fakeDocodemoDoor["address"] = "127.0.0.1"; fakeDocodemoDoor["address"] = "127.0.0.1";
QJsonObject apiInboundsRoot = GenerateInboundEntry("127.0.0.1", gConf.connectionConfig.statsPort, "dokodemo-door", fakeDocodemoDoor, API_TAG_INBOUND); QJsonObject apiInboundsRoot = GenerateInboundEntry("127.0.0.1", gConf.apiConfig.statsPort, "dokodemo-door", fakeDocodemoDoor, API_TAG_INBOUND);
inbounds.push_front(apiInboundsRoot); inbounds.push_front(apiInboundsRoot);
root["inbounds"] = inbounds; root["inbounds"] = inbounds;
// //

View File

@ -63,22 +63,18 @@ namespace Qv2ray
// Check if V2ray core returns a version number correctly. // Check if V2ray core returns a version number correctly.
QProcess proc; QProcess proc;
proc.start(vCorePath + " -version"); proc.start(vCorePath + " -version");
proc.waitForFinished();
auto exitCode = proc.exitCode();
if (!proc.waitForFinished(1000) || proc.exitCode() != 0) { if (exitCode != 0) {
DEBUG(MODULE_VCORE, "V2ray core not exited within 1 sec, or it failed with an exit code: " + QSTRN(proc.exitCode())) DEBUG(MODULE_VCORE, "VCore failed with an exit code: " + QSTRN(exitCode))
*message = tr("V2ray core failed with an exit code: ") + QSTRN(exitCode);
if (proc.exitCode() != 0) {
*message = tr("V2ray core failed with an exit code: ") + QSTRN(proc.exitCode());
} else {
*message = tr("V2ray core not responsed within 1 secs.");
}
return false; return false;
} }
QString output = QString(proc.readAllStandardOutput()); QString output = QString(proc.readAllStandardOutput());
LOG(MODULE_VCORE, "V2ray output: " + SplitLines(output).join(";")) LOG(MODULE_VCORE, "V2ray output: " + SplitLines(output).join(";"))
*message = SplitLines(output).first(); *message = SplitLines(output).first();
return true; return true;
} }
@ -134,7 +130,7 @@ namespace Qv2ray
KernelStarted = false; KernelStarted = false;
} }
bool V2rayKernelInstance::StartConnection(CONFIGROOT root, int apiPort) bool V2rayKernelInstance::StartConnection(CONFIGROOT root)
{ {
if (KernelStarted) { if (KernelStarted) {
LOG(MODULE_VCORE, "Status is invalid, expect STOPPED when calling StartConnection") LOG(MODULE_VCORE, "Status is invalid, expect STOPPED when calling StartConnection")
@ -169,15 +165,18 @@ namespace Qv2ray
vProcess->waitForStarted(); vProcess->waitForStarted();
DEBUG(MODULE_VCORE, "V2ray core started.") DEBUG(MODULE_VCORE, "V2ray core started.")
KernelStarted = true; KernelStarted = true;
auto conf = GetGlobalConfig();
if (StartupOption.noAPI) { if (StartupOption.noAPI) {
LOG(MODULE_VCORE, "API has been disabled by the command line argument \"-noAPI\"") LOG(MODULE_VCORE, "API has been disabled by the command line argument \"-noAPI\"")
} else if (!conf.apiConfig.enableAPI) {
LOG(MODULE_VCORE, "API has been disabled by the global config option")
} else if (inboundTags.isEmpty()) { } else if (inboundTags.isEmpty()) {
LOG(MODULE_VCORE, "API is disabled since no inbound tags configured. This is probably caused by a bad complex config.") LOG(MODULE_VCORE, "API is disabled since no inbound tags configured. This is probably caused by a bad complex config.")
} else { } else {
// Config API // Config API
apiFailedCounter = 0; apiFailedCounter = 0;
this->apiPort = apiPort; this->apiPort = conf.apiConfig.statsPort;
Channel = grpc::CreateChannel("127.0.0.1:" + to_string(apiPort), grpc::InsecureChannelCredentials()); Channel = grpc::CreateChannel("127.0.0.1:" + to_string(apiPort), grpc::InsecureChannelCredentials());
StatsService service; StatsService service;
Stub = service.NewStub(Channel); Stub = service.NewStub(Channel);

View File

@ -27,7 +27,7 @@ namespace Qv2ray
long getAllSpeedUp(); long getAllSpeedUp();
long getAllSpeedDown(); long getAllSpeedDown();
// //
bool StartConnection(CONFIGROOT root, int apiPort); bool StartConnection(CONFIGROOT root);
void StopConnection(); void StopConnection();
bool KernelStarted = false; bool KernelStarted = false;
// //

View File

@ -130,7 +130,7 @@ bool MainWindow::MWtryStartConnection()
{ {
auto connectionRoot = connections[CurrentConnectionIdentifier].config; auto connectionRoot = connections[CurrentConnectionIdentifier].config;
currentFullConfig = GenerateRuntimeConfig(connectionRoot); currentFullConfig = GenerateRuntimeConfig(connectionRoot);
bool startFlag = this->vinstance->StartConnection(currentFullConfig, currentConfig.connectionConfig.statsPort); bool startFlag = this->vinstance->StartConnection(currentFullConfig);
if (startFlag) { if (startFlag) {
bool usePAC = currentConfig.inboundConfig.pacConfig.enablePAC; bool usePAC = currentConfig.inboundConfig.pacConfig.enablePAC;

View File

@ -100,7 +100,7 @@ PreferencesWindow::PreferencesWindow(QWidget *parent) : QDialog(parent),
// //
vCorePathTxt->setText(CurrentConfig.v2CorePath); vCorePathTxt->setText(CurrentConfig.v2CorePath);
vCoreAssetsPathTxt->setText(CurrentConfig.v2AssetsPath); vCoreAssetsPathTxt->setText(CurrentConfig.v2AssetsPath);
statsPortBox->setValue(CurrentConfig.connectionConfig.statsPort); statsPortBox->setValue(CurrentConfig.apiConfig.statsPort);
// //
// //
bypassCNCb->setChecked(CurrentConfig.connectionConfig.bypassCN); bypassCNCb->setChecked(CurrentConfig.connectionConfig.bypassCN);
@ -203,7 +203,7 @@ void PreferencesWindow::on_buttonBox_accepted()
if (!StartupOption.noAPI) { if (!StartupOption.noAPI) {
size ++; size ++;
ports << CurrentConfig.connectionConfig.statsPort; ports << CurrentConfig.apiConfig.statsPort;
} }
if (ports.size() != size) { if (ports.size() != size) {
@ -466,7 +466,7 @@ void PreferencesWindow::on_bypassCNCb_stateChanged(int arg1)
void PreferencesWindow::on_statsPortBox_valueChanged(int arg1) void PreferencesWindow::on_statsPortBox_valueChanged(int arg1)
{ {
NEEDRESTART NEEDRESTART
CurrentConfig.connectionConfig.statsPort = arg1; CurrentConfig.apiConfig.statsPort = arg1;
} }
void PreferencesWindow::on_socksPortLE_valueChanged(int arg1) void PreferencesWindow::on_socksPortLE_valueChanged(int arg1)
@ -1021,3 +1021,10 @@ void PreferencesWindow::on_maxLogLinesSB_valueChanged(int arg1)
NEEDRESTART NEEDRESTART
CurrentConfig.uiConfig.maximumLogLines = arg1; CurrentConfig.uiConfig.maximumLogLines = arg1;
} }
void PreferencesWindow::on_enableAPI_stateChanged(int arg1)
{
LOADINGCHECK
NEEDRESTART
CurrentConfig.apiConfig.enableAPI = arg1 == Qt::Checked;
}

View File

@ -160,6 +160,8 @@ class PreferencesWindow : public QDialog, private Ui::PreferencesWindow
void on_maxLogLinesSB_valueChanged(int arg1); void on_maxLogLinesSB_valueChanged(int arg1);
void on_enableAPI_stateChanged(int arg1);
private: private:
void SetAutoStartButtonsState(bool isAutoStart); void SetAutoStartButtonsState(bool isAutoStart);
// Set ui parameters for a line; // Set ui parameters for a line;

View File

@ -44,7 +44,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>647</width> <width>647</width>
<height>476</height> <height>547</height>
</rect> </rect>
</property> </property>
<layout class="QFormLayout" name="formLayout_5"> <layout class="QFormLayout" name="formLayout_5">
@ -248,13 +248,107 @@
</layout> </layout>
</item> </item>
<item row="2" column="0"> <item row="2" column="0">
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<spacer name="verticalSpacer_16">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLabel" name="label_64">
<property name="text">
<string>API Subsystem</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer_15">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item row="2" column="1">
<layout class="QGridLayout" name="gridLayout_4" columnstretch="0,0,1">
<item row="1" column="1">
<widget class="QLabel" name="label_8">
<property name="text">
<string>API Port</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLabel" name="label_68">
<property name="text">
<string>Enabled</string>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QSpinBox" name="statsPortBox">
<property name="minimumSize">
<size>
<width>100</width>
<height>0</height>
</size>
</property>
<property name="minimum">
<number>1024</number>
</property>
<property name="maximum">
<number>65535</number>
</property>
<property name="value">
<number>15934</number>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QCheckBox" name="enableAPI">
<property name="text">
<string>Enabled</string>
</property>
</widget>
</item>
<item row="0" column="0" rowspan="2">
<widget class="Line" name="line_4">
<property name="frameShadow">
<enum>QFrame::Plain</enum>
</property>
<property name="lineWidth">
<number>2</number>
</property>
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
</item>
</layout>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label"> <widget class="QLabel" name="label">
<property name="text"> <property name="text">
<string>Log Level</string> <string>Log Level</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="2" column="1"> <item row="3" column="1">
<widget class="QComboBox" name="logLevelComboBox"> <widget class="QComboBox" name="logLevelComboBox">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed"> <sizepolicy hsizetype="Minimum" vsizetype="Fixed">
@ -295,7 +389,7 @@
</item> </item>
</widget> </widget>
</item> </item>
<item row="3" column="0"> <item row="4" column="0">
<layout class="QVBoxLayout" name="verticalLayout_9"> <layout class="QVBoxLayout" name="verticalLayout_9">
<item> <item>
<spacer name="verticalSpacer_6"> <spacer name="verticalSpacer_6">
@ -332,7 +426,7 @@
</item> </item>
</layout> </layout>
</item> </item>
<item row="3" column="1"> <item row="4" column="1">
<layout class="QGridLayout" name="gridLayout_8" columnstretch="0,0,1"> <layout class="QGridLayout" name="gridLayout_8" columnstretch="0,0,1">
<item row="1" column="1"> <item row="1" column="1">
<widget class="QLabel" name="label_61"> <widget class="QLabel" name="label_61">
@ -381,21 +475,21 @@
</item> </item>
</layout> </layout>
</item> </item>
<item row="4" column="0"> <item row="5" column="0">
<widget class="QLabel" name="label_7"> <widget class="QLabel" name="label_7">
<property name="text"> <property name="text">
<string>Transparent Proxy</string> <string>Transparent Proxy</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="4" column="1"> <item row="5" column="1">
<widget class="QCheckBox" name="tProxyCheckBox"> <widget class="QCheckBox" name="tProxyCheckBox">
<property name="text"> <property name="text">
<string>Enabled</string> <string>Enabled</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="5" column="0"> <item row="6" column="0">
<layout class="QVBoxLayout" name="verticalLayout_8"> <layout class="QVBoxLayout" name="verticalLayout_8">
<item> <item>
<spacer name="verticalSpacer_12"> <spacer name="verticalSpacer_12">
@ -432,7 +526,7 @@
</item> </item>
</layout> </layout>
</item> </item>
<item row="5" column="1"> <item row="6" column="1">
<layout class="QGridLayout" name="gridLayout_9"> <layout class="QGridLayout" name="gridLayout_9">
<item row="0" column="0" rowspan="3"> <item row="0" column="0" rowspan="3">
<widget class="Line" name="line_3"> <widget class="Line" name="line_3">
@ -494,7 +588,7 @@
</item> </item>
</layout> </layout>
</item> </item>
<item row="6" column="1"> <item row="7" column="1">
<spacer name="verticalSpacer_11"> <spacer name="verticalSpacer_11">
<property name="orientation"> <property name="orientation">
<enum>Qt::Vertical</enum> <enum>Qt::Vertical</enum>
@ -995,53 +1089,27 @@
</widget> </widget>
</item> </item>
<item row="2" column="0"> <item row="2" column="0">
<widget class="QLabel" name="label_8">
<property name="text">
<string>API Port</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QSpinBox" name="statsPortBox">
<property name="minimumSize">
<size>
<width>100</width>
<height>0</height>
</size>
</property>
<property name="minimum">
<number>1024</number>
</property>
<property name="maximum">
<number>65535</number>
</property>
<property name="value">
<number>15934</number>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_18"> <widget class="QLabel" name="label_18">
<property name="text"> <property name="text">
<string>Use Local DNS</string> <string>Use Local DNS</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="3" column="1"> <item row="2" column="1">
<widget class="QCheckBox" name="localDNSCb"> <widget class="QCheckBox" name="localDNSCb">
<property name="text"> <property name="text">
<string>Enabled</string> <string>Enabled</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="4" column="0"> <item row="3" column="0">
<widget class="QLabel" name="label_21"> <widget class="QLabel" name="label_21">
<property name="text"> <property name="text">
<string>Custom DNS List</string> <string>Custom DNS List</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="4" column="1"> <item row="3" column="1">
<widget class="QPlainTextEdit" name="DNSListTxt"/> <widget class="QPlainTextEdit" name="DNSListTxt"/>
</item> </item>
</layout> </layout>
@ -1879,7 +1947,6 @@ p, li { white-space: pre-wrap; }
<tabstop>pushButton</tabstop> <tabstop>pushButton</tabstop>
<tabstop>proxyDefaultCb</tabstop> <tabstop>proxyDefaultCb</tabstop>
<tabstop>bypassCNCb</tabstop> <tabstop>bypassCNCb</tabstop>
<tabstop>statsPortBox</tabstop>
<tabstop>localDNSCb</tabstop> <tabstop>localDNSCb</tabstop>
<tabstop>DNSListTxt</tabstop> <tabstop>DNSListTxt</tabstop>
<tabstop>fpGroupBox</tabstop> <tabstop>fpGroupBox</tabstop>

View File

@ -1,21 +1,21 @@
@echo off @echo off
cd tools cd tools
echo Extracting files. echo Extracting files.
%~dp0\7z.exe -y e %~dp0gRPC-win32.tar.gz -o%~dp0 && %~dp0\7z.exe -y x %~dp0gRPC-win32.tar -o%~dp0\..\libs\gRPC-win32 "%~dp0\7z.exe" -y e "%~dp0gRPC-win32.tar.gz" -o"%~dp0 && %~dp0\7z.exe" -y x "%~dp0gRPC-win32.tar" -o"%~dp0\..\libs\gRPC-win32"
if errorlevel 1 goto errored if errorlevel 1 goto errored
del %~dp0gRPC-win32.tar del "%~dp0gRPC-win32.tar"
mkdir %~dp0..\libs\gen mkdir "%~dp0..\libs\gen"
echo. echo.
echo Generate grpc.pb.h using gRPC and protocol buffer echo Generate grpc.pb.h using gRPC and protocol buffer
echo. ---^> Generating gRPC file. echo. ---^> Generating gRPC file.
%~dp0\..\libs\gRPC-win32\bin\protoc.exe v2ray_api_commands.proto --grpc_out=%~dp0..\libs\gen --plugin=protoc-gen-grpc="%~dp0..\libs\gRPC-win32\bin\grpc_cpp_plugin.exe" "%~dp0\..\libs\gRPC-win32\bin\protoc.exe" v2ray_api_commands.proto --grpc_out="%~dp0..\libs\gen" --plugin=protoc-gen-grpc="%~dp0..\libs\gRPC-win32\bin\grpc_cpp_plugin.exe"
if errorlevel 1 goto errored if errorlevel 1 goto errored
echo. ---^> Generating proto file. echo. ---^> Generating proto file.
%~dp0\..\libs\gRPC-win32\bin\protoc.exe v2ray_api_commands.proto --cpp_out=%~dp0..\libs\gen "%~dp0\..\libs\gRPC-win32\bin\protoc.exe" v2ray_api_commands.proto --cpp_out="%~dp0..\libs\gen"
if errorlevel 1 goto errored if errorlevel 1 goto errored
echo DONE echo DONE