aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/TrackerServer.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/traccar/TrackerServer.java')
-rw-r--r--src/main/java/org/traccar/TrackerServer.java57
1 files changed, 38 insertions, 19 deletions
diff --git a/src/main/java/org/traccar/TrackerServer.java b/src/main/java/org/traccar/TrackerServer.java
index 59ba123e2..8e2fce616 100644
--- a/src/main/java/org/traccar/TrackerServer.java
+++ b/src/main/java/org/traccar/TrackerServer.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2012 - 2018 Anton Tananaev (anton@traccar.org)
+ * Copyright 2012 - 2022 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.
@@ -23,28 +23,58 @@ import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.channel.socket.nio.NioDatagramChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
+import io.netty.handler.ssl.SslHandler;
import io.netty.util.concurrent.GlobalEventExecutor;
import org.traccar.config.Keys;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLEngine;
import java.net.InetSocketAddress;
-public abstract class TrackerServer {
+public abstract class TrackerServer implements TrackerConnector {
private final boolean datagram;
+ private final boolean secure;
+
+ @SuppressWarnings("rawtypes")
private final AbstractBootstrap bootstrap;
+ private final int port;
+ private final String address;
+
+ private final ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
+
+ @Override
public boolean isDatagram() {
return datagram;
}
+ @Override
+ public boolean isSecure() {
+ return secure;
+ }
+
public TrackerServer(boolean datagram, String protocol) {
this.datagram = datagram;
+ secure = Context.getConfig().getBoolean(Keys.PROTOCOL_SSL.withPrefix(protocol));
address = Context.getConfig().getString(Keys.PROTOCOL_ADDRESS.withPrefix(protocol));
port = Context.getConfig().getInteger(Keys.PROTOCOL_PORT.withPrefix(protocol));
BasePipelineFactory pipelineFactory = new BasePipelineFactory(this, protocol) {
@Override
+ protected void addTransportHandlers(PipelineBuilder pipeline) {
+ try {
+ if (isSecure()) {
+ SSLEngine engine = SSLContext.getDefault().createSSLEngine();
+ pipeline.addLast(new SslHandler(engine));
+ }
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Override
protected void addProtocolHandlers(PipelineBuilder pipeline) {
TrackerServer.this.addProtocolHandlers(pipeline);
}
@@ -52,14 +82,14 @@ public abstract class TrackerServer {
if (datagram) {
- this.bootstrap = new Bootstrap()
+ bootstrap = new Bootstrap()
.group(EventLoopGroupFactory.getWorkerGroup())
.channel(NioDatagramChannel.class)
.handler(pipelineFactory);
} else {
- this.bootstrap = new ServerBootstrap()
+ bootstrap = new ServerBootstrap()
.group(EventLoopGroupFactory.getBossGroup(), EventLoopGroupFactory.getWorkerGroup())
.channel(NioServerSocketChannel.class)
.childHandler(pipelineFactory);
@@ -69,32 +99,20 @@ public abstract class TrackerServer {
protected abstract void addProtocolHandlers(PipelineBuilder pipeline);
- private int port;
-
public int getPort() {
return port;
}
- public void setPort(int port) {
- this.port = port;
- }
-
- private String address;
-
public String getAddress() {
return address;
}
- public void setAddress(String address) {
- this.address = address;
- }
-
- private final ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
-
+ @Override
public ChannelGroup getChannelGroup() {
return channelGroup;
}
+ @Override
public void start() throws Exception {
InetSocketAddress endpoint;
if (address == null) {
@@ -103,12 +121,13 @@ public abstract class TrackerServer {
endpoint = new InetSocketAddress(address, port);
}
- Channel channel = bootstrap.bind(endpoint).sync().channel();
+ Channel channel = bootstrap.bind(endpoint).syncUninterruptibly().channel();
if (channel != null) {
getChannelGroup().add(channel);
}
}
+ @Override
public void stop() {
channelGroup.close().awaitUninterruptibly();
}