Skip to content
This repository has been archived by the owner on Dec 4, 2021. It is now read-only.

Commit

Permalink
Initial Release
Browse files Browse the repository at this point in the history
Initial release SSH Tunnel Plugin for Boardies MySQL Manager for Android
  • Loading branch information
boardy committed Jul 3, 2017
0 parents commit c531aa1
Show file tree
Hide file tree
Showing 41 changed files with 4,316 additions and 0 deletions.
32 changes: 32 additions & 0 deletions MySQLManager-TunnelPlugin_C++.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.24720.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MySQLManager-TunnelPlugin_C++", "MySQLManager-TunnelPlugin_C++\MySQLManager-TunnelPlugin_C++.vcxproj", "{C0F4FA51-B91A-4DC1-8334-51190CB115EC}"
EndProject
Global
GlobalSection(SubversionScc) = preSolution
Svn-Managed = True
Manager = AnkhSVN - Subversion Support for Visual Studio
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C0F4FA51-B91A-4DC1-8334-51190CB115EC}.Debug|x64.ActiveCfg = Debug|x64
{C0F4FA51-B91A-4DC1-8334-51190CB115EC}.Debug|x64.Build.0 = Debug|x64
{C0F4FA51-B91A-4DC1-8334-51190CB115EC}.Debug|x86.ActiveCfg = Debug|Win32
{C0F4FA51-B91A-4DC1-8334-51190CB115EC}.Debug|x86.Build.0 = Debug|Win32
{C0F4FA51-B91A-4DC1-8334-51190CB115EC}.Release|x64.ActiveCfg = Release|x64
{C0F4FA51-B91A-4DC1-8334-51190CB115EC}.Release|x64.Build.0 = Release|x64
{C0F4FA51-B91A-4DC1-8334-51190CB115EC}.Release|x86.ActiveCfg = Release|Win32
{C0F4FA51-B91A-4DC1-8334-51190CB115EC}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
17 changes: 17 additions & 0 deletions MySQLManager-TunnelPlugin_C++/ActiveTunnels.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "ActiveTunnels.h"


using namespace std;

/**
Container object for open SSH tunnels. These objects are stored within a list within the TunnelManager
to ensure that SSH tunnels that have been open for too long are terminated
@param sshTunnelForwarder The SSH tunnel forwarder class object, this is responsible for opening/closing and sending/receiving SSH data via the SSH socket
@param localPort The local port that is being used for the SSH tunnel
*/
ActiveTunnels::ActiveTunnels(SSHTunnelForwarder *sshTunnelForwarder, int localPort)
{
this->sshTunnelForwarder = sshTunnelForwarder;
this->localPort = localPort;
this->tunnelCreatedTime = std::time(nullptr);
}
20 changes: 20 additions & 0 deletions MySQLManager-TunnelPlugin_C++/ActiveTunnels.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once
#ifndef ACTIVETUNNELS_H
#define ACTIVETUNNELS_H
#ifndef SSHTUNNELFORWARDER_H
#include "SSHTunnelForwarder.h"
#endif
#include "StaticSettings.h"
#include "StatusManager.h"

class ActiveTunnels
{
public:
ActiveTunnels(SSHTunnelForwarder *sshTunnelForwarder, int localPort);
SSHTunnelForwarder *sshTunnelForwarder;
int localPort;
time_t tunnelCreatedTime;
};


#endif
34 changes: 34 additions & 0 deletions MySQLManager-TunnelPlugin_C++/BaseSocket.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
This class is the base class for socket handler classes, WindowsSocket.cpp and LinuxSocket.cpp
This class can't be created directly, will only be instantiated when WindowsSocket or LinuxSocket is initialised
*/

#include "BaseSocket.h"

using namespace std;

/**
@param logger The logger class to allow the socket handler classes mentioned above, be able to log the current state and debg
*/
BaseSocket::BaseSocket(Logger *logger)
{
this->logger = logger;
}

/**
This sets up the buffer length and socket port number being used, the WindowsSocket.cpp and LinuxSocket.cpp
will do other stuff to create the port but this is the default port that each platform has to do.
@param port The socket port number that is going to be created. This is confiruable within tunnel.conf. This is used when creating the socket that is used by the PHP API.
@param bufferLength This is the length of the buffer for the socket, i.e. this is how many bytes will be retrieved from the socket at a time.
*/
bool BaseSocket::createsocket(int port, int bufferLength)
{
this->bufferLength = bufferLength;
this->buffer = new char[bufferLength];
this->socketPort = port;

stringstream logstream;
logstream << "Creating buffer of length " << bufferLength;
this->logger->writeToLog(logstream.str(), "BaseSocket", "createSocket");
return true; //The base method can't fail but has to return soemthing
}
29 changes: 29 additions & 0 deletions MySQLManager-TunnelPlugin_C++/BaseSocket.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef BASESOCKET_H
#define BASESOCKET_H

#include <iostream>
#include "Logger.h"
#include <string>
#include "SocketException.h"

class BaseSocket
{
public:
BaseSocket(Logger *logger);
virtual bool createsocket(int port, int bufferLength);
virtual bool bindAndStartListening(int backlog) { return true; };
virtual bool bindAndStartListening() { return true; };
virtual int sendToSocket(void* clientSocket, std::string dataToSend) { return 0; };
virtual std::string getErrorStringFromErrorCode(int errorCode) { return string(); };
virtual std::string receiveDataOnSocket(void* socket) { return string(); };
virtual void closeSocket() {};
virtual void closeSocket(void* socket) {};
virtual void updateClassSocket(void *socket) {};
protected:
Logger *logger = NULL;
int bufferLength;
char *buffer = NULL;
int socketPort;
};

