Return to Snippet

Revision: 57186
at January 22, 2013 12:44 by laurenceosx


Updated Code
#!/usr/bin/env groovy

// file: FtpSvr.groovy

/*
	Laurence Toenjes Jan 12, 2013
	
	Standalone Groovy ftp server script (no jars to download if Groovy is 
	properly installed).  
	
	The default user is guest with password of guest
	and the ftp home/base directory is the home folder of the account
	running this script.
	A root account is also created with the ftp home/base dir set to /
	and the password is root (for root to fully work you would obviously
	have to run it under an account with expanded file permissions).
	
	Optional command line args:
		arg0 defaults to port 8021.
		arg1 (ftp base dir) defaults to the home directory of the user running this script.
*/

// See http://mina.apache.org/ftpserver/ for more details.

// for OS X/*nix
//   don't forget to do: chmod +x FtpServer.groovy
//   also run as sudo ./FtpSvr.groovy for when using a port number < 1024

@Grapes([
	  @Grab(group='org.apache.ftpserver', module='ftpserver-core', version='1.0.6')
	, @Grab(group='ch.qos.logback', module='logback-classic', version='1.0.9')
])

import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.FtpServer;

import org.apache.ftpserver.listener.ListenerFactory;

import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
import org.apache.ftpserver.usermanager.SaltedPasswordEncryptor;
import org.apache.ftpserver.usermanager.impl.BaseUser;

import org.apache.ftpserver.ftplet.UserManager;
import org.apache.ftpserver.ftplet.Authority;

import org.apache.ftpserver.usermanager.impl.WritePermission;

import java.util.Properties;

public class FtpSvr {
	
	def go(String[] args) {
		String ftpUsersPropFileName = "FtpSvrUsers.properties"
		int    defaultPort = 8021

		String userHome = System.properties.'user.home';
		String homeDir = args.size()>=2 ? args[1].replace('~', userHome) : userHome ;
		assert new File(homeDir).exists();

		def gi = groovy.inspect.swingui.ObjectBrowser.&inspect; // for debug

		int port = args.size()>=1 ? args[0].toInteger() : defaultPort ;
		println "### ftp port   : $port"
		println "### ftp homeDir: $homeDir"
		println ''

		// the basics
		// FtpServerFactory serverFactory = new FtpServerFactory();
		// FtpServer server = serverFactory.createServer();
		// server.start();

		// setup users

		PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();

		def myusersPropsFile = new File( ftpUsersPropFileName );
		if ( !myusersPropsFile.exists() ) {
			myusersPropsFile.createNewFile();
		}

		userManagerFactory.setFile( myusersPropsFile );
		userManagerFactory.setPasswordEncryptor(new SaltedPasswordEncryptor());

		UserManager um = userManagerFactory.createUserManager();

		def users = []
		users << [ uid:'guest', pwd:'guest', home:homeDir ]
		users << [ uid:'root' , pwd:'root' , home:'/'     ]

		def addUser = { map ->
			BaseUser user = new BaseUser();
			user.setName( map.uid );
			user.setPassword( map.pwd );
			user.setHomeDirectory( map.home );
			
			List<Authority> auths = new ArrayList<Authority>();
			Authority auth = new WritePermission();
			auths.add(auth);
			user.setAuthorities(auths);
			// gi user;
			// gi user.getAuthorities();
			um.save(user);
		}

		users.each { itUserMap -> addUser( itUserMap ) }

		FtpServerFactory serverFactory = new FtpServerFactory();

		serverFactory.setUserManager( um );

		ListenerFactory factory = new ListenerFactory();
				
		// set the port of the listener
		factory.setPort( port );

		// replace the default listener
		serverFactory.addListener("default", factory.createListener());

		// start the server
		FtpServer server = serverFactory.createServer(); 

		// groovy.inspect.swingui.ObjectBrowser.inspect( server );
		// groovy.inspect.swingui.ObjectBrowser.inspect( server.userManager );
		// gi server.userManager.getAllUserNames() as List;
			
		server.start();
	}
	
	public static void main(String[] args) {
		def mo = new FtpSvr();
		mo.go(args)
	}
}

Revision: 57185
at January 22, 2013 12:33 by laurenceosx


Updated Code
#!/usr/bin/env groovy

// file: FtpSvr.groovy

