Skip to content

Commit

Permalink
Add IP check for remote command
Browse files Browse the repository at this point in the history
  • Loading branch information
nekomona authored and ImUrX committed Jul 15, 2023
1 parent 4123311 commit 249c183
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#define serialDebug false // Set to true to get Serial output for debugging
#define serialBaudRate 115200
#define USE_REMOTE_COMMAND true
#define ALLOW_REMOTE_WIFI_PROV false
#define LED_INTERVAL_STANDBY 10000
#define PRINT_STATE_EVERY_MS 60000

Expand Down
2 changes: 2 additions & 0 deletions src/network/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ class Connection {
);
#endif

friend RemoteCmd;

private:
void updateSensorState(Sensor* const sensor1, Sensor* const sensor2);

Expand Down
28 changes: 24 additions & 4 deletions src/network/remotecmd.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "remotecmd.h"

#include "GlobalVars.h"

namespace SlimeVR {
namespace Network {

Expand All @@ -17,10 +19,28 @@ void RemoteCmd::update() {
r_Logger.info("Remote command multi-connection dropped");
} else {
rcmdClient = rcmdServer.accept();
r_Logger.info(
"Remote command from %s connected",
rcmdClient.remoteIP().toString().c_str()
);
if (networkConnection.isConnected()) {
// Only accept if rcmdClient have the same remote IP as udpmanager
if (rcmdClient.remoteIP() = networkConnection.m_ServerHost) {
rcmdClient.stop();
}
}
#if !ALLOW_REMOTE_WIFI_PROV
else {
rcmdClient.stop();
}
#endif
if (rcmdClient.connected()) {
r_Logger.info(
"Remote command from %s connected",
rcmdClient.remoteIP().toString().c_str()
);
} else {
r_Logger.info(
"Remote command from %s dropped",
rcmdClient.remoteIP().toString().c_str()
);
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/network/wifihandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ void WiFiNetwork::setUp() {
void onConnected() {
WiFiNetwork::stopProvisioning();
statusManager.setStatus(SlimeVR::Status::WIFI_CONNECTING, false);
networkRemoteCmd.reset();
isWifiConnected = true;
hadWifi = true;
wifiHandlerLogger.info("Connected successfully to SSID '%s', ip address %s", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
Expand Down
19 changes: 19 additions & 0 deletions src/serial/serialcommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace SerialCommands {
CmdCallback<6> cmdCallbacks;
CmdParser cmdParser;
CmdBuffer<64> cmdBuffer;
bool cmdFromRemote = false;

void cmdSet(CmdParser * parser) {
if(parser->getParamCount() != 1 && parser->equalCmdParam(1, "WIFI") ) {
Expand Down Expand Up @@ -84,6 +85,10 @@ namespace SerialCommands {
}

void cmdGet(CmdParser * parser) {
#if USE_REMOTE_COMMAND && ALLOW_REMOTE_WIFI_PROV
if (cmdFromRemote && !networkConnection.isConnected()) return;
#endif

if (parser->getParamCount() < 2) {
return;
}
Expand Down Expand Up @@ -141,11 +146,19 @@ namespace SerialCommands {
}

void cmdReboot(CmdParser * parser) {
#if USE_REMOTE_COMMAND && ALLOW_REMOTE_WIFI_PROV
if (cmdFromRemote && !networkConnection.isConnected()) return;
#endif

logger.info("REBOOT");
ESP.restart();
}

void cmdFactoryReset(CmdParser * parser) {
#if USE_REMOTE_COMMAND && ALLOW_REMOTE_WIFI_PROV
if (cmdFromRemote && !networkConnection.isConnected()) return;
#endif

logger.info("FACTORY RESET");

configuration.reset();
Expand All @@ -171,6 +184,10 @@ namespace SerialCommands {
}

void cmdTemperatureCalibration(CmdParser* parser) {
#if USE_REMOTE_COMMAND && ALLOW_REMOTE_WIFI_PROV
if (cmdFromRemote && !networkConnection.isConnected()) return;
#endif

if (parser->getParamCount() > 1) {
if (parser->equalCmdParam(1, "PRINT")) {
sensorManager.getFirst()->printTemperatureCalibrationState();
Expand Down Expand Up @@ -212,9 +229,11 @@ namespace SerialCommands {
#if USE_REMOTE_COMMAND
if (networkRemoteCmd.isConnected()) {
Stream & networkStream = networkRemoteCmd.getStream();
cmdFromRemote = true;
while (networkStream.available()) {
cmdCallbacks.updateCmdProcessing(&cmdParser, &cmdBuffer, &networkStream);
}
cmdFromRemote = false;
}
#endif
}
Expand Down

0 comments on commit 249c183

Please sign in to comment.