Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a Man In The Middle for OBD and Moblie App
- Loading branch information
Rafael
committed
Feb 7, 2019
1 parent
c5f0ee5
commit 6bf08c5
Showing
6 changed files
with
247 additions
and
580 deletions.
There are no files selected for viewing
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,40 @@ | ||
#------------------------------------------------- | ||
# | ||
# Project created by QtCreator 2019-02-07T08:51:09 | ||
# | ||
#------------------------------------------------- | ||
|
||
QT += core gui | ||
|
||
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets network | ||
|
||
TARGET = ManInTheMiddleOBD | ||
TEMPLATE = app | ||
|
||
# The following define makes your compiler emit warnings if you use | ||
# any feature of Qt which has been marked as deprecated (the exact warnings | ||
# depend on your compiler). Please consult the documentation of the | ||
# deprecated API in order to know how to port your code away from it. | ||
DEFINES += QT_DEPRECATED_WARNINGS | ||
|
||
# You can also make your code fail to compile if you use deprecated APIs. | ||
# In order to do so, uncomment the following line. | ||
# You can also select to disable deprecated APIs only up to a certain version of Qt. | ||
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 | ||
|
||
CONFIG += c++11 | ||
|
||
SOURCES += \ | ||
main.cpp \ | ||
mainwindow.cpp | ||
|
||
HEADERS += \ | ||
mainwindow.h | ||
|
||
FORMS += \ | ||
mainwindow.ui | ||
|
||
# Default rules for deployment. | ||
qnx: target.path = /tmp/$${TARGET}/bin | ||
else: unix:!android: target.path = /opt/$${TARGET}/bin | ||
!isEmpty(target.path): INSTALLS += target |
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,11 @@ | ||
#include "mainwindow.h" | ||
#include <QApplication> | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
QApplication a(argc, argv); | ||
MainWindow w; | ||
w.show(); | ||
|
||
return a.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,99 @@ | ||
#include "mainwindow.h" | ||
#include "ui_mainwindow.h" | ||
|
||
MainWindow::MainWindow(QWidget *parent) : | ||
QMainWindow(parent), | ||
ui(new Ui::MainWindow) | ||
{ | ||
ui->setupUi(this); | ||
|
||
socket = new QTcpSocket(this); | ||
|
||
socket->connectToHost("192.168.0.10", 35000); | ||
|
||
if(socket->waitForConnected()) | ||
LogDat("Connected to OBD"); | ||
else | ||
LogDatError("Failed to connect"); | ||
|
||
connect(socket, &QTcpSocket::readyRead, this, &MainWindow::readNetwork1Line); | ||
|
||
|
||
server = new QTcpServer(this); | ||
|
||
if(server->listen(QHostAddress::Any, 1024)) | ||
LogDat("Listening to 1024"); | ||
else | ||
LogDatError("Failed to listen"); | ||
|
||
connect(server, &QTcpServer::newConnection, this, &MainWindow::newConnection); | ||
} | ||
|
||
MainWindow::~MainWindow() | ||
{ | ||
delete ui; | ||
} | ||
|
||
QByteArray MainWindow::readLine(QTcpSocket* sk) | ||
{ | ||
QByteArray msg = ""; | ||
while(sk->bytesAvailable() > 0){ | ||
QByteArray c = sk->read(1); | ||
msg.append(c); | ||
if(c[0] == '>'){ | ||
return msg; | ||
} | ||
} | ||
return ""; | ||
} | ||
|
||
void MainWindow::newConnection() | ||
{ | ||
while (server->hasPendingConnections()) | ||
{ | ||
QTcpSocket *s_socket = server->nextPendingConnection(); | ||
LogDat("Client connected from " + QHostAddress(s_socket->peerAddress().toIPv4Address()).toString() + "::" + QString::number(s_socket->peerPort())); | ||
clients.append(s_socket); | ||
connect(s_socket, &QTcpSocket::readyRead, this, &MainWindow::readNetwork2Line); | ||
connect(s_socket, &QTcpSocket::disconnected, this, &MainWindow::disconnected); | ||
} | ||
} | ||
|
||
void MainWindow::disconnected() | ||
{ | ||
QTcpSocket *s_socket = static_cast<QTcpSocket*>(sender()); | ||
LogDat("Client disconnected from " + QHostAddress(s_socket->peerAddress().toIPv4Address()).toString() + "::" + QString::number(s_socket->peerPort())); | ||
s_socket->deleteLater(); | ||
clients.removeAll(s_socket); | ||
} | ||
|
||
void MainWindow::readNetwork1Line() | ||
{ | ||
QByteArray msg = readLine(socket); | ||
LogDat("OBD sended: " + msg); | ||
foreach(QTcpSocket* sk, clients) | ||
sk->write(msg); | ||
} | ||
|
||
void MainWindow::readNetwork2Line() | ||
{ | ||
QTcpSocket *s_socket = static_cast<QTcpSocket*>(sender()); | ||
QByteArray msg = readLine(s_socket); | ||
LogDat("Mobile App sended: " + msg); | ||
socket->write(msg); | ||
} | ||
|
||
void MainWindow::LogDat(QString log) | ||
{ | ||
ui->scrollArea->widget()->layout()->addWidget(new QLabel(log)); | ||
} | ||
|
||
void MainWindow::LogDatError(QString log) | ||
{ | ||
QLabel* aux = new QLabel("ERROR: " + log); | ||
QPalette* aux2 = new QPalette(); | ||
aux2->setColor(aux->foregroundRole(), Qt::red); | ||
aux->setPalette(*aux2); | ||
|
||
ui->scrollArea->widget()->layout()->addWidget(aux); | ||
} |
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,36 @@ | ||
#ifndef MAINWINDOW_H | ||
#define MAINWINDOW_H | ||
|
||
#include <QMainWindow> | ||
#include <QTcpSocket> | ||
#include <QTcpServer> | ||
#include <QLabel> | ||
|
||
namespace Ui { | ||
class MainWindow; | ||
} | ||
|
||
class MainWindow : public QMainWindow | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
explicit MainWindow(QWidget *parent = nullptr); | ||
~MainWindow(); | ||
|
||
private: | ||
Ui::MainWindow *ui; | ||
QTcpSocket *socket; | ||
QTcpServer *server; | ||
QList<QTcpSocket*> clients; | ||
|
||
void readNetwork1Line(); | ||
void readNetwork2Line(); | ||
void LogDat(QString log); | ||
void LogDatError(QString log); | ||
QByteArray readLine(QTcpSocket* sk); | ||
void newConnection(); | ||
void disconnected(); | ||
}; | ||
|
||
#endif // MAINWINDOW_H |
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,61 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ui version="4.0"> | ||
<class>MainWindow</class> | ||
<widget class="QMainWindow" name="MainWindow"> | ||
<property name="geometry"> | ||
<rect> | ||
<x>0</x> | ||
<y>0</y> | ||
<width>400</width> | ||
<height>300</height> | ||
</rect> | ||
</property> | ||
<property name="windowTitle"> | ||
<string>MainWindow</string> | ||
</property> | ||
<widget class="QWidget" name="centralWidget"> | ||
<layout class="QVBoxLayout" name="verticalLayout"> | ||
<item> | ||
<widget class="QScrollArea" name="scrollArea"> | ||
<property name="widgetResizable"> | ||
<bool>true</bool> | ||
</property> | ||
<widget class="QWidget" name="scrollAreaWidgetContents"> | ||
<property name="geometry"> | ||
<rect> | ||
<x>0</x> | ||
<y>0</y> | ||
<width>386</width> | ||
<height>237</height> | ||
</rect> | ||
</property> | ||
<layout class="QVBoxLayout" name="verticalLayout_3"/> | ||
</widget> | ||
</widget> | ||
</item> | ||
</layout> | ||
</widget> | ||
<widget class="QMenuBar" name="menuBar"> | ||
<property name="geometry"> | ||
<rect> | ||
<x>0</x> | ||
<y>0</y> | ||
<width>400</width> | ||
<height>17</height> | ||
</rect> | ||
</property> | ||
</widget> | ||
<widget class="QToolBar" name="mainToolBar"> | ||
<attribute name="toolBarArea"> | ||
<enum>TopToolBarArea</enum> | ||
</attribute> | ||
<attribute name="toolBarBreak"> | ||
<bool>false</bool> | ||
</attribute> | ||
</widget> | ||
<widget class="QStatusBar" name="statusBar"/> | ||
</widget> | ||
<layoutdefault spacing="6" margin="11"/> | ||
<resources/> | ||
<connections/> | ||
</ui> |
Oops, something went wrong.