/*
	Laurence Toenjes Jan 12, 2013
	
	Standalone Groovy ftp server script (no jars to download if Groovy is 
	properly installed).  
	
	The default user is guest with password of guest
	and the ftp home/base directory is the home folder of the account
	running this script.
	A root account is also created with the ftp home/base dir set to /
	and the password is root (for root to fully work you would obviously
	have to run it under an account with expanded file permissions).
	
	Optional command line args:
		arg0 defaults to port 8021.
		arg1 (ftp base dir) defaults to the home directory of the user running this script.
*/

// See http://mina.apache.org/ftpserver/ for more details.

// for OS X/*nix
//   don't forget to do: chmod +x FtpServer.groovy
//   also run as sudo ./FtpSvr.groovy for when using a port number < 1024

@Grapes([
	  @Grab(group='org.apache.ftpserver', module='ftpserver-core', version='1.0.6')
	, @Grab(group='ch.qos.logback', module='logback-classic', version='1.0.9')
])

import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.FtpServer;

import org.apache.ftpserver.listener.ListenerFactory;

import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
import org.apache.ftpserver.usermanager.SaltedPasswordEncryptor;
import org.apache.ftpserver.usermanager.impl.BaseUser;

import org.apache.ftpserver.ftplet.UserManager;
import org.apache.ftpserver.ftplet.Authority;

import org.apache.ftpserver.usermanager.impl.WritePermission;

import java.util.Properties;

String ftpUsersPropFileName = "FtpSvrUsers.properties"
int    defaultPort = 8021

String userHome = System.properties.'user.home';
String homeDir = args.size()>=2 ? args[1].replace('~', userHome) : userHome ;
assert new File(homeDir).exists();

def gi = groovy.inspect.swingui.ObjectBrowser.&inspect; // for debug

int port = args.size()>=1 ? args[0].toInteger() : defaultPort ;
println "### ftp port   : $port"
println "### ftp homeDir: $homeDir"
println ''

// the basics
// FtpServerFactory serverFactory = new FtpServerFactory();
// FtpServer server = serverFactory.createServer();
// server.start();

// setup users

PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();

def myusersPropsFile = new File( ftpUsersPropFileName );
if ( !myusersPropsFile.exists() ) {
    myusersPropsFile.createNewFile();
}

userManagerFactory.setFile( myusersPropsFile );
userManagerFactory.setPasswordEncryptor(new SaltedPasswordEncryptor());

UserManager um = userManagerFactory.createUserManager();

def users = []
users << [ uid:'guest', pwd:'guest', home:homeDir ]
users << [ uid:'root' , pwd:'root' , home:'/'     ]

def addUser = { map ->
	BaseUser user = new BaseUser();
	user.setName( map.uid );
	user.setPassword( map.pwd );
	user.setHomeDirectory( map.home );
	
	List<Authority> auths = new ArrayList<Authority>();
	Authority auth = new WritePermission();
	auths.add(auth);
	user.setAuthorities(auths);
	// gi user;
	// gi user.getAuthorities();
	um.save(user);
}

users.each { itUserMap -> addUser( itUserMap ) }

FtpServerFactory serverFactory = new FtpServerFactory();

serverFactory.setUserManager( um );

ListenerFactory factory = new ListenerFactory();
        
// set the port of the listener
factory.setPort( port );

// replace the default listener
serverFactory.addListener("default", factory.createListener());

// start the server
FtpServer server = serverFactory.createServer(); 

// groovy.inspect.swingui.ObjectBrowser.inspect( server );
// groovy.inspect.swingui.ObjectBrowser.inspect( server.userManager );
// gi server.userManager.getAllUserNames() as List;
    
server.start();

Revision: 57184
at January 16, 2013 16:52 by laurenceosx


Updated Code
#!/usr/bin/env groovy

// file: FtpSvr.groovy

/*
	Laurence Toenjes Jan 12, 2013
	
	Standalone Groovy ftp server script (no jars to download if Groovy is 
	properly installed).  
	
	The default user is guest with password of guest
	and the ftp home/base directory is the home folder of the account
	running this script.
	A root account is also created with the ftp home/base dir set to /
	and the password is root (for root to fully work you would obviously
	have to run it under an account with expanded file permissions).
	
	Optional command line args:
		arg0 defaults to port 8021.
		arg1 (ftp base dir) defaults to the home directory of the user running this script.
*/

// See http://mina.apache.org/ftpserver/ for more details.

// for OS X/*nix
//   don't forget to do: chmod +x FtpServer.groovy
//   also run as sudo ./FtpSvr.groovy for when using a port number < 1024

@Grapes([
	  @Grab(group='org.apache.ftpserver', module='ftpserver-core', version='1.0.6')
	, @Grab(group='ch.qos.logback', module='logback-classic', version='1.0.9')
])

