📄 ftpserver.java
字号:
/****************************************************************************** * * Copyright (c) 1999-2003 AppGate Network Security AB. All Rights Reserved. * * This file contains Original Code and/or Modifications of Original Code as * defined in and that are subject to the MindTerm Public Source License, * Version 2.0, (the 'License'). You may not use this file except in compliance * with the License. * * You should have received a copy of the MindTerm Public Source License * along with this software; see the file LICENSE. If not, write to * AppGate Network Security AB, Otterhallegatan 2, SE-41118 Goteborg, SWEDEN * *****************************************************************************/package com.mindbright.net.ftp;import java.util.Hashtable;import java.util.StringTokenizer;import java.io.InputStream;import java.io.OutputStream;import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.IOException;import java.io.EOFException;import java.net.ServerSocket;import java.net.Socket;import java.net.InetAddress;import java.net.UnknownHostException;import java.util.Date;import java.text.SimpleDateFormat;public class FTPServer implements Runnable { private Thread myThread; private String identity; private String user; private String password; private boolean keepRunning; private String type; protected InetAddress localHost; protected Socket dataSocket; protected ServerSocket dataPasvAccept; protected int[] dataPortAddr; protected boolean dataConnected; protected InputStream dataIn; protected OutputStream dataOut; private FTPServerEventHandler eventHandler; private BufferedReader cmdInput; private OutputStream cmdOutput; private boolean needPassword; private final static boolean DEBUG = false; public final static int CMD_UNKN = -1; public final static int CMD_USER = 0; public final static int CMD_PASS = 1; public final static int CMD_ACCT = 2; public final static int CMD_CWD = 3; public final static int CMD_CDUP = 4; public final static int CMD_SMNT = 5; public final static int CMD_QUIT = 6; public final static int CMD_REIN = 7; public final static int CMD_PORT = 8; public final static int CMD_PASV = 9; public final static int CMD_TYPE = 10; public final static int CMD_STRU = 11; public final static int CMD_MODE = 12; public final static int CMD_RETR = 13; public final static int CMD_STOR = 14; public final static int CMD_STOU = 15; public final static int CMD_APPE = 16; public final static int CMD_ALLO = 17; public final static int CMD_REST = 18; public final static int CMD_RNFR = 19; public final static int CMD_RNTO = 20; public final static int CMD_ABOR = 21; public final static int CMD_DELE = 22; public final static int CMD_RMD = 23; public final static int CMD_MKD = 24; public final static int CMD_PWD = 25; public final static int CMD_LIST = 26; public final static int CMD_NLST = 27; public final static int CMD_SITE = 28; public final static int CMD_SYST = 29; public final static int CMD_STAT = 30; public final static int CMD_HELP = 31; public final static int CMD_NOOP = 32; public final static int CMD_FEAT = 33; public final static int CMD_MDTM = 34; public final static int CMD_SIZE = 35; public static Hashtable commands; static { commands = new Hashtable(); commands.put("USER", new Integer(CMD_USER)); commands.put("PASS", new Integer(CMD_PASS)); commands.put("ACCT", new Integer(CMD_ACCT)); commands.put("CWD", new Integer(CMD_CWD)); commands.put("CDUP", new Integer(CMD_CDUP)); commands.put("SMNT", new Integer(CMD_SMNT)); commands.put("QUIT", new Integer(CMD_QUIT)); commands.put("REIN", new Integer(CMD_REIN)); commands.put("PORT", new Integer(CMD_PORT)); commands.put("PASV", new Integer(CMD_PASV)); commands.put("TYPE", new Integer(CMD_TYPE)); commands.put("STRU", new Integer(CMD_STRU)); commands.put("MODE", new Integer(CMD_MODE)); commands.put("RETR", new Integer(CMD_RETR)); commands.put("STOR", new Integer(CMD_STOR)); commands.put("STOU", new Integer(CMD_STOU)); commands.put("APPE", new Integer(CMD_APPE)); commands.put("ALLO", new Integer(CMD_ALLO)); commands.put("REST", new Integer(CMD_REST)); commands.put("RNFR", new Integer(CMD_RNFR)); commands.put("RNTO", new Integer(CMD_RNTO)); commands.put("ABOR", new Integer(CMD_ABOR)); commands.put("DELE", new Integer(CMD_DELE)); commands.put("RMD", new Integer(CMD_RMD)); commands.put("MKD", new Integer(CMD_MKD)); commands.put("XPWD", new Integer(CMD_PWD)); commands.put("PWD", new Integer(CMD_PWD)); commands.put("LIST", new Integer(CMD_LIST)); commands.put("NLST", new Integer(CMD_NLST)); commands.put("SITE", new Integer(CMD_SITE)); commands.put("SYST", new Integer(CMD_SYST)); commands.put("STAT", new Integer(CMD_STAT)); commands.put("HELP", new Integer(CMD_HELP)); commands.put("NOOP", new Integer(CMD_NOOP)); commands.put("FEAT", new Integer(CMD_FEAT)); commands.put("MDTM", new Integer(CMD_MDTM)); commands.put("SIZE", new Integer(CMD_SIZE)); } public FTPServer(String identity, FTPServerEventHandler eventHandler, InputStream cmdInput, OutputStream cmdOutput, boolean needPassword) { this(identity, eventHandler, null, cmdInput, cmdOutput, needPassword); } public FTPServer(String identity, FTPServerEventHandler eventHandler, InetAddress localHost, InputStream cmdInput, OutputStream cmdOutput, boolean needPassword) { this.identity = identity; this.eventHandler = eventHandler; this.localHost = localHost; this.cmdInput = new BufferedReader(new InputStreamReader(cmdInput)); this.cmdOutput = cmdOutput; this.needPassword = needPassword; this.dataConnected = false; if(this.localHost == null) { try { this.localHost = InetAddress.getByName("0.0.0.0"); } catch (UnknownHostException e) { throw new Error("Error in FTPServer: " + e); } } this.type = "A"; this.keepRunning = true; this.myThread = new Thread(this, "FTPServer_" + identity); this.myThread.start(); } public void run() { if (com.mindbright.util.Util.isNetscapeJava()) { try { netscape.security.PrivilegeManager.enablePrivilege("TerminalEmulator"); } catch (netscape.security.ForbiddenTargetException e) { } } try { String[] argv = new String[2]; int code; reply(220, identity); boolean firstTry = true; do { if(!firstTry) { reply(530, "Login incorrect."); } readLogin(); firstTry = false; } while(!eventHandler.login(user, password)); reply(230, "User " + user + " logged in."); while(keepRunning) { code = readCommand(argv); try { switch(code) { case CMD_CDUP: argv[1] = ".."; case CMD_CWD: eventHandler.changeDirectory(argv[1]); reply(250, "CWD command successful."); break; case CMD_QUIT: eventHandler.quit(); // !!! TODO generate some stats ??? reply(221, "Goodbye."); keepRunning = false; break; case CMD_PORT: dataPortAddr = dataPort(argv[1]); reply(200, "PORT command successful."); break; case CMD_PASV: String hostAndPort = dataPassive(); reply(227, "Entering passive mode (" + hostAndPort + ")"); break; case CMD_TYPE: if("A".equals(argv[1]) || "I".equals(argv[1])) { type = argv[1]; reply(200, "Type set to " + argv[1] + "."); } else { reply(504, "Type " + argv[1] + " not implemented."); } break; case CMD_RETR: { checkArgAndPlainFile(argv); OutputStream data = getDataOutput(); dataStart(argv[1]); eventHandler.retrieve(argv[1], data, type.equals("I")); dataComplete(); break; } case CMD_STOR: { InputStream data = getDataInput(); dataStart(argv[1]); eventHandler.store(argv[1], data, type.equals("I")); dataComplete(); break; } case CMD_RNFR: eventHandler.renameFrom(argv[1]); reply(350, "File exists, ready for destination name."); break; case CMD_RNTO: eventHandler.renameTo(argv[1]); reply(250, "RNTO command successful."); break; case CMD_ABOR: eventHandler.abort(); dataReset(); reply(225, "ABOR command successful."); break; case CMD_DELE: eventHandler.delete(argv[1]); reply(250, "DELE command successful."); break; case CMD_RMD: eventHandler.rmdir(argv[1]); reply(250, "RMD command successful."); break; case CMD_MKD: eventHandler.mkdir(argv[1]); reply(257, "\"" + argv[1] + "\" new directory created."); break; case CMD_PWD: reply(257, "\"" + eventHandler.pwd() + "\" is current directory."); break; case CMD_LIST: { OutputStream data = getDataOutput(); dataStart("list" + (argv[1] != null ? " " + argv[1] : "")); eventHandler.list(argv[1], data); dataComplete(); break; } case CMD_NLST: { OutputStream data = getDataOutput(); dataStart("nlst" + (argv[1] != null ? argv[1] : "")); eventHandler.nameList(argv[1], data); dataComplete(); break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -