Search in sources :

Example 6 with BaseUser

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));
    }
}
Also used : BaseUser(org.apache.ftpserver.usermanager.impl.BaseUser) SharedPreferences(android.content.SharedPreferences) Authority(org.apache.ftpserver.ftplet.Authority) FtpServerFactory(org.apache.ftpserver.FtpServerFactory) InputStream(java.io.InputStream) GeneralSecurityException(java.security.GeneralSecurityException) ArrayList(java.util.ArrayList) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) ConnectionConfigFactory(org.apache.ftpserver.ConnectionConfigFactory) IOException(java.io.IOException) WritePermission(org.apache.ftpserver.usermanager.impl.WritePermission) SocketException(java.net.SocketException) GeneralSecurityException(java.security.GeneralSecurityException) FtpException(org.apache.ftpserver.ftplet.FtpException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) FileOutputStream(java.io.FileOutputStream) FtpException(org.apache.ftpserver.ftplet.FtpException) SslConfigurationFactory(org.apache.ftpserver.ssl.SslConfigurationFactory) File(java.io.File) ListenerFactory(org.apache.ftpserver.listener.ListenerFactory)

Example 7 with BaseUser

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;
}
Also used : BaseUser(org.apache.ftpserver.usermanager.impl.BaseUser) Authority(org.apache.ftpserver.ftplet.Authority) ConcurrentLoginPermission(org.apache.ftpserver.usermanager.impl.ConcurrentLoginPermission) FtpServerFactory(org.apache.ftpserver.FtpServerFactory) ArrayList(java.util.ArrayList) TransferRatePermission(org.apache.ftpserver.usermanager.impl.TransferRatePermission) FtpException(org.apache.ftpserver.ftplet.FtpException) ConnectionConfigFactory(org.apache.ftpserver.ConnectionConfigFactory) WritePermission(org.apache.ftpserver.usermanager.impl.WritePermission) ListenerFactory(org.apache.ftpserver.listener.ListenerFactory) FtpException(org.apache.ftpserver.ftplet.FtpException)

Example 8 with BaseUser

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);
}
Also used : BaseUser(org.apache.ftpserver.usermanager.impl.BaseUser) Authority(org.apache.ftpserver.ftplet.Authority) ArrayList(java.util.ArrayList) WritePermission(org.apache.ftpserver.usermanager.impl.WritePermission)

Aggregations

BaseUser (org.apache.ftpserver.usermanager.impl.BaseUser)8 FtpServerFactory (org.apache.ftpserver.FtpServerFactory)7 ListenerFactory (org.apache.ftpserver.listener.ListenerFactory)7 WritePermission (org.apache.ftpserver.usermanager.impl.WritePermission)6 ArrayList (java.util.ArrayList)5 Authority (org.apache.ftpserver.ftplet.Authority)5 UserManager (org.apache.ftpserver.ftplet.UserManager)5 File (java.io.File)4 PropertiesUserManagerFactory (org.apache.ftpserver.usermanager.PropertiesUserManagerFactory)4 ConnectionConfigFactory (org.apache.ftpserver.ConnectionConfigFactory)3 FtpException (org.apache.ftpserver.ftplet.FtpException)3 IOException (java.io.IOException)2 FtpServer (org.apache.ftpserver.FtpServer)2 SslConfigurationFactory (org.apache.ftpserver.ssl.SslConfigurationFactory)2 PendingIntent (android.app.PendingIntent)1 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 SocketException (java.net.SocketException)1