Browse Source

add "Bind" option

master
Syping 11 months ago
parent
commit
8c987d1288
  1. 12
      src/main.cpp
  2. 20
      src/proxyserver.cpp
  3. 2
      src/proxyserver.h

12
src/main.cpp

@ -11,7 +11,7 @@ int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
app.setApplicationName("proxylight");
app.setApplicationVersion("0.1");
app.setApplicationVersion("0.2");
QCommandLineParser commandLineParser;
commandLineParser.addHelpOption();
@ -49,10 +49,16 @@ int main(int argc, char *argv[])
const QString serverName = it.key();
int localPort = -1;
int remotePort = -1;
QString remoteHost;
QString serverBind, remoteHost;
const QJsonObject object = value.toObject();
for (auto it = object.constBegin(); it != object.constEnd(); it++) {
const QString key = it.key();
if (key == "Bind") {
const QJsonValue value = it.value();
if (value.isString()) {
serverBind = value.toString();
}
}
if (key == "LocalPort") {
const QJsonValue value = it.value();
if (value.isDouble()) {
@ -84,7 +90,7 @@ int main(int argc, char *argv[])
QTextStream(stderr) << serverName << ": Loading failed!" << Qt::endl;
break;
}
ProxyServer *proxyServer = new ProxyServer(localPort, remotePort, remoteHost, serverName);
ProxyServer *proxyServer = new ProxyServer(localPort, remotePort, remoteHost, serverName, serverBind);
if (!proxyServer->isListening()) {
QTextStream(stderr) << serverName << ": Binding failed!" << Qt::endl;
proxyServer->deleteLater();

20
src/proxyserver.cpp

@ -2,9 +2,25 @@
#include <QTextStream>
#include <QTcpSocket>
ProxyServer::ProxyServer(int localPort, int remotePort, const QString &remoteHost, const QString &serverName)
ProxyServer::ProxyServer(int localPort, int remotePort, const QString &remoteHost, const QString &serverName, const QString &serverBind)
{
listen(QHostAddress::LocalHost, localPort);
QHostAddress bindAddress = QHostAddress::LocalHost;
if (!serverBind.isEmpty()) {
if (serverBind == "::0.0.0.0") {
bindAddress = QHostAddress::Any;
}
else if (serverBind == "0.0.0.0") {
bindAddress = QHostAddress::AnyIPv4;
}
else if (serverBind == "::0") {
bindAddress = QHostAddress::AnyIPv6;
}
else {
if (!bindAddress.setAddress(serverBind))
return;
}
}
listen(bindAddress, localPort);
p_remotePort = remotePort;
p_remoteHost = remoteHost;
p_serverName = serverName;

2
src/proxyserver.h

@ -8,7 +8,7 @@ class ProxyServer : public QTcpServer
{
Q_OBJECT
public:
ProxyServer(int localPort, int remotePort, const QString &remoteHost, const QString &serverName);
ProxyServer(int localPort, int remotePort, const QString &remoteHost, const QString &serverName, const QString &serverBind);
protected:
void incomingConnection(qintptr socketDescriptor);

Loading…
Cancel
Save