Skip to content

Commit 57f87bc

Browse files
authored
Merge branch 'refactor' into main
2 parents 6e31630 + 9f90586 commit 57f87bc

25 files changed

+633
-819
lines changed

CMakeLists.txt

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,30 @@ project(AIS-catcher)
1616
find_package(Git QUIET)
1717

1818
if(GIT_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git")
19-
20-
set(GIT_VERSION_DESCRIBE "")
21-
22-
# First try to fetch tags (ignore errors if repo is already complete)
19+
# For shallow clones, fetch enough history to reach a tag
2320
execute_process(
24-
COMMAND ${GIT_EXECUTABLE} fetch --prune --unshallow
21+
COMMAND ${GIT_EXECUTABLE} fetch --deepen=200 --tags
2522
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
26-
OUTPUT_VARIABLE GIT_FETCH_OUTPUT
27-
ERROR_VARIABLE GIT_FETCH_ERROR
28-
OUTPUT_STRIP_TRAILING_WHITESPACE
23+
OUTPUT_QUIET
2924
ERROR_QUIET
30-
RESULT_VARIABLE GIT_FETCH_RESULT
3125
)
32-
33-
# Now get the version using git describe
26+
27+
# Get the version using git describe
3428
execute_process(
35-
COMMAND ${GIT_EXECUTABLE} describe --tags
29+
COMMAND ${GIT_EXECUTABLE} describe --tags --always --dirty
3630
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
3731
OUTPUT_VARIABLE GIT_VERSION_DESCRIBE
3832
OUTPUT_STRIP_TRAILING_WHITESPACE
3933
RESULT_VARIABLE GIT_RESULT
40-
ERROR_VARIABLE GIT_ERROR
34+
ERROR_QUIET
4135
)
4236

4337
if(GIT_VERSION_DESCRIBE)
4438
set(VERSION_DESCRIBE "${GIT_VERSION_DESCRIBE}")
4539
message(STATUS "Git version: ${VERSION_DESCRIBE}")
4640
else()
4741
set(VERSION_DESCRIBE "v0.00-1-unknown")
48-
message(STATUS "All git describe methods failed, using fallback version: ${VERSION_DESCRIBE}")
42+
message(STATUS "Git describe failed, using fallback version: ${VERSION_DESCRIBE}")
4943
endif()
5044
else()
5145
set(VERSION_DESCRIBE "v0.00-1-unknown")
@@ -159,7 +153,6 @@ endif()
159153

160154
if(NOT MSVC AND NMEA2000)
161155

162-
163156
check_include_file("linux/can.h" HAVE_CAN_HEADER)
164157
check_include_file("linux/can/raw.h" HAVE_RAW_CAN_HEADER)
165158

@@ -245,7 +238,6 @@ if(OPENSSL)
245238
endif()
246239
endif()
247240

248-
249241
# Find libcurl
250242
if(CURL)
251243
if(MSVC AND NOT MSVC_VCPKG)
@@ -884,7 +876,7 @@ set(CPP
884876
Source/Marine/N2K.cpp
885877
Source/Marine/NMEA.cpp
886878
Source/Library/Logger.cpp
887-
Source/Library/TCP.cpp
879+
Source/IO/TCPServer.cpp
888880
Source/Tracking/DB.cpp
889881
Source/Tracking/Ships.cpp
890882
Source/Utilities/Parse.cpp
@@ -899,7 +891,7 @@ set(HEADER
899891
Source/Application/AIS-catcher.h Source/Application/Prometheus.h Source/Application/Config.h Source/Application/DeviceManager.h Source/Application/WebDB.h Source/Library/Logger.h Source/Application/WebViewer.h Source/Application/Receiver.h Source/Tracking/Ships.h Source/Tracking/DB.h Source/DBMS/PostgreSQL.h Source/IO/HTTPClient.h Source/Application/MapTiles.h Source/Aviation/Beast.h
900892
Source/Device/Device.h Source/Device/FileWAV.h Source/Device/RTLTCP.h Source/Device/UDP.h Source/DSP/Demod.h Source/DSP/Filters.h Source/Marine/AIS.h Source/Marine/Message.h Source/Marine/NMEA.h Source/Library/ZIP.h Source/Library/Signals.h Source/Device/SoapySDR.h Source/JSON/JSONAIS.h Source/JSON/JSON.h Source/Aviation/Basestation.h Source/Aviation/ADSB.h
901893
Source/Device/AIRSPY.h Source/Library/FIFO.h Source/Device/N2KsktCAN.h Source/Device/HACKRF.h Source/Device/SDRPLAY.h Source/DSP/DSP.h Source/DSP/Model.h Source/Tracking/History.h Source/Tracking/Statistics.h Source/Library/Common.h Source/Library/Stream.h Source/Device/SpyServer.h Source/JSON/Keys.h Source/JSON/StringBuilder.h Source/JSON/Parser.h Source/Tracking/PlaneDB.h
902-
Source/Device/Serial.h Source/IO/N2KInterface.h Source/Marine/N2K.h Source/IO/N2KStream.h Source/Device/AIRSPYHF.h Source/Device/FileRAW.h Source/Device/RTLSDR.h Source/Device/ZMQ.h Source/DSP/FFT.h Source/IO/MsgOut.h Source/IO/Screen.h Source/IO/File.h Source/IO/StreamCounter.h Source/IO/Network.h Source/IO/HTTPServer.h Source/Utilities/StreamHelpers.h Source/Library/TCP.h Source/IO/Protocol.h
894+
Source/Device/Serial.h Source/IO/N2KInterface.h Source/Marine/N2K.h Source/IO/N2KStream.h Source/Device/AIRSPYHF.h Source/Device/FileRAW.h Source/Device/RTLSDR.h Source/Device/ZMQ.h Source/DSP/FFT.h Source/IO/MsgOut.h Source/IO/Screen.h Source/IO/File.h Source/IO/StreamCounter.h Source/IO/Network.h Source/IO/HTTPServer.h Source/Utilities/StreamHelpers.h Source/IO/TCPServer.h Source/IO/Protocol.h
903895
Source/Utilities/Parse.h Source/Utilities/Convert.h Source/Utilities/Helper.h Source/Utilities/Serialize.h Source/Utilities/PackedInt.h Source/Utilities/TemplateString.h)
904896

905897
set(APP_INCLUDES . ./Source ./Source/Tracking ./Source/DBMS ./Source/Library ./Source/Marine ./Source/Aviation ./Source/DSP ./Source/Application ./Source/IO ./Source/JSON ./Source/Utilities)

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
SRC = Tracking/Ships.cpp Marine/N2K.cpp IO/N2KInterface.cpp Device/N2KsktCAN.cpp IO/N2KStream.cpp Application/Prometheus.cpp Application/Main.cpp Application/WebViewer.cpp Application/MapTiles.cpp IO/HTTPClient.cpp DBMS/PostgreSQL.cpp Tracking/DB.cpp Application/Config.cpp Application/DeviceManager.cpp Application/Receiver.cpp IO/HTTPServer.cpp DSP/DSP.cpp JSON/JSONAIS.cpp JSON/Parser.cpp JSON/StringBuilder.cpp JSON/Keys.cpp Marine/AIS.cpp IO/Network.cpp DSP/Model.cpp Marine/NMEA.cpp DSP/Demod.cpp Marine/Message.cpp Device/UDP.cpp Device/ZMQ.cpp Device/RTLSDR.cpp Device/AIRSPYHF.cpp Device/SoapySDR.cpp Device/AIRSPY.cpp Device/FileRAW.cpp Device/FileWAV.cpp Device/SDRPLAY.cpp Device/RTLTCP.cpp Device/HACKRF.cpp Device/Serial.cpp Library/TCP.cpp Device/SpyServer.cpp JSON/JSON.cpp IO/Protocol.cpp IO/MsgOut.cpp IO/Screen.cpp Library/Logger.cpp Aviation/Basestation.cpp Application/WebDB.cpp Aviation/Beast.cpp Aviation/ADSB.cpp Utilities/Parse.cpp Utilities/Convert.cpp Utilities/Helper.cpp Utilities/Serialize.cpp Utilities/TemplateString.cpp Utilities/StreamHelpers.cpp
2-
OBJ = Ships.o Main.o N2KStream.o N2K.o N2KInterface.o N2KsktCAN.o Prometheus.o DeviceManager.o Receiver.o Config.o WebViewer.o MapTiles.o HTTPClient.o PostgreSQL.o DB.o DSP.o AIS.o Model.o Network.o Demod.o Serial.o RTLSDR.o HTTPServer.o AIRSPYHF.o Keys.o AIRSPY.o Parser.o StringBuilder.o FileRAW.o FileWAV.o SDRPLAY.o NMEA.o RTLTCP.o HACKRF.o ZMQ.o UDP.o SoapySDR.o TCP.o Message.o SpyServer.o JSON.o JSONAIS.o Protocol.o MsgOut.o Screen.o Logger.o Basestation.o WebDB.o Beast.o ADSB.o Parse.o Convert.o Helper.o Serialize.o TemplateString.o StreamHelpers.o
1+
SRC = Tracking/Ships.cpp Marine/N2K.cpp IO/N2KInterface.cpp Device/N2KsktCAN.cpp IO/N2KStream.cpp Application/Prometheus.cpp Application/Main.cpp Application/WebViewer.cpp Application/MapTiles.cpp IO/HTTPClient.cpp DBMS/PostgreSQL.cpp Tracking/DB.cpp Application/Config.cpp Application/DeviceManager.cpp Application/Receiver.cpp IO/HTTPServer.cpp DSP/DSP.cpp JSON/JSONAIS.cpp JSON/Parser.cpp JSON/StringBuilder.cpp JSON/Keys.cpp Marine/AIS.cpp IO/Network.cpp DSP/Model.cpp Marine/NMEA.cpp DSP/Demod.cpp Marine/Message.cpp Device/UDP.cpp Device/ZMQ.cpp Device/RTLSDR.cpp Device/AIRSPYHF.cpp Device/SoapySDR.cpp Device/AIRSPY.cpp Device/FileRAW.cpp Device/FileWAV.cpp Device/SDRPLAY.cpp Device/RTLTCP.cpp Device/HACKRF.cpp Device/Serial.cpp IO/TCPServer.cpp Device/SpyServer.cpp JSON/JSON.cpp IO/Protocol.cpp IO/MsgOut.cpp IO/Screen.cpp Library/Logger.cpp Aviation/Basestation.cpp Application/WebDB.cpp Aviation/Beast.cpp Aviation/ADSB.cpp Utilities/Parse.cpp Utilities/Convert.cpp Utilities/Helper.cpp Utilities/Serialize.cpp Utilities/TemplateString.cpp Utilities/StreamHelpers.cpp
2+
OBJ = Ships.o Main.o N2KStream.o N2K.o N2KInterface.o N2KsktCAN.o Prometheus.o DeviceManager.o Receiver.o Config.o WebViewer.o MapTiles.o HTTPClient.o PostgreSQL.o DB.o DSP.o AIS.o Model.o Network.o Demod.o Serial.o RTLSDR.o HTTPServer.o AIRSPYHF.o Keys.o AIRSPY.o Parser.o StringBuilder.o FileRAW.o FileWAV.o SDRPLAY.o NMEA.o RTLTCP.o HACKRF.o ZMQ.o UDP.o SoapySDR.o TCPServer.o Message.o SpyServer.o JSON.o JSONAIS.o Protocol.o MsgOut.o Screen.o Logger.o Basestation.o WebDB.o Beast.o ADSB.o Parse.o Convert.o Helper.o Serialize.o TemplateString.o StreamHelpers.o
33
INCLUDE = -I. -ISource -ISource/JSON/ -ISource/DBMS/ -ISource/Tracking/ -ISource/Library/ -ISource/Marine/ -ISource/Aviation/ -ISource/DSP/ -ISource/Application/ -ISource/IO/ -ISource/Utilities/
44
CC = clang
55

66
override CFLAGS += -Ofast -std=c++11 -g -pg -Wno-sign-compare $(INCLUDE)
7-
override LFLAGS += -lstdc++ -lpthread -g -pg -lm -o AIS-catcher
7+
override LFLAGS += -lstdc++ -lpthread -g -pg -lm -o AIS-catcher
88

99
CFLAGS_RTL = -DHASRTLSDR $(shell pkg-config --cflags librtlsdr)
1010
CFLAGS_AIRSPYHF = -DHASAIRSPYHF $(shell pkg-config --cflags libairspyhf)

Source/Application/Config.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ void Config::setSharing(const std::vector<JSON::Property> &props)
350350
_msg.push_back(std::unique_ptr<IO::OutputMessage>(new IO::TCPClientStreamer()));
351351
commm_feed = _msg.back().get();
352352

353-
commm_feed->Set("HOST", AISCATCHER_URL).Set("PORT", AISCATCHER_PORT).Set("JSON", "on").Set("FILTER", "on").Set("GPS", "off").Set("REMOVE_EMPTY","on").Set("KEEP_ALIVE", "on").Set("DOWNSAMPLE", "on").Set("INCLUDE_SAMPLE_START", "on");
353+
commm_feed->Set("HOST", AISCATCHER_URL).Set("PORT", AISCATCHER_PORT).Set("MSGFORMAT", "COMMUNITY_HUB").Set("FILTER", "on").Set("GPS", "off").Set("REMOVE_EMPTY","on").Set("KEEP_ALIVE", "on").Set("DOWNSAMPLE", "on").Set("INCLUDE_SAMPLE_START", "on");
354354
}
355355
if (!uuid.empty() && commm_feed)
356356
commm_feed->Set("UUID", uuid);

Source/Application/Prometheus.cpp

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@ const std::string ShippingClassNames[] = {
3434
"Search and Rescue Transponder EPIRB" // CLASS_SARTEPIRB
3535
};
3636

37-
PromotheusCounter::PromotheusCounter() {
37+
PromotheusCounter::PromotheusCounter()
38+
{
3839
Reset();
3940
Clear();
4041
}
4142

42-
void PromotheusCounter::Clear() {
43+
void PromotheusCounter::Clear()
44+
{
4345

4446
m.lock();
4547
std::memset(_msg, 0, sizeof(_msg));
@@ -51,10 +53,13 @@ void PromotheusCounter::Clear() {
5153
m.unlock();
5254
}
5355

54-
void PromotheusCounter::Add(const AIS::Message& m, const TAG& tag, bool new_vessel) {
56+
void PromotheusCounter::Add(const AIS::Message &m, const TAG &tag, bool new_vessel)
57+
{
5558

56-
if (m.type() > 27 || m.type() < 1) return;
57-
if (tag.shipclass < 0 || tag.shipclass > 13) return;
59+
if (m.type() > 27 || m.type() < 1)
60+
return;
61+
if (tag.shipclass < 0 || tag.shipclass > 13)
62+
return;
5863

5964
std::string speed = tag.speed < 0 ? "Unknown" : (tag.speed > 0.5 ? "Moving" : "Stationary");
6065

@@ -67,19 +72,21 @@ void PromotheusCounter::Add(const AIS::Message& m, const TAG& tag, bool new_vess
6772
_count++;
6873
_msg[m.type() - 1]++;
6974

70-
71-
if (m.getChannel() >= 'A' || m.getChannel() <= 'D')
75+
if (m.getChannel() >= 'A' && m.getChannel() <= 'D')
7276
_channel[m.getChannel() - 'A']++;
7377

74-
if (tag.distance > _distance) {
78+
if (tag.distance > _distance)
79+
{
7580
_distance = tag.distance;
7681
}
7782
}
7883

79-
void PromotheusCounter::Receive(const JSON::JSON* json, int len, TAG& tag) {
80-
AIS::Message& data = *((AIS::Message*)json[0].binary);
84+
void PromotheusCounter::Receive(const JSON::JSON *json, int len, TAG &tag)
85+
{
86+
AIS::Message &data = *((AIS::Message *)json[0].binary);
8187

82-
if (ppm.size() > 32768 || level.size() > 32768) {
88+
if (ppm.size() > 32768 || level.size() > 32768)
89+
{
8390
return;
8491
}
8592

@@ -90,14 +97,16 @@ void PromotheusCounter::Receive(const JSON::JSON* json, int len, TAG& tag) {
9097
m.unlock();
9198
}
9299

93-
void PromotheusCounter::Reset() {
100+
void PromotheusCounter::Reset()
101+
{
94102
m.lock();
95103
ppm = "# HELP ais_msg_ppm\n# TYPE ais_msg_ppm gauge\n";
96104
level = "# HELP ais_msg_level\n# TYPE ais_msg_level gauge\n";
97105
m.unlock();
98106
}
99107

100-
std::string PromotheusCounter::toPrometheus() {
108+
std::string PromotheusCounter::toPrometheus()
109+
{
101110
m.lock();
102111
std::string element;
103112

@@ -109,14 +118,16 @@ std::string PromotheusCounter::toPrometheus() {
109118
element += "# TYPE ais_stat_distance gauge\n";
110119
element += "ais_stat_distance " + std::to_string(_distance) + "\n";
111120

112-
for (int i = 0; i < 4; i++) {
121+
for (int i = 0; i < 4; i++)
122+
{
113123
std::string ch(1, i + 'A');
114124
element += "# HELP ais_stat_count_channel_" + ch + " Total number of messages on channel " + ch + "\n";
115125
element += "# TYPE ais_stat_count_channel_" + ch + " counter\n";
116126
element += "ais_stat_count_channel_" + ch + " " + std::to_string(_channel[i]) + "\n";
117127
}
118128

119-
for (int i = 0; i < 27; i++) {
129+
for (int i = 0; i < 27; i++)
130+
{
120131
std::string type = std::to_string(i + 1);
121132
element += "# HELP ais_stat_count_type_" + type + " Total number of messages of type " + type + "\n";
122133
element += "# TYPE ais_stat_count_type_" + type + " counter\n";

Source/Application/WebViewer.cpp

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -376,15 +376,16 @@ void WebViewer::connect(Receiver &r)
376376
for (int j = 0; j < r.Count(); j++)
377377
if (r.Output(j).canConnect(groups_in))
378378
{
379-
if (!rec_details)
380-
{
381-
auto* device = r.getDeviceManager().getDevice();
379+
if (!rec_details)
380+
{
381+
auto *device = r.getDeviceManager().getDevice();
382382

383-
sample_rate += device->getRateDescription() + "<br>";
383+
sample_rate += device->getRateDescription() + "<br>";
384384

385-
JSON::StringBuilder::stringify(device->getProduct(), product, false);
386-
JSON::StringBuilder::stringify(device->getVendor().empty() ? "-" : device->getVendor(), vendor, false);
387-
JSON::StringBuilder::stringify(device->getSerial().empty() ? "-" : device->getSerial(), serial, false); product += newline;
385+
JSON::StringBuilder::stringify(device->getProduct(), product, false);
386+
JSON::StringBuilder::stringify(device->getVendor().empty() ? "-" : device->getVendor(), vendor, false);
387+
JSON::StringBuilder::stringify(device->getSerial().empty() ? "-" : device->getSerial(), serial, false);
388+
product += newline;
388389
vendor += newline;
389390
serial += newline;
390391

@@ -514,7 +515,7 @@ void WebViewer::close()
514515
}
515516
}
516517

517-
void WebViewer::Request(TCP::ServerConnection &c, const std::string &response, bool gzip)
518+
void WebViewer::Request(IO::TCPServerConnection &c, const std::string &response, bool gzip)
518519
{
519520

520521
std::string r;
@@ -718,9 +719,17 @@ void WebViewer::Request(TCP::ServerConnection &c, const std::string &response, b
718719
std::stringstream ss(a);
719720
std::string mmsi_str;
720721
std::string content = "{";
722+
int count = 0;
723+
const int MAX_MMSI_COUNT = 100; // Limit number of MMSIs to prevent DoS
721724

722725
while (std::getline(ss, mmsi_str, ','))
723726
{
727+
if (++count > MAX_MMSI_COUNT)
728+
{
729+
Error() << "Server - path MMSI count exceeds limit: " << MAX_MMSI_COUNT;
730+
break;
731+
}
732+
724733
try
725734
{
726735
int mmsi = std::stoi(mmsi_str);
@@ -816,8 +825,8 @@ void WebViewer::Request(TCP::ServerConnection &c, const std::string &response, b
816825
{
817826
try
818827
{
819-
if (a.size() > 1024)
820-
throw std::runtime_error("Input too large");
828+
if (a.empty() || a.size() > 1024)
829+
throw std::runtime_error("Input size limit exceeded");
821830

822831
std::string result = decodeNMEAtoJSON(a, true);
823832

Source/Application/WebViewer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ class WebViewer : public IO::HTTPServer, public Setting
185185

186186
bool isPortSet() { return port_set; }
187187
// HTTP callbacks
188-
void Request(TCP::ServerConnection &c, const std::string &r, bool gzip);
188+
void Request(IO::TCPServerConnection &c, const std::string &r, bool gzip);
189189

190190
Setting &Set(std::string option, std::string arg);
191191
std::string Get() { return ""; }

Source/Device/AIRSPY.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,8 @@ namespace Device
210210
for (j = 5; j >= 0; j--)
211211
if ((r820t_hf[j] - r820t_lf[i]) <= tuner_bandwidth)
212212
break;
213-
j++;
213+
if (j < 5)
214+
j++;
214215

215216
airspy_r820t_write(dev, 10, 0xB0 | (15 - j));
216217
airspy_r820t_write(dev, 11, 0xE0 | (15 - i));

Source/Device/RTLSDR.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,11 @@ namespace Device
197197
throw std::runtime_error("RTLSDR: cannot set gain mode.");
198198

199199
int nGains = rtlsdr_get_tuner_gains(dev, nullptr);
200-
if (nGains <= 0)
201-
throw std::runtime_error("RTLSDR: no gains available");
200+
if (nGains <= 0 || nGains > 1000)
201+
throw std::runtime_error("RTLSDR: cannot get tuner gains.");
202202

203203
std::vector<int> gains(nGains);
204-
nGains = rtlsdr_get_tuner_gains(dev, gains.data());
204+
rtlsdr_get_tuner_gains(dev, gains.data());
205205

206206
int gain = gains[0];
207207

0 commit comments

Comments
 (0)