-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(copy_terminal_proxy_cmd): 添加终端代理指令复制小图标
- Loading branch information
Showing
7 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#include <QApplication> | ||
#include <QSystemTrayIcon> | ||
#include <QMenu> | ||
#include <QAction> | ||
#include <QClipboard> | ||
#include <QNetworkInterface> | ||
#include <QFile> | ||
#include <QTextStream> | ||
|
||
// 获取网络接口和IPv4地址的函数 | ||
QMap<QString, QString> getNetworkInterfaces() { | ||
QMap<QString, QString> interfaceAddresses; | ||
for (const QNetworkInterface& networkInterface : QNetworkInterface::allInterfaces()) { | ||
// 检查接口是否已激活 | ||
if (networkInterface.flags().testFlag(QNetworkInterface::IsUp) && | ||
!networkInterface.flags().testFlag(QNetworkInterface::IsLoopBack)) { | ||
// 获取IPv4地址 | ||
for (const QNetworkAddressEntry& entry : networkInterface.addressEntries()) { | ||
if (entry.ip().protocol() == QAbstractSocket::IPv4Protocol) { | ||
interfaceAddresses.insert(networkInterface.humanReadableName(), entry.ip().toString()); | ||
} | ||
} | ||
} | ||
} | ||
interfaceAddresses.insert("lo", "127.0.0.1"); | ||
return interfaceAddresses; | ||
} | ||
|
||
|
||
QString readProxyTemplate(const QString& filePath) { | ||
QFile file(filePath); | ||
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { | ||
return ""; // 如果文件打开失败,返回空字符串 | ||
} | ||
QTextStream in(&file); | ||
QString content = in.readAll(); | ||
file.close(); | ||
return content; | ||
} | ||
|
||
int main(int argc, char *argv[]) { | ||
QApplication app(argc, argv); | ||
QSystemTrayIcon trayIcon; | ||
QMenu menu; | ||
QAction *exitAction = new QAction("退出", &menu); | ||
|
||
// 读取代理模板文件 | ||
QString proxyTemplate = readProxyTemplate("proxy_template.txt"); | ||
|
||
// 获取网络接口和IPv4地址 | ||
QMap<QString, QString> interfaces = getNetworkInterfaces(); | ||
|
||
// 为每个IP地址创建菜单项 | ||
for (auto it = interfaces.constBegin(); it != interfaces.constEnd(); ++it) { | ||
QAction *interfaceAction = new QAction(it.key() + ": " + it.value(), &menu); | ||
menu.addAction(interfaceAction); | ||
|
||
// 连接菜单项的触发信号到槽函数,复制IP地址到剪切板 | ||
QObject::connect(interfaceAction, &QAction::triggered, [proxyTemplate, it](bool) { | ||
QString formattedProxy = proxyTemplate.arg(it.value()); | ||
QApplication::clipboard()->setText(formattedProxy); | ||
}); | ||
} | ||
|
||
menu.addSeparator(); | ||
menu.addAction(exitAction); | ||
|
||
// 连接退出菜单项的触发信号到应用程序的quit槽 | ||
QObject::connect(exitAction, &QAction::triggered, &QApplication::quit); | ||
|
||
trayIcon.setContextMenu(&menu); | ||
// QPixmap pixmap(":/clash.png"); | ||
// QIcon icon(pixmap.scaled(10, 10, Qt::KeepAspectRatio, Qt::SmoothTransformation)); | ||
// trayIcon.setIcon(icon); | ||
trayIcon.setIcon(QIcon(":/clash.png")); | ||
trayIcon.show(); | ||
|
||
return app.exec(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
TEMPLATE = app | ||
TARGET = get_terminal_proxy_cmd | ||
INCLUDEPATH += . | ||
|
||
# Add the necessary Qt modules here | ||
QT += widgets network core svg | ||
|
||
SOURCES += get_terminal_proxy_cmd.cpp | ||
RESOURCES += resources.qrc | ||
|
||
# qmake ../get_terminal_proxy_cmd.pro | ||
# make | ||
# ./get_terminal_proxy_cmd | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!/bin/bash | ||
|
||
# 获取特定网络接口的 IPv4 地址 | ||
# 例如,假设你的网络接口是 eth0,你可以根据实际情况修改它 | ||
INTERFACE="eth0" | ||
IP=$(ip addr show $INTERFACE | grep 'inet ' | awk '{print $2}' | cut -d/ -f1) | ||
|
||
# 检查是否成功获取到 IP 地址 | ||
if [ -z "$IP" ]; then | ||
echo "无法获取 IP 地址" | ||
exit 1 | ||
fi | ||
|
||
# 构造 export 命令 | ||
EXPORT_CMD="export https_proxy=http://$IP:7890 http_proxy=http://$IP:7890 all_proxy=socks5://$IP:7890" | ||
|
||
# 打印出执行的命令,以便确认 | ||
echo $EXPORT_CMD |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export https_proxy=http://%1:7890 http_proxy=http://%1:7890 all_proxy=socks5://%1:7890 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<RCC> | ||
<qresource prefix="/"> | ||
<file>clash.png</file> | ||
<!-- 其他文件 --> | ||
</qresource> | ||
</RCC> |