void _sftpserver_OnFileOpen(object sender, SftpserverFileOpenEventArgs e) { string operation = ""; if ((e.Flags & 1) != 0) //Read { operation = "downloading"; //如果fileopen状态为没有发现文件则认为这是下载并且文件也确实没有,需要把otherdirfileinfo的状态清空 if (e.StatusCode == 2) { if (m_filepath.Contains(e.ConnectionId)) { OtherDirFileInfo odfi = m_filepath[e.ConnectionId] as OtherDirFileInfo; if (!odfi.m_is_download) { //还需要删除多余目录 odfi.clean(); } } } } if ((e.Flags & 2) != 0) //Write { operation = "uploading"; } LogHelper.InfoLog(e.User + " started " + operation + " " + e.Path + " " + e.Flags); }
public void OnAuthUser(object sender, AuthUser_EventArgs e) { LogHelper.InfoLog(string.Format("{0} clinet count:{1}", IPAddress.Parse(((IPEndPoint)e.Session.RemoteEndPoint).Address.ToString()), m_pServer.ClientCount)); string[] user = e.UserName.Split('|'); if (user.Length < 2) { LogHelper.InfoLog(string.Format("User's format error,user:{0};", e.UserName)); e.Validated = false; return; } OtherDirFileInfo ofi = new OtherDirFileInfo(); ofi.UserName = user[0]; ofi.TenantID = user[1]; ofi.Password = e.PasswData; Parameters par = Parameters.Instance(); string result = Common.SendSSLMsg(par.SftpParam.AuthenticateServerHost, par.SftpParam.AuthenticateServerPort, CreateAuthMsg(ofi)); string[] strings; if (ReturnAuthAnalyze(result, out strings, true)) { LogHelper.InfoLog(string.Format("login successed,user:{0};", e.UserName)); e.Validated = true; } else { LogHelper.InfoLog(string.Format("Authentication Failed,user:{0},pwd:***;", e.UserName)); LogHelper.DebugLog(string.Format("Authentication server info:{0},{1},pwd:{2};", par.SftpParam.AuthenticateServerHost, par.SftpParam.AuthenticateServerPort, e.PasswData)); e.Validated = false; } }
void _sftpserver_OnFileClose(object sender, SftpserverFileCloseEventArgs e) { LogHelper.InfoLog(e.User + " transferred " + e.Path); OtherDirFileInfo ofi = m_filepath[e.ConnectionId] as OtherDirFileInfo; if (ofi != null) { if (ofi.m_is_download && !string.IsNullOrEmpty(ofi.m_filePath)) { try { LogHelper.InfoLog(string.Format("file download success,delete file:{0}", ofi.m_filePath)); File.Delete(ofi.m_filePath); } catch (System.Exception ex) { LogHelper.ErrorLog(ex); } } else { Parameters pars = Parameters.Instance(); string scr_path = ""; if (ofi.m_filePath.IndexOf(".vls") != -1) { scr_path = pars.SftpParam.ScrDriver + e.Path; } else { scr_path = pars.SftpParam.VoxDriver + e.Path; } scr_path = scr_path.Replace("/", @"\"); try { //如果没有原路径代表可能此文件之前为获取文件大小 if (string.IsNullOrEmpty(ofi.m_filePath)) { File.Delete(pars.SftpParam.RootDir + e.Path); LogHelper.InfoLog(string.Format("file download success,delete file:{0}sp", ofi.m_filePath)); } else { Common.CopyFileEx(ofi.m_filePath, scr_path, true); File.Delete(ofi.m_filePath); LogHelper.InfoLog(string.Format("file upload success,copye file:{0},delete file:{1};", scr_path, ofi.m_filePath)); } } catch (System.Exception ex) { LogHelper.ErrorLog(ex); } } ofi.clean(); } }
void _sftpserver_OnSSHUserAuthRequest(object sender, SftpserverSSHUserAuthRequestEventArgs e) { LogHelper.InfoLog(string.Format("AuthMethod is {0};{1};***;", e.AuthMethod, e.User)); Parameters par = Parameters.Instance(); LogHelper.InfoLog(string.Format("IP:{0};Port:{1};", par.SftpParam.AuthenticateServerHost, par.SftpParam.AuthenticateServerPort)); if (e.AuthMethod == "none") { LogHelper.InfoLog("AuthMethod is none"); return; } string[] user = e.User.Split('|'); if (user.Length < 2) { LogHelper.InfoLog(string.Format("User's format error,user:{0};", e.User)); return; } OtherDirFileInfo ofi = new OtherDirFileInfo(); ofi.UserName = user[0]; ofi.TenantID = user[1]; ofi.Password = e.AuthParam; string result = Common.SendSSLMsg(par.SftpParam.AuthenticateServerHost, par.SftpParam.AuthenticateServerPort, CreateAuthMsg(ofi)); string[] strings; if (ReturnAuthAnalyze(result, out strings, true)) { m_filepath.Add(e.ConnectionId, ofi); e.Accept = true; LogHelper.InfoLog(string.Format("login successed,user:{0};", e.User)); } else { LogHelper.InfoLog(string.Format("Authentication Failed,user:{0},pwd:***;", e.User)); LogHelper.DebugLog(string.Format("Authentication server info:{0},{1},pwd:{2};", par.SftpParam.AuthenticateServerHost, par.SftpParam.AuthenticateServerPort, e.AuthParam)); } return; }
string CreateAuthMsg(OtherDirFileInfo ofi, string refe_type = "", string refe_value = "", string partition = "") { if (string.IsNullOrEmpty(ofi.UserName) || string.IsNullOrEmpty(ofi.TenantID) || string.IsNullOrEmpty(ofi.Password)) { return(string.Empty); } string LStrSendMessage = string.Empty; string LStrVerificationCode004 = string.Empty; LStrSendMessage = EncryDecryHelper.EncryptionCommuString("M01A21"); //0 LStrSendMessage += AscCodeToChr(27) + (ofi.TenantID); //1 LStrSendMessage += AscCodeToChr(27) + (ofi.UserName); //2 LStrSendMessage += AscCodeToChr(27) + (ofi.Password); //3 LStrSendMessage += AscCodeToChr(27) + (refe_value); //4 LStrSendMessage += AscCodeToChr(27) + (refe_type); //5 LStrSendMessage += AscCodeToChr(27) + (partition); //6 LogHelper.DebugLog(LStrSendMessage); return(LStrSendMessage); }
public void OnGetFile(object sender, FileSysEntry_EventArgs e) { try { //if(File.Exists(GetPhysicalPath(e.Name))){ // e.FileStream = File.OpenRead(GetPhysicalPath(e.Name)); OtherDirFileInfo ofi = new OtherDirFileInfo(); string[] user = e.Session.UserName.Split('|'); if (user.Length < 2) { LogHelper.InfoLog(string.Format("User's format error,user:{0};", e.Session.UserName)); return; } ofi.UserName = user[0]; ofi.TenantID = user[1]; ofi.Password = e.Session.Password; string[] request_paths = e.Name.Split('-'); if (request_paths.Length != 3) { //LogHelper.InfoLog("Request file format error!"); return; } string refe_type = request_paths[0].Substring(request_paths[0].IndexOf('/') + 1); //按文件名请求绝对路径 Parameters pars = Parameters.Instance(); string result = Common.SendSSLMsg(pars.SftpParam.AuthenticateServerHost, pars.SftpParam.AuthenticateServerPort, CreateAuthMsg(ofi, refe_type, request_paths[1], request_paths[2])); string[] strings; if (ReturnAuthAnalyze(result, out strings, false)) { if (strings.Length > 2) { string soucre_file_path = strings[1];//源文件绝对路径 try { if (File.Exists(soucre_file_path)) { e.FileStream = File.OpenRead(soucre_file_path); } else { LogHelper.ErrorLog(string.Format("file not found,filename:{0}", soucre_file_path)); } } catch (Exception ex) { LogHelper.ErrorLog(ex.Message); } } else { LogHelper.ErrorLog(string.Format("AuthAnalyze failed,count:{0}", strings.Length)); } } else { LogHelper.ErrorLog(string.Format("Authenticate failed,reference type:{0},reference:{1},partition:{2}" , refe_type, request_paths[1], request_paths[2])); } } catch { e.Validated = false; } }