#endif //!BASESOCKET_H#pragma once
190 changes: 190 additions & 0 deletions MySQLManager-TunnelPlugin_C++/HelperMethods.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
/**
Some static methods that do some common tasks that may be needed
*/
#include "HelperMethods.h"
#include <string>
#include <vector>
#include <iostream>
#include <sstream>

static const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";

using namespace std;

static inline bool is_base64(unsigned char c) {
return (isalnum(c) || (c == '+') || (c == '/'));
}

/**
Base 64 encode a string
@param buffer The text to be base64 encoded
@param in_length The length of the text
@return string A base64 encoded string
*/
string HelperMethods::base64Encode(const char* buffer, int in_len)
{
std::string ret;
int i = 0;
int j = 0;
unsigned char char_array_3[3];
unsigned char char_array_4[4];

while (in_len--) {
char_array_3[i++] = *(buffer++);
if (i == 3) {
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;

for (i = 0; (i < 4); i++)
ret += base64_chars[char_array_4[i]];
i = 0;
}
}

if (i)
{
for (j = i; j < 3; j++)
char_array_3[j] = '\0';

char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;

for (j = 0; (j < i + 1); j++)
ret += base64_chars[char_array_4[j]];

while ((i++ < 3))
ret += '=';

}

return ret;
}

/**
Base64 decode a string
@param encoded_string The base64 encoded string that should be decoded
@return string The decoded version of the string
*/
string HelperMethods::base64Decode(string const& encoded_string)
{
int in_len = encoded_string.size();
int i = 0;
int j = 0;
int in_ = 0;
unsigned char char_array_4[4], char_array_3[3];
std::string ret;

while (in_len-- && (encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
char_array_4[i++] = encoded_string[in_]; in_++;
if (i == 4) {
for (i = 0; i <4; i++)
char_array_4[i] = base64_chars.find(char_array_4[i]);

char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];

for (i = 0; (i < 3); i++)
ret += char_array_3[i];
i = 0;
}
}

if (i) {
for (j = i; j <4; j++)
char_array_4[j] = 0;

for (j = 0; j <4; j++)
char_array_4[j] = base64_chars.find(char_array_4[j]);

char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];

for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
}

return ret;
}

/**
Split a string by a particular character into a vector of string
@param content The string content that should be split
@char delimiter A char of what should be used to do the split
@return vecor<string>
*/
vector<string> HelperMethods::splitString(string content, char delimiter)
{
stringstream contentStream(content);
string segment;
vector<string> seglist;
while (getline(contentStream, segment, delimiter))
{
seglist.push_back(segment);
}
return seglist;
}

/**
Trim the given string to remove white space
@param s The string that should be trimmed, the passed in string is modified, a copy is not returned.
*/
void HelperMethods::trimString(string& s) {
while (s.compare(0, 1, " ") == 0)
s.erase(s.begin()); // remove leading whitespaces
while (s.size()>0 && s.compare(s.size() - 1, 1, " ") == 0)
s.erase(s.end() - 1); // remove trailing whitespaces
}

/**
Find the filename and the extension of the passed in path
@param fileName The path and/or filename where the filename and extension should be returned
@param fileNameWithoutExt A pointer to the string, if the method is succesful, then this will contain just file name but no extension
@param extension A pointer to the string, if the method is successful, then this will contain just the exnteion, without the dot.
@return bool Returns true if both the filename and the extension was found, otherwise false is returned
*/
bool HelperMethods::findFileNameAndExtensionFromFileName(const string fileName, string *fileNameWithoutExt, string *extension)
{
size_t extensionStart = fileName.find('.');
if (extensionStart == string::npos)
{
*fileNameWithoutExt = "";
*extension = "";
return false; //No full stop so no extension, so return false and set the return points to an empty string
}

//A full stop was found so we can extract the extension
*fileNameWithoutExt = fileName.substr(0, extensionStart);
*extension = fileName.substr(extensionStart + 1);
return true;
}

/**
Check if the passed in directory path exists on the file system
@param directoryPath the path that should be checked if it exists
@return boolean false if the directory does not exist, otherwise true
*/
bool HelperMethods::doesDirectoryExist(string directoryPath)
{
struct stat info;
if (stat(directoryPath.c_str(), &info) != 0)
{
//Directory cannot be access
return false;
}
else if (info.st_mode & S_IFDIR)
{
return true;
}
else
{
return false;
}
}
30 changes: 30 additions & 0 deletions MySQLManager-TunnelPlugin_C++/HelperMethods.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef BITS_HELPERMETHODS_H

#define BITS_HELPERMETHODS_H


#include <iostream>
#include <openssl/sha.h>
#include <openssl/hmac.h>
#include <openssl/evp.h>
#include <openssl/bio.h>
#include <openssl/buffer.h>
#include <vector>

#include <sys/types.h>
#include <sys/stat.h>

using namespace std;

class HelperMethods
{
public:
string base64Encode(const char* buffer, int in_len);
string base64Decode(string const& encoded_string);
vector<string> splitString(string content, char delimiter);
void trimString(string& s);
bool findFileNameAndExtensionFromFileName(const string fileName, string *fileNameWithoutExt, string *extension);
bool doesDirectoryExist(string directPath);
};

#endif // !HELPERMETHODS_H
Loading

0 comments on commit c531aa1

Please sign in to comment.