use of org.apache.ftpserver.usermanager.impl.BaseUser in project AmazeFileManager by TeamAmaze.
the class FTPService method run.
@Override
public void run() {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
FtpServerFactory serverFactory = new FtpServerFactory();
ConnectionConfigFactory connectionConfigFactory = new ConnectionConfigFactory();
connectionConfigFactory.setAnonymousLoginEnabled(true);
serverFactory.setConnectionConfig(connectionConfigFactory.createConnectionConfig());
String usernamePreference = preferences.getString(KEY_PREFERENCE_USERNAME, DEFAULT_USERNAME);
if (!usernamePreference.equals(DEFAULT_USERNAME)) {
username = usernamePreference;
try {
password = CryptUtil.decryptPassword(getApplicationContext(), preferences.getString(KEY_PREFERENCE_PASSWORD, ""));
isPasswordProtected = true;
} catch (GeneralSecurityException | IOException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), getResources().getString(R.string.error), Toast.LENGTH_SHORT).show();
// can't decrypt the password saved in preferences, remove the preference altogether
// and start an anonymous connection instead
preferences.edit().putString(FTPService.KEY_PREFERENCE_PASSWORD, "").apply();
isPasswordProtected = false;
}
}
BaseUser user = new BaseUser();
if (!isPasswordProtected) {
user.setName("anonymous");
} else {
user.setName(username);
user.setPassword(password);
}
user.setHomeDirectory(preferences.getString(KEY_PREFERENCE_PATH, DEFAULT_PATH));
List<Authority> list = new ArrayList<>();
list.add(new WritePermission());
user.setAuthorities(list);
try {
serverFactory.getUserManager().save(user);
} catch (FtpException e) {
e.printStackTrace();
}
ListenerFactory fac = new ListenerFactory();
if (preferences.getBoolean(KEY_PREFERENCE_SECURE, DEFAULT_SECURE)) {
SslConfigurationFactory sslConfigurationFactory = new SslConfigurationFactory();
File file;
try {
InputStream stream = getResources().openRawResource(R.raw.key);
file = File.createTempFile("keystore.bks", "");
FileOutputStream outputStream = new FileOutputStream(file);
IOUtils.copy(stream, outputStream);
} catch (Exception e) {
e.printStackTrace();
file = null;
}
if (file != null) {
sslConfigurationFactory.setKeystoreFile(file);
sslConfigurationFactory.setKeystorePassword("vishal007");
fac.setSslConfiguration(sslConfigurationFactory.createSslConfiguration());
fac.setImplicitSsl(true);
} else {
// no keystore found
preferences.edit().putBoolean(KEY_PREFERENCE_SECURE, false).apply();
}
}
fac.setPort(getPort(preferences));
fac.setIdleTimeout(preferences.getInt(KEY_PREFERENCE_TIMEOUT, DEFAULT_TIMEOUT));
serverFactory.addListener("default", fac.createListener());
try {
server = serverFactory.createServer();
server.start();
sendBroadcast(new Intent(FTPService.ACTION_STARTED).putExtra(TAG_STARTED_BY_TILE, isStartedByTile));
} catch (Exception e) {
sendBroadcast(new Intent(FTPService.ACTION_FAILEDTOSTART));
}
}
use of org.apache.ftpserver.usermanager.impl.BaseUser in project AnExplorer by 1hakr.
the class ConnectionsService method launchServer.
@Override
public boolean launchServer() {
ListenerFactory listenerFactory = new ListenerFactory();
listenerFactory.setPort(ConnectionUtils.getAvailablePortForFTP());
FtpServerFactory serverFactory = new FtpServerFactory();
serverFactory.addListener("default", listenerFactory.createListener());
ConnectionConfigFactory connectionConfigFactory = new ConnectionConfigFactory();
connectionConfigFactory.setAnonymousLoginEnabled(getNetworkConnection().isAnonymousLogin());
connectionConfigFactory.setMaxLoginFailures(5);
connectionConfigFactory.setLoginFailureDelay(2000);
serverFactory.setConnectionConfig(connectionConfigFactory.createConnectionConfig());
BaseUser user = new BaseUser();
user.setName(getNetworkConnection().getUserName());
user.setPassword(getNetworkConnection().getPassword());
user.setHomeDirectory(getNetworkConnection().getPath());
List<Authority> list = new ArrayList<>();
list.add(new WritePermission());
list.add(new TransferRatePermission(0, 0));
list.add(new ConcurrentLoginPermission(10, 10));
user.setAuthorities(list);
try {
serverFactory.getUserManager().save(user);
} catch (FtpException e) {
CrashReportingManager.logException(e);
}
// do start server
try {
ftpServer = serverFactory.createServer();
ftpServer.start();
return true;
} catch (Exception e) {
ftpServer = null;
handleServerStartError(e);
}
return false;
}
use of org.apache.ftpserver.usermanager.impl.BaseUser in project hutool by looly.
the class SimpleFtpServer method addAnonymous.
/**
* 添加匿名用户
*
* @param homePath 用户路径,匿名用户对此路径有读写权限
* @return this
*/
public SimpleFtpServer addAnonymous(String homePath) {
BaseUser user = new BaseUser();
user.setName("anonymous");
user.setHomeDirectory(homePath);
List<Authority> authorities = new ArrayList<>();
// 添加用户读写权限
authorities.add(new WritePermission());
user.setAuthorities(authorities);
return addUser(user);
}
Aggregations