import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.FtpServer;

import org.apache.ftpserver.listener.ListenerFactory;

import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
import org.apache.ftpserver.usermanager.SaltedPasswordEncryptor;
import org.apache.ftpserver.usermanager.impl.BaseUser;

import org.apache.ftpserver.ftplet.UserManager;
import org.apache.ftpserver.ftplet.Authority;

import org.apache.ftpserver.usermanager.impl.WritePermission;

import java.util.Properties;

String ftpUsersPropFileName = "FtpSvrUsers.properties"
int    defaultPort = 8021

String userHome = System.properties.'user.home';
String homeDir = args.size()>=2 ? args[1].replace('~', userHome) : userHome ;
assert new File(homeDir).exists();

def gi = groovy.inspect.swingui.ObjectBrowser.&inspect; // for debug

int port = args.size()>=1 ? args[0].toInteger() : defaultPort ;
println "### ftp port   : $port"
println "### ftp homeDir: $homeDir"
println ''

// the basics
// FtpServerFactory serverFactory = new FtpServerFactory();
// FtpServer server = serverFactory.createServer();
// server.start();

// setup guest user

PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();

def myusersPropsFile = new File( ftpUsersPropFileName );
if ( !myusersPropsFile.exists() ) {
    myusersPropsFile.createNewFile();
}

userManagerFactory.setFile( myusersPropsFile );
userManagerFactory.setPasswordEncryptor(new SaltedPasswordEncryptor());

UserManager um = userManagerFactory.createUserManager();

BaseUser user = new BaseUser();
user.setName("guest");
user.setPassword("guest");
// user.setHomeDirectory("/");
user.setHomeDirectory( homeDir );

user.setName("root");
user.setPassword("root");
user.setHomeDirectory( '/' );

// add write permission
List<Authority> auths = new ArrayList<Authority>();
Authority auth = new WritePermission();
auths.add(auth);
user.setAuthorities(auths);

//gi user;
//gi user.getAuthorities();

um.save(user);

//
/*
def propsObj = new Properties();
propsObj.load( new FileInputStream( myusersPropsFile ) );
println ''
println "### ftpserver.user.guest.homedirectory: ${propsObj['ftpserver.user.guest.homedirectory']}"
println ''
*/
//

FtpServerFactory serverFactory = new FtpServerFactory();

serverFactory.setUserManager( um );
  
ListenerFactory factory = new ListenerFactory();
        
// set the port of the listener
// factory.setPort(2221);

factory.setPort( port );

// replace the default listener
serverFactory.addListener("default", factory.createListener());
                
// start the server
FtpServer server = serverFactory.createServer(); 

// groovy.inspect.swingui.ObjectBrowser.inspect( server );
// groovy.inspect.swingui.ObjectBrowser.inspect( server.userManager );

// gi server.userManager.getAllUserNames() as List;
    
server.start();

Revision: 57183
at January 13, 2013 02:32 by laurenceosx


Updated Code
#!/usr/bin/env groovy

// file: FtpSvr.groovy

/*
	Laurence Toenjes Jan 12, 2013
	
	Standalone Groovy ftp server script (no jars to downnload if Groovy is 
	properly installed).  
	
	The default user is guest with password of guest
	and the ftp home/base directory is the home folder of the account
	running this script.
	A root account is also created with the ftp home/base dir set to /
	and the password is root (for root to fully work you would obviously
	have to run it under an account with expanded file permissions).
	
	Optional command line args:
		arg0 defaults to port 8021.
		arg1 (ftp base dir) defaults to the home directory of the user running this script.
*/

// See http://mina.apache.org/ftpserver/ for more details.

// for OS X/*nix
//   don't forget to do: chmod +x FtpServer.groovy
//   also run as sudo ./FtpSvr.groovy for when using a port number < 1024

@Grapes([
	  @Grab(group='org.apache.ftpserver', module='ftpserver-core', version='1.0.6')
	, @Grab(group='ch.qos.logback', module='logback-classic', version='1.0.9')
])

import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.FtpServer;

import org.apache.ftpserver.listener.ListenerFactory;

import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
import org.apache.ftpserver.usermanager.SaltedPasswordEncryptor;
import org.apache.ftpserver.usermanager.impl.BaseUser;

import org.apache.ftpserver.ftplet.UserManager;
import org.apache.ftpserver.ftplet.Authority;

import org.apache.ftpserver.usermanager.impl.WritePermission;

import java.util.Properties;

String ftpUsersPropFileName = "FtpSvrUsers.properties"
int    defaultPort = 8021

