]> git.basschouten.com Git - openhab-addons.git/commitdiff
[ftpupload] Add configurable passive port (#10127)
authorChristopher Schirner <christopher+github@schirner.org>
Fri, 26 Feb 2021 18:55:19 +0000 (19:55 +0100)
committerGitHub <noreply@github.com>
Fri, 26 Feb 2021 18:55:19 +0000 (10:55 -0800)
* Add configurable passive port
* Don't use apache library; Use standard Java
* Ran spotless:apply
* Update readme

Signed-off-by: schinken <schinken@bamberg.ccc.de>
Co-authored-by: Hilbrand Bouwkamp <hilbrand@h72.nl>
bundles/org.openhab.binding.ftpupload/README.md
bundles/org.openhab.binding.ftpupload/src/main/java/org/openhab/binding/ftpupload/internal/FtpUploadHandlerFactory.java
bundles/org.openhab.binding.ftpupload/src/main/java/org/openhab/binding/ftpupload/internal/ftp/FtpServer.java
bundles/org.openhab.binding.ftpupload/src/main/resources/OH-INF/binding/binding.xml

index 5d102e058881823efdaf6202987d0ed3a55996c8..8ee0f6ac7baaa0b8b69d9c34288a692f65682c8d 100644 (file)
@@ -18,10 +18,11 @@ Automatic discovery is not supported.
 
 The binding has the following configuration options:
 
-| Parameter   | Name         | Description                                                                                                            | Required | Default value |
-|-------------|--------------|------------------------------------------------------------------------------------------------------------------------|----------|---------------|
-| port        | TCP Port     | TCP port of the FTP server                                                                                             | no       | 2121          |
-| idleTimeout | Idle timeout | The number of seconds before an inactive client is disconnected. If this value is set to 0, the idle time is disabled. | no       | 60            |
+| Parameter    | Name          | Description                                                                                                                                                                                                                                                                                                               | Required | Default value |
+|--------------|---------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------|---------------|
+| port         | TCP Port      | TCP port of the FTP server                                                                                                                                                                                                                                                                                                | no       | 2121          |
+| idleTimeout  | Idle timeout  | The number of seconds before an inactive client is disconnected. If this value is set to 0, the idle time is disabled.                                                                                                                                                                                                    | no       | 60            |
+| passivePorts | Passive Ports | A string of passive ports, can contain a single port (as an integer), multiple ports seperated by commas (e.g. 123,124,125) or ranges of ports, including open ended ranges (e.g. 123-125, 30000-, -1023). Combinations for single ports and ranges is also supported. Empty (default) allows all ports as passive ports. | no       |               |
 
 ## Thing Configuration
 
index b888e570a9053bd614da288c3b6caa15b0acfe6d..8990d0ec761316655a1fe9c3668af038e577d4f7 100644 (file)
@@ -19,6 +19,7 @@ import java.util.Dictionary;
 import java.util.Set;
 
 import org.apache.commons.lang.StringUtils;
+import org.apache.ftpserver.DataConnectionConfigurationFactory;
 import org.apache.ftpserver.FtpServerConfigurationException;
 import org.apache.ftpserver.ftplet.FtpException;
 import org.openhab.binding.ftpupload.internal.ftp.FtpServer;
@@ -90,6 +91,7 @@ public class FtpUploadHandlerFactory extends BaseThingHandlerFactory {
     protected synchronized void modified(ComponentContext componentContext) {
         stopFtpServer();
         Dictionary<String, Object> properties = componentContext.getProperties();
+        DataConnectionConfigurationFactory dataConnectionConfigurationFactory = new DataConnectionConfigurationFactory();
 
         int port = DEFAULT_PORT;
         int idleTimeout = DEFAULT_IDLE_TIMEOUT;
@@ -116,9 +118,21 @@ public class FtpUploadHandlerFactory extends BaseThingHandlerFactory {
             }
         }
 
+        if (properties.get("passivePorts") != null) {
+            String strPassivePorts = properties.get("passivePorts").toString();
+            if (!strPassivePorts.isEmpty()) {
+                try {
+                    dataConnectionConfigurationFactory.setPassivePorts(strPassivePorts);
+                } catch (IllegalArgumentException e) {
+                    logger.warn("Invalid passive ports '{}' ({})", strPassivePorts, e.getMessage());
+                }
+            }
+        }
+
         try {
             logger.debug("Starting FTP server, port={}, idleTimeout={}", port, idleTimeout);
-            ftpServer.startServer(port, idleTimeout);
+            ftpServer.startServer(port, idleTimeout,
+                    dataConnectionConfigurationFactory.createDataConnectionConfiguration());
         } catch (FtpException | FtpServerConfigurationException e) {
             logger.warn("FTP server starting failed, reason: {}", e.getMessage());
         }
index df1444f85a912786c3561870ed7d7747f8dbedde..0cec11fdad1a74eef91f7f4db3e16951451adf1f 100644 (file)
@@ -19,6 +19,7 @@ import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 
+import org.apache.ftpserver.DataConnectionConfiguration;
 import org.apache.ftpserver.FtpServerConfigurationException;
 import org.apache.ftpserver.FtpServerFactory;
 import org.apache.ftpserver.ftplet.DefaultFtplet;
@@ -48,6 +49,7 @@ public class FtpServer {
     private final Logger logger = LoggerFactory.getLogger(FtpServer.class);
 
     private int port;
+    private DataConnectionConfiguration dataConnectionConfiguration;
     int idleTimeout;
 
     private org.apache.ftpserver.FtpServer server;
@@ -61,10 +63,12 @@ public class FtpServer {
         FTPUserManager = new FTPUserManager();
     }
 
-    public void startServer(int port, int idleTimeout) throws FtpException {
+    public void startServer(int port, int idleTimeout, DataConnectionConfiguration dataConnectionConfiguration)
+            throws FtpException {
         stopServer();
         this.port = port;
         this.idleTimeout = idleTimeout;
+        this.dataConnectionConfiguration = dataConnectionConfiguration;
         FTPUserManager.setIdleTimeout(idleTimeout);
         initServer();
     }
@@ -127,8 +131,10 @@ public class FtpServer {
     private void initServer() throws FtpException {
         FtpServerFactory serverFactory = new FtpServerFactory();
         ListenerFactory listenerFactory = new ListenerFactory();
+
         listenerFactory.setPort(port);
         listenerFactory.setIdleTimeout(idleTimeout);
+        listenerFactory.setDataConnectionConfiguration(dataConnectionConfiguration);
 
         Listener listener = listenerFactory.createListener();
 
index 891acf55fc0b4bc5e74170dfe907bb6b4a8d58a5..f76a75aabaa599fde5cb925449c2969c0c4b88be 100644 (file)
                                time is disabled.</description>
                        <default>60</default>
                </parameter>
+               <parameter name="passivePorts" type="text">
+                       <label>Passive Port Range</label>
+                       <description>A string of passive ports, can contain a single port (as an integer), multiple ports seperated by
+                               commas
+                               (e.g. 123,124,125) or ranges of ports, including open ended ranges (e.g. 123-125, 30000-, -1023).
+                               Combinations for
+                               single ports and ranges is also supported. Empty (default) allows all ports as passive ports.</description>
+                       <default></default>
+                       <advanced>true</advanced>
+               </parameter>
        </config-description>
 </binding:binding>