Skip to content

Commit

Permalink
Support server options in Trojan server subscription.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dr-Incognito committed Apr 3, 2020
1 parent 47a275e commit 7031f99
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
40 changes: 29 additions & 11 deletions src/serverconfighelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,24 +378,42 @@ QJsonObject ServerConfigHelper::getTrojanServerConfigFromUrl(
int atIndex = serverUrl.indexOf('@');
int colonIndex = serverUrl.indexOf(':');
int sharpIndex = serverUrl.indexOf('#');
int questionMarkIndex =
serverUrl.indexOf('?') == -1 ? sharpIndex : serverUrl.indexOf('?');

QString password = serverUrl.left(atIndex);
QString serverAddr = serverUrl.mid(atIndex + 1, colonIndex - atIndex - 1);
QString serverPort =
serverUrl.mid(colonIndex + 1, sharpIndex - colonIndex - 1);
serverUrl.mid(colonIndex + 1, questionMarkIndex - colonIndex - 1);
QString options =
serverUrl.mid(questionMarkIndex + 1, sharpIndex - questionMarkIndex - 1);
QString serverName =
QUrl::fromPercentEncoding(serverUrl.mid(sharpIndex + 1).toUtf8()).trimmed();

return QJsonObject{{"serverName", serverName},
{"autoConnect", false},
{"subscription", subscriptionUrl},
{"serverAddr", serverAddr},
{"serverPort", serverPort},
{"password", password},
{"sni", DEFAULT_TROJRAN_SNI},
{"udp", DEFAULT_TROJRAN_ENABLE_UDP},
{"alpn", DEFAULT_TROJRAN_ALPN},
{"allowInsecure", DEFAULT_TROJRAN_ALLOW_INSECURE}};
QJsonObject serverConfig{
{"serverName", serverName}, {"autoConnect", false},
{"subscription", subscriptionUrl}, {"serverAddr", serverAddr},
{"serverPort", serverPort}, {"password", password}};

QJsonObject serverOptions = getTrojanOptions(options);
for (auto itr = serverOptions.begin(); itr != serverOptions.end(); ++itr) {
serverConfig[itr.key()] = itr.value();
}
return serverConfig;
}

QJsonObject ServerConfigHelper::getTrojanOptions(const QString& optionString) {
QJsonObject options{{"sni", DEFAULT_TROJRAN_SNI},
{"udp", DEFAULT_TROJRAN_ENABLE_UDP},
{"alpn", DEFAULT_TROJRAN_ALPN},
{"allowInsecure", DEFAULT_TROJRAN_ALLOW_INSECURE}};

for (QPair<QString, QString> p : QUrlQuery(optionString).queryItems()) {
if (options.contains(p.first)) {
options[p.first] = p.second;
}
}
return options;
}

QList<QJsonObject> ServerConfigHelper::getServerConfigFromV2RayConfig(
Expand Down
1 change: 1 addition & 0 deletions src/serverconfighelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class ServerConfigHelper : public QObject {
static QJsonObject getPrettyTrojanConfig(const QJsonObject &serverConfig);
static QJsonObject getTrojanServerConfigFromUrl(
QString serverUrl, const QString &subscriptionUrl);
static QJsonObject getTrojanOptions(const QString &optionString);
};

#endif // SERVERCONFIGHELPER_H

0 comments on commit 7031f99

Please sign in to comment.