mirror of
https://github.com/amnezia-vpn/amnezia-client.git
synced 2026-06-23 02:00:20 +07:00
fixed secret & enum
This commit is contained in:
@@ -18,7 +18,7 @@ using namespace amnezia;
|
|||||||
namespace {
|
namespace {
|
||||||
constexpr QLatin1String kTelemtClientJsonPath("/data/amnezia-telemt-client.json");
|
constexpr QLatin1String kTelemtClientJsonPath("/data/amnezia-telemt-client.json");
|
||||||
constexpr QLatin1String kTelemtClientJsonUploadPath("data/amnezia-telemt-client.json");
|
constexpr QLatin1String kTelemtClientJsonUploadPath("data/amnezia-telemt-client.json");
|
||||||
constexpr QLatin1String kTelemtSecretPath("/data/.amnezia-secret");
|
constexpr QLatin1String kTelemtSecretPath("/data/secret");
|
||||||
}
|
}
|
||||||
|
|
||||||
TelemtInstaller::TelemtInstaller(QObject *parent) : InstallerBase(parent) {}
|
TelemtInstaller::TelemtInstaller(QObject *parent) : InstallerBase(parent) {}
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ mkdir -p /data/tlsfront
|
|||||||
# Secret: substituted $TELEMT_SECRET -> saved file -> openssl (same rules as MTProxy configure)
|
# Secret: substituted $TELEMT_SECRET -> saved file -> openssl (same rules as MTProxy configure)
|
||||||
if [ -n "$TELEMT_SECRET" ]; then
|
if [ -n "$TELEMT_SECRET" ]; then
|
||||||
SECRET="$TELEMT_SECRET"
|
SECRET="$TELEMT_SECRET"
|
||||||
|
elif [ -f /data/secret ]; then
|
||||||
|
SECRET=$(cat /data/secret)
|
||||||
elif [ -f /data/.amnezia-secret ]; then
|
elif [ -f /data/.amnezia-secret ]; then
|
||||||
SECRET=$(cat /data/.amnezia-secret)
|
SECRET=$(cat /data/.amnezia-secret)
|
||||||
else
|
else
|
||||||
@@ -61,8 +63,8 @@ rm -f /data/config.toml
|
|||||||
echo "$TELEMT_USER_NAME = \"$SECRET\""
|
echo "$TELEMT_USER_NAME = \"$SECRET\""
|
||||||
} > /data/config.toml
|
} > /data/config.toml
|
||||||
|
|
||||||
echo "$SECRET" > /data/.amnezia-secret
|
echo "$SECRET" > /data/secret
|
||||||
chmod 600 /data/.amnezia-secret 2>/dev/null || true
|
chmod 600 /data/secret 2>/dev/null || true
|
||||||
|
|
||||||
# Do not start telemt here: a long-lived process + curl loop inside `docker exec` can confuse SSH/Docker
|
# Do not start telemt here: a long-lived process + curl loop inside `docker exec` can confuse SSH/Docker
|
||||||
# timing and is unnecessary — start.sh runs telemt after configure. Links can be empty until the service
|
# timing and is unnecessary — start.sh runs telemt after configure. Links can be empty until the service
|
||||||
|
|||||||
@@ -330,30 +330,32 @@ void InstallUiController::setContainerEnabled(int serverIndex, int containerInde
|
|||||||
const ServerCredentials credentials = m_serversController->getServerCredentials(serverIndex);
|
const ServerCredentials credentials = m_serversController->getServerCredentials(serverIndex);
|
||||||
const QString containerName = ContainerUtils::containerToString(container);
|
const QString containerName = ContainerUtils::containerToString(container);
|
||||||
|
|
||||||
emit serverIsBusy(true);
|
if (container == amnezia::ContainerEnumNS::MtProxy || container == amnezia::ContainerEnumNS::Telemt) {
|
||||||
SshSession sshSession(this);
|
emit serverIsBusy(true);
|
||||||
const QString script = enabled
|
SshSession sshSession(this);
|
||||||
? QString("sudo docker start %1").arg(containerName)
|
const QString script = enabled
|
||||||
: QString("sudo docker stop %1").arg(containerName);
|
? QString("sudo docker start %1").arg(containerName)
|
||||||
const ErrorCode errorCode = sshSession.runScript(credentials, script);
|
: QString("sudo docker stop %1").arg(containerName);
|
||||||
emit serverIsBusy(false);
|
const ErrorCode errorCode = sshSession.runScript(credentials, script);
|
||||||
|
emit serverIsBusy(false);
|
||||||
|
|
||||||
if (errorCode == ErrorCode::NoError) {
|
if (errorCode == ErrorCode::NoError) {
|
||||||
ContainerConfig currentConfig = m_serversController->getContainerConfig(serverIndex, container);
|
ContainerConfig currentConfig = m_serversController->getContainerConfig(serverIndex, container);
|
||||||
if (auto *mtConfig = currentConfig.getMtProxyProtocolConfig()) {
|
if (auto *mtConfig = currentConfig.getMtProxyProtocolConfig()) {
|
||||||
mtConfig->isEnabled = enabled;
|
mtConfig->isEnabled = enabled;
|
||||||
m_serversController->updateContainerConfig(serverIndex, container, currentConfig);
|
m_serversController->updateContainerConfig(serverIndex, container, currentConfig);
|
||||||
m_protocolModel->updateModel(currentConfig);
|
m_protocolModel->updateModel(currentConfig);
|
||||||
} else if (auto *telemtConfig = currentConfig.getTelemtProtocolConfig()) {
|
} else if (auto *telemtConfig = currentConfig.getTelemtProtocolConfig()) {
|
||||||
telemtConfig->isEnabled = enabled;
|
telemtConfig->isEnabled = enabled;
|
||||||
m_serversController->updateContainerConfig(serverIndex, container, currentConfig);
|
m_serversController->updateContainerConfig(serverIndex, container, currentConfig);
|
||||||
m_protocolModel->updateModel(currentConfig);
|
m_protocolModel->updateModel(currentConfig);
|
||||||
|
}
|
||||||
|
emit setContainerEnabledFinished(enabled);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
emit setContainerEnabledFinished(enabled);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
emit installationErrorOccurred(errorCode);
|
emit installationErrorOccurred(errorCode);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void InstallUiController::refreshContainerStatus(int serverIndex, int containerIndex) {
|
void InstallUiController::refreshContainerStatus(int serverIndex, int containerIndex) {
|
||||||
@@ -361,31 +363,33 @@ void InstallUiController::refreshContainerStatus(int serverIndex, int containerI
|
|||||||
const ServerCredentials credentials = m_serversController->getServerCredentials(serverIndex);
|
const ServerCredentials credentials = m_serversController->getServerCredentials(serverIndex);
|
||||||
const QString containerName = ContainerUtils::containerToString(container);
|
const QString containerName = ContainerUtils::containerToString(container);
|
||||||
|
|
||||||
QString stdOut;
|
if (container == amnezia::ContainerEnumNS::MtProxy || container == amnezia::ContainerEnumNS::Telemt) {
|
||||||
auto cbReadStdOut = [&](const QString &data, libssh::Client &) {
|
QString stdOut;
|
||||||
stdOut += data;
|
auto cbReadStdOut = [&](const QString &data, libssh::Client &) {
|
||||||
return ErrorCode::NoError;
|
stdOut += data;
|
||||||
};
|
return ErrorCode::NoError;
|
||||||
|
};
|
||||||
|
|
||||||
SshSession sshSession(this);
|
SshSession sshSession(this);
|
||||||
const QString script = QString(
|
const QString script = QString(
|
||||||
"sudo docker inspect --format '{{.State.Status}}' %1 2>/dev/null || echo 'not_found'")
|
"sudo docker inspect --format '{{.State.Status}}' %1 2>/dev/null || echo 'not_found'")
|
||||||
.arg(containerName);
|
.arg(containerName);
|
||||||
const ErrorCode errorCode = sshSession.runScript(credentials, script, cbReadStdOut);
|
const ErrorCode errorCode = sshSession.runScript(credentials, script, cbReadStdOut);
|
||||||
if (errorCode != ErrorCode::NoError) {
|
if (errorCode != ErrorCode::NoError) {
|
||||||
emit containerStatusRefreshed(3);
|
emit containerStatusRefreshed(3);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const QString status = stdOut.trimmed();
|
const QString status = stdOut.trimmed();
|
||||||
if (status == "running") {
|
if (status == "running") {
|
||||||
emit containerStatusRefreshed(1);
|
emit containerStatusRefreshed(1);
|
||||||
} else if (status == "not_found" || status.isEmpty()) {
|
} else if (status == "not_found" || status.isEmpty()) {
|
||||||
emit containerStatusRefreshed(0);
|
emit containerStatusRefreshed(0);
|
||||||
} else if (status == "exited" || status == "created" || status == "paused") {
|
} else if (status == "exited" || status == "created" || status == "paused") {
|
||||||
emit containerStatusRefreshed(2);
|
emit containerStatusRefreshed(2);
|
||||||
} else {
|
} else {
|
||||||
emit containerStatusRefreshed(3);
|
emit containerStatusRefreshed(3);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -394,55 +398,57 @@ void InstallUiController::refreshContainerDiagnostics(int serverIndex, int conta
|
|||||||
const DockerContainer container = static_cast<DockerContainer>(containerIndex);
|
const DockerContainer container = static_cast<DockerContainer>(containerIndex);
|
||||||
const QString containerName = ContainerUtils::containerToString(container);
|
const QString containerName = ContainerUtils::containerToString(container);
|
||||||
|
|
||||||
const QString script =
|
if (container == amnezia::ContainerEnumNS::MtProxy || container == amnezia::ContainerEnumNS::Telemt) {
|
||||||
QString(
|
const QString script =
|
||||||
"PORT_OK=$(sudo docker exec %1 sh -c 'ss -tlnp 2>/dev/null | grep -q :%2 && echo yes || echo no' 2>/dev/null || echo no); "
|
QString(
|
||||||
"TG_OK=$(curl -s --max-time 5 -o /dev/null -w '%%{http_code}' https://core.telegram.org/getProxySecret 2>/dev/null | grep -q '200' && echo yes || echo no); "
|
"PORT_OK=$(sudo docker exec %1 sh -c 'ss -tlnp 2>/dev/null | grep -q :%2 && echo yes || echo no' 2>/dev/null || echo no); "
|
||||||
"CLIENTS=$(sudo docker exec amnezia-mtproxy sh -c 'curl -s --max-time 3 http://localhost:2398/stats 2>/dev/null | grep -o \"total_special_connections:[0-9]*\" | cut -d: -f2' 2>/dev/null); "
|
"TG_OK=$(curl -s --max-time 5 -o /dev/null -w '%%{http_code}' https://core.telegram.org/getProxySecret 2>/dev/null | grep -q '200' && echo yes || echo no); "
|
||||||
"CONF_TIME=$(sudo docker exec amnezia-mtproxy sh -c 'stat -c \"%%y\" /data/proxy-multi.conf 2>/dev/null | cut -d. -f1' 2>/dev/null || echo unknown); "
|
"CLIENTS=$(sudo docker exec amnezia-mtproxy sh -c 'curl -s --max-time 3 http://localhost:2398/stats 2>/dev/null | grep -o \"total_special_connections:[0-9]*\" | cut -d: -f2' 2>/dev/null); "
|
||||||
"echo \"PORT_OK=${PORT_OK}\"; "
|
"CONF_TIME=$(sudo docker exec amnezia-mtproxy sh -c 'stat -c \"%%y\" /data/proxy-multi.conf 2>/dev/null | cut -d. -f1' 2>/dev/null || echo unknown); "
|
||||||
"echo \"TG_OK=${TG_OK}\"; "
|
"echo \"PORT_OK=${PORT_OK}\"; "
|
||||||
"echo \"CLIENTS=${CLIENTS:-0}\"; "
|
"echo \"TG_OK=${TG_OK}\"; "
|
||||||
"echo \"CONF_TIME=${CONF_TIME}\"; "
|
"echo \"CLIENTS=${CLIENTS:-0}\"; "
|
||||||
"echo \"STATS=http://localhost:2398/stats\";")
|
"echo \"CONF_TIME=${CONF_TIME}\"; "
|
||||||
.arg(containerName)
|
"echo \"STATS=http://localhost:2398/stats\";")
|
||||||
.arg(port);
|
.arg(containerName)
|
||||||
|
.arg(port);
|
||||||
|
|
||||||
QString stdOut;
|
QString stdOut;
|
||||||
auto cbReadStdOut = [&](const QString &data, libssh::Client &) {
|
auto cbReadStdOut = [&](const QString &data, libssh::Client &) {
|
||||||
stdOut += data;
|
stdOut += data;
|
||||||
return ErrorCode::NoError;
|
return ErrorCode::NoError;
|
||||||
};
|
};
|
||||||
|
|
||||||
SshSession sshSession(this);
|
SshSession sshSession(this);
|
||||||
const ErrorCode errorCode = sshSession.runScript(credentials, script, cbReadStdOut);
|
const ErrorCode errorCode = sshSession.runScript(credentials, script, cbReadStdOut);
|
||||||
if (errorCode != ErrorCode::NoError) {
|
if (errorCode != ErrorCode::NoError) {
|
||||||
emit containerDiagnosticsRefreshed(false, false, -1, QString(), QString());
|
emit containerDiagnosticsRefreshed(false, false, -1, QString(), QString());
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
bool portReachable = false;
|
|
||||||
bool upstreamReachable = false;
|
|
||||||
int clientsConnected = -1;
|
|
||||||
QString lastConfigRefresh;
|
|
||||||
QString statsEndpoint;
|
|
||||||
|
|
||||||
for (const QString &line: stdOut.split('\n', Qt::SkipEmptyParts)) {
|
|
||||||
if (line.startsWith("PORT_OK=")) {
|
|
||||||
portReachable = line.mid(8).trimmed() == "yes";
|
|
||||||
} else if (line.startsWith("TG_OK=")) {
|
|
||||||
upstreamReachable = line.mid(6).trimmed() == "yes";
|
|
||||||
} else if (line.startsWith("CLIENTS=")) {
|
|
||||||
clientsConnected = line.mid(8).trimmed().toInt();
|
|
||||||
} else if (line.startsWith("CONF_TIME=")) {
|
|
||||||
lastConfigRefresh = line.mid(10).trimmed();
|
|
||||||
} else if (line.startsWith("STATS=")) {
|
|
||||||
statsEndpoint = line.mid(6).trimmed();
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
emit containerDiagnosticsRefreshed(portReachable, upstreamReachable, clientsConnected, lastConfigRefresh,
|
bool portReachable = false;
|
||||||
statsEndpoint);
|
bool upstreamReachable = false;
|
||||||
|
int clientsConnected = -1;
|
||||||
|
QString lastConfigRefresh;
|
||||||
|
QString statsEndpoint;
|
||||||
|
|
||||||
|
for (const QString &line: stdOut.split('\n', Qt::SkipEmptyParts)) {
|
||||||
|
if (line.startsWith("PORT_OK=")) {
|
||||||
|
portReachable = line.mid(8).trimmed() == "yes";
|
||||||
|
} else if (line.startsWith("TG_OK=")) {
|
||||||
|
upstreamReachable = line.mid(6).trimmed() == "yes";
|
||||||
|
} else if (line.startsWith("CLIENTS=")) {
|
||||||
|
clientsConnected = line.mid(8).trimmed().toInt();
|
||||||
|
} else if (line.startsWith("CONF_TIME=")) {
|
||||||
|
lastConfigRefresh = line.mid(10).trimmed();
|
||||||
|
} else if (line.startsWith("STATS=")) {
|
||||||
|
statsEndpoint = line.mid(6).trimmed();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
emit containerDiagnosticsRefreshed(portReachable, upstreamReachable, clientsConnected, lastConfigRefresh,
|
||||||
|
statsEndpoint);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void InstallUiController::fetchContainerSecret(int serverIndex, int containerIndex) {
|
void InstallUiController::fetchContainerSecret(int serverIndex, int containerIndex) {
|
||||||
@@ -450,26 +456,27 @@ void InstallUiController::fetchContainerSecret(int serverIndex, int containerInd
|
|||||||
const DockerContainer container = static_cast<DockerContainer>(containerIndex);
|
const DockerContainer container = static_cast<DockerContainer>(containerIndex);
|
||||||
const QString containerName = ContainerUtils::containerToString(container);
|
const QString containerName = ContainerUtils::containerToString(container);
|
||||||
|
|
||||||
QString stdOut;
|
if (container == amnezia::ContainerEnumNS::MtProxy || container == amnezia::ContainerEnumNS::Telemt) {
|
||||||
auto cbReadStdOut = [&](const QString &data, libssh::Client &) {
|
QString stdOut;
|
||||||
stdOut += data;
|
auto cbReadStdOut = [&](const QString &data, libssh::Client &) {
|
||||||
return ErrorCode::NoError;
|
stdOut += data;
|
||||||
};
|
return ErrorCode::NoError;
|
||||||
|
};
|
||||||
|
|
||||||
SshSession sshSession(this);
|
SshSession sshSession(this);
|
||||||
const QString path = container == DockerContainer::Telemt ? QStringLiteral("/data/.amnezia-secret")
|
const QString path = QStringLiteral("/data/secret");
|
||||||
: QStringLiteral("/data/secret");
|
const QString cmd =
|
||||||
const QString cmd =
|
QStringLiteral("sudo docker exec %1 cat %2").arg(containerName, path);
|
||||||
QStringLiteral("sudo docker exec %1 cat %2").arg(containerName, path);
|
const ErrorCode errorCode = sshSession.runScript(credentials, cmd, cbReadStdOut);
|
||||||
const ErrorCode errorCode = sshSession.runScript(credentials, cmd, cbReadStdOut);
|
if (errorCode != ErrorCode::NoError) {
|
||||||
if (errorCode != ErrorCode::NoError) {
|
emit containerSecretFetched(QString());
|
||||||
emit containerSecretFetched(QString());
|
return;
|
||||||
return;
|
}
|
||||||
|
|
||||||
|
const QString secret = stdOut.trimmed();
|
||||||
|
static const QRegularExpression hex32(QStringLiteral("^[0-9a-fA-F]{32}$"));
|
||||||
|
emit containerSecretFetched(hex32.match(secret).hasMatch() ? secret : QString());
|
||||||
}
|
}
|
||||||
|
|
||||||
const QString secret = stdOut.trimmed();
|
|
||||||
static const QRegularExpression hex32(QStringLiteral("^[0-9a-fA-F]{32}$"));
|
|
||||||
emit containerSecretFetched(hex32.match(secret).hasMatch() ? secret : QString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void InstallUiController::rebootServer(int serverIndex)
|
void InstallUiController::rebootServer(int serverIndex)
|
||||||
|
|||||||
Reference in New Issue
Block a user