String userHome = System.properties.'user.home';
String homeDir = args.size()>=2 ? args[1].replace('~', userHome) : userHome ;
assert new File(homeDir).exists();

def gi = groovy.inspect.swingui.ObjectBrowser.&inspect; // for debug

int port = args.size()>=1 ? args[0].toInteger() : defaultPort ;
println "### ftp port   : $port"
println "### ftp homeDir: $homeDir"
println ''

// the basics
// FtpServerFactory serverFactory = new FtpServerFactory();
// FtpServer server = serverFactory.createServer();
// server.start();

// setup guest user

PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();

def myusersPropsFile = new File( ftpUsersPropFileName );
if ( !myusersPropsFile.exists() ) {
    myusersPropsFile.createNewFile();
}

userManagerFactory.setFile( myusersPropsFile );
userManagerFactory.setPasswordEncryptor(new SaltedPasswordEncryptor());

UserManager um = userManagerFactory.createUserManager();

BaseUser user = new BaseUser();
user.setName("guest");
user.setPassword("guest");
// user.setHomeDirectory("/");
user.setHomeDirectory( homeDir );

user.setName("root");
user.setPassword("root");
user.setHomeDirectory( '/' );

// add write permission
List<Authority> auths = new ArrayList<Authority>();
Authority auth = new WritePermission();
auths.add(auth);
user.setAuthorities(auths);

//gi user;
//gi user.getAuthorities();

um.save(user);

//
/*
def propsObj = new Properties();
propsObj.load( new FileInputStream( myusersPropsFile ) );
println ''
println "### ftpserver.user.guest.homedirectory: ${propsObj['ftpserver.user.guest.homedirectory']}"
println ''
*/
//

FtpServerFactory serverFactory = new FtpServerFactory();

serverFactory.setUserManager( um );
  
ListenerFactory factory = new ListenerFactory();
        
// set the port of the listener
// factory.setPort(2221);

factory.setPort( port );

// replace the default listener
serverFactory.addListener("default", factory.createListener());
                
// start the server
FtpServer server = serverFactory.createServer(); 

// groovy.inspect.swingui.ObjectBrowser.inspect( server );
// groovy.inspect.swingui.ObjectBrowser.inspect( server.userManager );

// gi server.userManager.getAllUserNames() as List;
    
server.start();

Revision: 57182
at January 13, 2013 02:28 by laurenceosx


Updated Code
#!/usr/bin/env groovy

// file: FtpSvr.groovy

/*
	Laurence Toenjes Jan 12, 2013
	
	Standalone Groovy ftp server script (no jars to downnload if Groovy is 
	properly installed).  
	
	The default user is guest with password of guest
	and the ftp home/base directory is the home folder of the account
	running this script.
	A root account is also created with the ftp home/base dir set to /
	and the password is root (for root to fully work you would obviously
	have to run it under an account with expanded file permissions).
	
	Command line args:
		arg0 is the port # defaults to 8021
		arg1 it the home/base directory.
*/

// See http://mina.apache.org/ftpserver/ for more details.

// for OS X/*nix
//   don't forget to do: chmod +x FtpServer.groovy
//   also run as sudo ./FtpSvr.groovy for when using a port number < 1024

@Grapes([
	  @Grab(group='org.apache.ftpserver', module='ftpserver-core', version='1.0.6')
	, @Grab(group='ch.qos.logback', module='logback-classic', version='1.0.9')
])

import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.FtpServer;

import org.apache.ftpserver.listener.ListenerFactory;

import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
import org.apache.ftpserver.usermanager.SaltedPasswordEncryptor;
import org.apache.ftpserver.usermanager.impl.BaseUser;

import org.apache.ftpserver.ftplet.UserManager;
import org.apache.ftpserver.ftplet.Authority;

import org.apache.ftpserver.usermanager.impl.WritePermission;

import java.util.Properties;

String ftpUsersPropFileName = "FtpSvrUsers.properties"
int    defaultPort = 8021

String userHome = System.properties.'user.home';
String homeDir = args.size()>=2 ? args[1].replace('~', userHome) : userHome ;
assert new File(homeDir).exists();

def gi = groovy.inspect.swingui.ObjectBrowser.&inspect; // for debug

int port = args.size()>=1 ? args[0].toInteger() : defaultPort ;
println "### ftp port   : $port"
println "### ftp homeDir: $homeDir"
println ''

// the basics
// FtpServerFactory serverFactory = new FtpServerFactory();
// FtpServer server = serverFactory.createServer();
// server.start();

// setup guest user

PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();

