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

View File

@ -123,6 +123,16 @@ namespace Qv2ray
root["subscriptions"] = newSubscriptions;
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;

View File

@ -408,7 +408,7 @@ namespace Qv2ray
INBOUNDS inbounds = INBOUNDS(root["inbounds"].toArray());
INBOUNDSETTING fakeDocodemoDoor;
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);
root["inbounds"] = inbounds;
//

View File

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

View File

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

View File

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

View File

@ -100,7 +100,7 @@ PreferencesWindow::PreferencesWindow(QWidget *parent) : QDialog(parent),
//
vCorePathTxt->setText(CurrentConfig.v2CorePath);
vCoreAssetsPathTxt->setText(CurrentConfig.v2AssetsPath);
statsPortBox->setValue(CurrentConfig.connectionConfig.statsPort);
statsPortBox->setValue(CurrentConfig.apiConfig.statsPort);
//
//
bypassCNCb->setChecked(CurrentConfig.connectionConfig.bypassCN);
@ -203,7 +203,7 @@ void PreferencesWindow::on_buttonBox_accepted()
if (!StartupOption.noAPI) {
size ++;
ports << CurrentConfig.connectionConfig.statsPort;
ports << CurrentConfig.apiConfig.statsPort;
}
if (ports.size() != size) {
@ -466,7 +466,7 @@ void PreferencesWindow::on_bypassCNCb_stateChanged(int arg1)
void PreferencesWindow::on_statsPortBox_valueChanged(int arg1)
{
NEEDRESTART
CurrentConfig.connectionConfig.statsPort = arg1;
CurrentConfig.apiConfig.statsPort = arg1;
}
void PreferencesWindow::on_socksPortLE_valueChanged(int arg1)
@ -1021,3 +1021,10 @@ void PreferencesWindow::on_maxLogLinesSB_valueChanged(int arg1)
NEEDRESTART
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_enableAPI_stateChanged(int arg1);
private:
void SetAutoStartButtonsState(bool isAutoStart);
// Set ui parameters for a line;

View File

@ -44,7 +44,7 @@
<x>0</x>
<y>0</y>
<width>647</width>
<height>476</height>
<height>547</height>
</rect>
</property>
<layout class="QFormLayout" name="formLayout_5">
@ -248,13 +248,107 @@
</layout>
</item>
<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">
<property name="text">
<string>Log Level</string>
</property>
</widget>
</item>
<item row="2" column="1">
<item row="3" column="1">
<widget class="QComboBox" name="logLevelComboBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
@ -295,7 +389,7 @@
</item>
</widget>
</item>
<item row="3" column="0">
<item row="4" column="0">
<layout class="QVBoxLayout" name="verticalLayout_9">
<item>
<spacer name="verticalSpacer_6">
@ -332,7 +426,7 @@
</item>
</layout>
</item>
<item row="3" column="1">
<item row="4" column="1">
<layout class="QGridLayout" name="gridLayout_8" columnstretch="0,0,1">
<item row="1" column="1">
<widget class="QLabel" name="label_61">
@ -381,21 +475,21 @@
</item>
</layout>
</item>
<item row="4" column="0">
<item row="5" column="0">
<widget class="QLabel" name="label_7">
<property name="text">
<string>Transparent Proxy</string>
</property>
</widget>
</item>
<item row="4" column="1">
<item row="5" column="1">
<widget class="QCheckBox" name="tProxyCheckBox">
<property name="text">
<string>Enabled</string>
</property>
</widget>
</item>
<item row="5" column="0">
<item row="6" column="0">
<layout class="QVBoxLayout" name="verticalLayout_8">
<item>
<spacer name="verticalSpacer_12">
@ -432,7 +526,7 @@
</item>
</layout>
</item>
<item row="5" column="1">
<item row="6" column="1">
<layout class="QGridLayout" name="gridLayout_9">
<item row="0" column="0" rowspan="3">
<widget class="Line" name="line_3">
@ -494,7 +588,7 @@
</item>
</layout>
</item>
<item row="6" column="1">
<item row="7" column="1">
<spacer name="verticalSpacer_11">
<property name="orientation">
<enum>Qt::Vertical</enum>
@ -995,53 +1089,27 @@
</widget>
</item>
<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">
<property name="text">
<string>Use Local DNS</string>
</property>
</widget>
</item>
<item row="3" column="1">
<item row="2" column="1">
<widget class="QCheckBox" name="localDNSCb">
<property name="text">
<string>Enabled</string>
</property>
</widget>
</item>
<item row="4" column="0">
<item row="3" column="0">
<widget class="QLabel" name="label_21">
<property name="text">
<string>Custom DNS List</string>
</property>
</widget>
</item>
<item row="4" column="1">
<item row="3" column="1">
<widget class="QPlainTextEdit" name="DNSListTxt"/>
</item>
</layout>
@ -1879,7 +1947,6 @@ p, li { white-space: pre-wrap; }
<tabstop>pushButton</tabstop>
<tabstop>proxyDefaultCb</tabstop>
<tabstop>bypassCNCb</tabstop>
<tabstop>statsPortBox</tabstop>
<tabstop>localDNSCb</tabstop>
<tabstop>DNSListTxt</tabstop>
<tabstop>fpGroupBox</tabstop>

View File

@ -1,21 +1,21 @@
@echo off
cd tools
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
del %~dp0gRPC-win32.tar
del "%~dp0gRPC-win32.tar"
mkdir %~dp0..\libs\gen
mkdir "%~dp0..\libs\gen"
echo.
echo Generate grpc.pb.h using gRPC and protocol buffer
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
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
echo DONE