Skip to content

Commit

Permalink
feat(copy_terminal_proxy_cmd): 添加终端代理指令复制小图标
Browse files Browse the repository at this point in the history
  • Loading branch information
zymouse committed Dec 1, 2023
1 parent d016742 commit 84666c9
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 0 deletions.
Binary file added script/copy_terminal_proxy_cmd/clash.png
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.
79 changes: 79 additions & 0 deletions script/copy_terminal_proxy_cmd/get_terminal_proxy_cmd.cpp
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();
}
14 changes: 14 additions & 0 deletions script/copy_terminal_proxy_cmd/get_terminal_proxy_cmd.pro
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

18 changes: 18 additions & 0 deletions script/copy_terminal_proxy_cmd/get_terminal_proxy_cmd.sh
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
1 change: 1 addition & 0 deletions script/copy_terminal_proxy_cmd/proxy_template.txt
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
6 changes: 6 additions & 0 deletions script/copy_terminal_proxy_cmd/resources.qrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<RCC>
<qresource prefix="/">
<file>clash.png</file>
<!-- 其他文件 -->
</qresource>
</RCC>

0 comments on commit 84666c9

Please sign in to comment.