def myusersPropsFile = new File( ftpUsersPropFileName );
if ( !myusersPropsFile.exists() ) {
    myusersPropsFile.createNewFile();
}

userManagerFactory.setFile( myusersPropsFile );
userManagerFactory.setPasswordEncryptor(new SaltedPasswordEncryptor());

UserManager um = userManagerFactory.createUserManager();

BaseUser user = new BaseUser();
user.setName("guest");
user.setPassword("guest");
// user.setHomeDirectory("/");
user.setHomeDirectory( homeDir );

user.setName("root");
user.setPassword("root");
user.setHomeDirectory( '/' );

// add write permission
List<Authority> auths = new ArrayList<Authority>();
Authority auth = new WritePermission();
auths.add(auth);
user.setAuthorities(auths);

//gi user;
//gi user.getAuthorities();

um.save(user);

//
/*
def propsObj = new Properties();
propsObj.load( new FileInputStream( myusersPropsFile ) );
println ''
println "### ftpserver.user.guest.homedirectory: ${propsObj['ftpserver.user.guest.homedirectory']}"
println ''
*/
//

FtpServerFactory serverFactory = new FtpServerFactory();

serverFactory.setUserManager( um );
  
ListenerFactory factory = new ListenerFactory();
        
// set the port of the listener
// factory.setPort(2221);

factory.setPort( port );

// replace the default listener
serverFactory.addListener("default", factory.createListener());
                
// start the server
FtpServer server = serverFactory.createServer(); 

// groovy.inspect.swingui.ObjectBrowser.inspect( server );
// groovy.inspect.swingui.ObjectBrowser.inspect( server.userManager );

// gi server.userManager.getAllUserNames() as List;
    
server.start();

Revision: 57181
at May 19, 2012 04:41 by laurenceosx


Initial Code
// file: r.groovy
// Laurence Toenjes - 2012-05-18 14:39:41
// embedded apache ftp server on port 21 example
// with guest user account with password of guest
// that has write permissions
// and listens to root file system

// http://mina.apache.org/ftpserver/

/*

@rem put this batch file named r.bat in the apache ftp server base folder
@pushd "%~dp0"
SET CLASSPATH=.;.\common\classes;.\common\lib\*;%GROOVY_HOME%\embeddable\*;
start java groovy.ui.Console %~n0.groovy
@popd

*/

import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.FtpServer;

import org.apache.ftpserver.listener.ListenerFactory;

import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
import org.apache.ftpserver.usermanager.SaltedPasswordEncryptor;
import org.apache.ftpserver.usermanager.impl.BaseUser;

import org.apache.ftpserver.ftplet.UserManager;
import org.apache.ftpserver.ftplet.Authority;

import org.apache.ftpserver.usermanager.impl.WritePermission;

def gi = groovy.inspect.swingui.ObjectBrowser.&inspect; // for debug

// the basics
// FtpServerFactory serverFactory = new FtpServerFactory();
// FtpServer server = serverFactory.createServer();
// server.start();

// setup guest user

PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();

def myusersPropsFile = new File("myusers.properties");
if ( !myusersPropsFile.exists() ) {
    myusersPropsFile.createNewFile();
}

userManagerFactory.setFile( myusersPropsFile );
userManagerFactory.setPasswordEncryptor(new SaltedPasswordEncryptor());

UserManager um = userManagerFactory.createUserManager();

BaseUser user = new BaseUser();
user.setName("guest");
user.setPassword("guest");
user.setHomeDirectory("/");

// add write permission
List<Authority> auths = new ArrayList<Authority>();
Authority auth = new WritePermission();
auths.add(auth);
user.setAuthorities(auths);

//gi user;
//gi user.getAuthorities();

um.save(user);

FtpServerFactory serverFactory = new FtpServerFactory();

serverFactory.setUserManager( um );
  
ListenerFactory factory = new ListenerFactory();
        
// set the port of the listener
// factory.setPort(2221);
factory.setPort(21);

// replace the default listener
serverFactory.addListener("default", factory.createListener());
                
// start the server
FtpServer server = serverFactory.createServer(); 

// groovy.inspect.swingui.ObjectBrowser.inspect( server );
// groovy.inspect.swingui.ObjectBrowser.inspect( server.userManager );

// gi server.userManager.getAllUserNames() as List;
    
server.start();

Initial URL
http://mina.apache.org/ftpserver/

Initial Description
http://mina.apache.org/ftpserver/

Initial Title
Groovy Embedded Apache FTP Server Example

Initial Tags
groovy

Initial Language
Groovy