aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/forward
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2023-04-10 19:40:22 -0700
committerAnton Tananaev <anton@traccar.org>2023-04-10 19:40:22 -0700
commitdd8c46eec3282e344eeea5e27d084448c85e1518 (patch)
tree6e82515d28493ec506d250c282b07bbf96d16f34 /src/main/java/org/traccar/forward
parentee39f67d21078da595a819255b59d88c30e66d7a (diff)
downloadtrackermap-server-dd8c46eec3282e344eeea5e27d084448c85e1518.tar.gz
trackermap-server-dd8c46eec3282e344eeea5e27d084448c85e1518.tar.bz2
trackermap-server-dd8c46eec3282e344eeea5e27d084448c85e1518.zip
Add raw data forwarding
Diffstat (limited to 'src/main/java/org/traccar/forward')
-rw-r--r--src/main/java/org/traccar/forward/NetworkForwarder.java66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/main/java/org/traccar/forward/NetworkForwarder.java b/src/main/java/org/traccar/forward/NetworkForwarder.java
new file mode 100644
index 000000000..6abf2b7ba
--- /dev/null
+++ b/src/main/java/org/traccar/forward/NetworkForwarder.java
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2023 Anton Tananaev (anton@traccar.org)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.traccar.forward;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.traccar.config.Config;
+import org.traccar.config.Keys;
+
+import javax.inject.Inject;
+import javax.inject.Singleton;
+import java.io.IOException;
+import java.net.DatagramPacket;
+import java.net.DatagramSocket;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.Socket;
+import java.util.HashMap;
+import java.util.Map;
+
+@Singleton
+public class NetworkForwarder {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(NetworkForwarder.class);
+
+ private final InetAddress destination;
+ private final DatagramSocket connectionUdp;
+ private final Map<InetSocketAddress, Socket> connectionsTcp = new HashMap<>();
+
+ @Inject
+ public NetworkForwarder(Config config) throws IOException {
+ destination = InetAddress.getByName(config.getString(Keys.SERVER_FORWARD));
+ connectionUdp = new DatagramSocket();
+ }
+
+ public void forward(InetSocketAddress source, int port, boolean datagram, byte[] data) {
+ try {
+ if (datagram) {
+ connectionUdp.send(new DatagramPacket(data, data.length, destination, port));
+ } else {
+ Socket connectionTcp = connectionsTcp.get(source);
+ if (connectionTcp == null || connectionTcp.isClosed()) {
+ connectionTcp = new Socket(destination, port);
+ connectionsTcp.put(source, connectionTcp);
+ }
+ connectionTcp.getOutputStream().write(data);
+ }
+ } catch (IOException e) {
+ LOGGER.warn("Network forwarding error", e);
+ }
+ }
+
+}