What is the recommended way to work with FTP

Ok, I just learned how to use the commons-net libraries and I want you to give it a check. I want to send individual player chat logs to a FTP Server, and I am afraid this is incorrect. :worried:

package com.blockenton.server.manager.chat;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.inject.Inject;
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
import ninja.leaping.configurate.loader.ConfigurationLoader;
import org.apache.commons.net.ftp.FTPClient;
import org.slf4j.Logger;
import org.spongepowered.api.Game;
import org.spongepowered.api.Server;
import org.spongepowered.api.event.state.ServerStartedEvent;
import org.spongepowered.api.event.state.ServerStoppingEvent;
import org.spongepowered.api.plugin.Plugin;
import org.spongepowered.api.service.command.CommandService;
import org.spongepowered.api.service.config.DefaultConfig;
import org.spongepowered.api.text.TextBuilder;
import org.spongepowered.api.util.event.Subscribe;

import java.io.File;
import java.io.IOException;

import static com.blockenton.server.manager.chat.net.LogChat.*;

@Plugin(id="com.blockenton.server.manager.chat", name="chat-manager", version="0.0.1-DEV-B-0000001")
public class ChatManagerPlugin {

    @Inject private Game game;
    @Inject private static Logger logger;
    @Inject @DefaultConfig(sharedRoot=false) private File defaultConfig;
    @Inject @DefaultConfig(sharedRoot=false) private ConfigurationLoader<CommentedConfigurationNode> configManager;

    private CommandService cmdService = game.getCommandDispatcher();
    private Server server;
    private TextBuilder builder;

    private FTPClient ftpClient;

    private Gson gson = new GsonBuilder()
            .disableHtmlEscaping()
            .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
            .setPrettyPrinting()
            .serializeNulls()
            .create();

    private static Logger getLogger() {
        return logger;
    }

    public static boolean chatDirectoryExists, playerDirectoryExists;

    @Subscribe
    public void onServerStart(ServerStartedEvent event) {
        getLogger().info("Plugin Started");

        setIp("184.95.42.202"); //Set the FTP ip
        setPassword("YOU DONT NEED TO KNOW"); //Set FTP password
        setPort(21); //Set the FTP port
        setUsername("frogocomics.313453"); //Set the FTP username

        run(ftpClient); //Connect to the FTPServer
        chatDirectoryExists = checkForPlayerChatDictionary(ftpClient); //Check for the dictionaries
        playerDirectoryExists = checkForPlayerStatsDictionary(ftpClient);
        
        try {
            if(!chatDirectoryExists) {
                createDictionaries(ftpClient, "\\player-chat");
            }
            
            if(!playerDirectoryExists) {
                createDictionaries(ftpClient, "\\player-stats");
            }
        } catch(IOException e) {
            getLogger().error("Unable to connect to the database. Stopping the server is advised. Full stack trace:");
            getLogger().error(e.getMessage().trim());
        }
    }

    @Subscribe
    public void onServerStop(ServerStoppingEvent event) {
        getLogger().info("Stopping chat-manager");
    }
}

I also included some of my own methods here, so I will describe it to you.
(From: com.blockenton.server.manager.chat.net.LogChat)

public static void run(FTPClient ftpClient) //Connects to the server
public static boolean checkForPlayerStatsDirectory(FTPClient ftpClient, String dir) //Checks if the Player stats directory exists. dir(String) is the relative path.
public static boolean checkForPlayerChatDirectory(FTPClient ftpClient, String dir) //Look above!
setIp(String ip), setPassword(String password), setPort(int port), setUsername(String username) //Exactly what it says

In a thread where you spoon-fed code to somebody else.

Suggestion: Ask questions like “What is the recommended way to use FtpClient” and not “It doesn’t work”

4 Likes

You should never hard-code passwords.

1 Like

might be better to import a file instead (eg import securepasswordstorage.txt)

2 Likes

An obfuscated passcode in a .dat file would be better than YOU DONT NEED TO KNOW. Just sayin :wink:

Eh. If the password is compromised, it’s likely the entire server is compromised. And if they have access to both the program and the password file, the obfuscation doesn’t matter.

Changed the title. FTP is pretty new to me, so there might be errors.

security through obscurity is not an acceptable form of action, all that does is delay the inevitable compromise

2 Likes