protected override void ParseFileNameRemote()
 {
     String stdOut = "";
     String stdErr = "";
     se = new SshExec(remoteHost, user);
     se.Password = password;
     if (Dir.EndsWith("/") || Dir.EndsWith("\\"))
     {
         se.ConnectTimeout(900000);
         se.SetTimeout(900000);
         String command = "ls -lt " + Dir + " | grep ^-";
         Log.Log(LogType.FILE, LogLevel.DEBUG, "SSH command is : " + command);
         se.RunCommand(command, ref stdOut, ref stdErr);
         StringReader sr = new StringReader(stdOut);
         String line = "";
         bool first = true;
         while ((line = sr.ReadLine()) != null)
         {
             if (first)
             {
                 Log.Log(LogType.FILE, LogLevel.DEBUG, "Command returned : " + line);
                 first = false;
             }
             String[] arr = line.Split(' ');
             if (arr[arr.Length - 1].Contains("messages"))
             {
                 FileName = Dir + arr[arr.Length - 1];
                 break;
             }
         }
         stdOut = "";
         stdErr = "";
         se.Close();
     }
     else
         FileName = Dir;
 }
Exemplo n.º 2
0
        private void timer1_Tick(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                _timer1.Enabled = false;
                _l.Log(LogType.FILE, LogLevel.INFORM, " timer1_Tick -->> Timer is Started");
                var stdOut = "";
                var stdErr = "";

                if (string.IsNullOrEmpty(_remoteHost))
                {
                    _l.Log(LogType.FILE, LogLevel.ERROR,
                           " CryptTechHotspotV_1_0_1Recorder In timer1_Tick() --> Remote host is empty");
                    return;
                }

                string host;
                int port;
                int index = _remoteHost.IndexOf(':');
                if (index >= 0 && ++index < _remoteHost.Length)
                {
                    if (int.TryParse(_remoteHost.Substring(index), out port))
                        host = _remoteHost.Substring(0, index - 1);
                    else
                    {
                        _l.Log(LogType.FILE, LogLevel.ERROR,
                               " CryptTechHotspotV_1_0_1Recorder In timer1_Tick() --> Invalid Port in remote host:" +
                               _remoteHost);
                        return;
                    }
                }
                else
                {
                    host = index >= 0 ? _remoteHost.Substring(0, index - 1) : _remoteHost;
                    port = 22;
                }

                SshExec se = null;
                try
                {
                    se = new SshExec(host, _user) { Password = _password };
                    se.ConnectTimeout(int.MaxValue);
                    if (_location.EndsWith("/"))
                    {
                        _l.Log(LogType.FILE, LogLevel.DEBUG,
                               " CryptTechHotspotV_1_0_1Recorder In timer1_Tick() --> Directory | " + _location);
                        se.Connect(port);

                        _l.Log(LogType.FILE, LogLevel.DEBUG,
                               " CryptTechHotspotV_1_0_1Recorder In timer1_Tick() --> lastFile: " + _lastFile);

                        var linuxFileParameters = _lastFile.Trim().Split(';');

                        var fileId = "";
                        var fileCreateTime = "";
                        var fileName = "";
                        var filePattern = "";
                        if (linuxFileParameters.Length > 0)
                        {
                            fileId = linuxFileParameters[0];
                            fileName = linuxFileParameters[1];
                            filePattern = linuxFileParameters[2];
                            fileCreateTime = linuxFileParameters[3];
                        }
                        else
                        {
                            _l.Log(LogType.FILE, LogLevel.ERROR, "LastFile is unrecognized:" + _lastFile);
                            return;
                        }

                        if (fileCreateTime == "-1")
                        {
                            var dt = (long)DateTimeToUnixTimestamp(DateTime.Now);
                            fileCreateTime = dt.ToString(CultureInfo.InvariantCulture);
                        }

                        _l.Log(LogType.FILE, LogLevel.DEBUG, "_fileId: " + fileId);
                        _l.Log(LogType.FILE, LogLevel.DEBUG, "_fileName: " + fileName);
                        _l.Log(LogType.FILE, LogLevel.DEBUG, "_filePattern: " + filePattern);
                        _l.Log(LogType.FILE, LogLevel.DEBUG, "_fileCreateTime: " + fileCreateTime);

                        if (_lastRecordnum == 0)
                        {
                            _lastRecordnum = 1;
                        }

                        var command = _location + "printLog.sh" + " key " + _location + " " + fileId.Trim() + " " +
                                      fileName.Trim() + " '"
                                      + filePattern.Trim() + "' "
                                      + fileCreateTime + " "
                                      + _lastRecordnum + " " +
                                      (_lastRecordnum + _maxRecordSend);

                        se.RunCommand(command, ref stdOut, ref stdErr);
                        _l.Log(LogType.FILE, LogLevel.INFORM,
                               " CryptTechHotspotV_1_0_1Recorder In timer1_Tick() -->> SSH command : " + command);

                        var sr = new StringReader(stdOut);
                        _l.Log(LogType.FILE, LogLevel.INFORM,
                               "CryptTechHotspotV_1_0_1Recorder In timer1_Tick() -->> Result: " + stdOut);

                        var state = 1;
                        var lineCounter = 0;

                        var line = "";
                        while ((line = sr.ReadLine()) != null)
                        {
                            switch (state)
                            {
                                case 1:
                                    _l.Log(LogType.FILE, LogLevel.INFORM,
                                           " CryptTechHotspotV_1_0_1Recorder In timer1_Tick() -->> Start While.");
                                    if (line.Equals("key;BEGIN;NEW"))
                                    {
                                        state = 2;
                                    }
                                    else if (line.Equals("key;BEGIN;OK"))
                                    {
                                        state = 5;
                                    }
                                    else if (line.Equals("key;NOFILE"))
                                    {
                                        _l.Log(LogType.FILE, LogLevel.WARN,
                                               " CryptTechHotspotV_1_0_1Recorder In timer1_Tick() -->> State 1 Error There is no file.");
                                        return;
                                    }
                                    else
                                    {
                                        _l.Log(LogType.FILE, LogLevel.ERROR,
                                               " CryptTechHotspotV_1_0_1Recorder In timer1_Tick() -->> State 1 Error Unknown Line. " +
                                               line);
                                        return;
                                    }
                                    break;
                                case 2:
                                    if (line.StartsWith("FILE;"))
                                    {
                                        var lineArr = line.Split(new[] { ';' }, 4);
                                        if (lineArr.Length == 4)
                                        {
                                            fileId = lineArr[1];
                                            fileCreateTime = lineArr[2];
                                            fileName = lineArr[3];
                                            state = 3;
                                            break;
                                        }
                                        _l.Log(LogType.FILE, LogLevel.ERROR,
                                               " CryptTechHotspotV_1_0_1Recorder In timer1_Tick() -->> State 2 Error Missing Fields. " +
                                               line);
                                    }
                                    else
                                    {
                                        _l.Log(LogType.FILE, LogLevel.ERROR,
                                               " CryptTechHotspotV_1_0_1Recorder In timer1_Tick() -->> State 2 Error Unknown Line. " +
                                               line);
                                    }
                                    return;
                                case 3:
                                    if (line.Equals("key;ENDS"))
                                    {
                                        try
                                        {
                                            CustomServiceBase customServiceBase =
                                                GetInstanceService("Security Manager Remote Recorder");
                                            _l.Log(LogType.FILE, LogLevel.DEBUG,
                                                   " CryptTechHotspotV_1_0_1Recorder In timer1_Tick() -->> Record sending. " +
                                                   _lastFile + " / " + fileId + ";" + fileName + ";" + filePattern + ";" +
                                                   fileCreateTime);
                                            customServiceBase.SetReg(id, "0", "",
                                                                     fileId + ";" + fileName + ";" + filePattern + ";" +
                                                                     fileCreateTime, "", _lastRecordDate);
                                            _lastRecordnum = 0;
                                            _lastFile = fileId + ";" + fileName + ";" + filePattern + ";" +
                                                        fileCreateTime;
                                            _l.Log(LogType.FILE, LogLevel.DEBUG,
                                                   " CryptTechHotspotV_1_0_1Recorder In timer1_Tick() -->> Record sended." +
                                                   _lastFile);
                                        }
                                        catch (Exception exception)
                                        {
                                            _l.Log(LogType.FILE, LogLevel.ERROR,
                                                   " CryptTechHotspotV_1_0_1Recorder In timer1_Tick() -->> Record sending Error." +
                                                   exception.Message);
                                        }
                                    }
                                    else
                                    {
                                        _l.Log(LogType.FILE, LogLevel.ERROR,
                                               " CryptTechHotspotV_1_0_1Recorder In timer1_Tick() -->> State 3 Error Unknown Line. " +
                                               line);
                                    }
                                    return;
                                case 5:
                                    if (line.StartsWith("FILE;"))
                                    {
                                        var lineArr = line.Split(new[] { ';' }, 4);
                                        if (lineArr.Length == 4)
                                        {
                                            fileId = lineArr[1];
                                            fileCreateTime = lineArr[2];
                                            fileName = lineArr[3];
                                            state = 6;
                                            break;
                                        }
                                        _l.Log(LogType.FILE, LogLevel.ERROR,
                                               " CryptTechHotspotV_1_0_1Recorder In timer1_Tick() -->> State 5 Error Missing Fields. " +
                                               line);
                                    }
                                    else
                                    {
                                        _l.Log(LogType.FILE, LogLevel.ERROR,
                                               " CryptTechHotspotV_1_0_1Recorder In timer1_Tick() -->> State 5 Error Unknown Line. " +
                                               line);
                                    }
                                    return;
                                case 6:
                                    if (line.Equals("OUTPUT;BEGIN"))
                                    {
                                        try
                                        {
                                            CustomServiceBase customServiceBase =
                                                GetInstanceService("Security Manager Remote Recorder");
                                            _l.Log(LogType.FILE, LogLevel.DEBUG,
                                                   " CryptTechHotspotV_1_0_1Recorder In timer1_Tick() -->> Record sending.");
                                            customServiceBase.SetReg(id,
                                                                     _lastRecordnum.ToString(
                                                                         CultureInfo.InvariantCulture), line,
                                                                     fileId + ";" + fileName + ";" + filePattern + ";" +
                                                                     fileCreateTime, "", _lastRecordDate);
                                            _lastFile = fileId + ";" + fileName + ";" + filePattern + ";" +
                                                        fileCreateTime;
                                            _l.Log(LogType.FILE, LogLevel.DEBUG,
                                                   " CryptTechHotspotV_1_0_1Recorder In timer1_Tick() -->> Record sended.");
                                            state = 7;
                                            break;
                                        }
                                        catch (Exception exception)
                                        {
                                            _l.Log(LogType.FILE, LogLevel.ERROR,
                                                   " CryptTechHotspotV_1_0_1Recorder In CoderParse() -->> Record sending Error." +
                                                   exception.Message);
                                        }
                                    }
                                    else
                                    {
                                        _l.Log(LogType.FILE, LogLevel.ERROR,
                                               " CryptTechHotspotV_1_0_1Recorder In timer1_Tick() -->> State 3 Error Unknown Line. " +
                                               line);
                                    }
                                    return;
                                case 7:
                                    if (line.StartsWith("+"))
                                    {
                                        _l.Log(LogType.FILE, LogLevel.DEBUG,
                                               " CryptTechHotspotV_1_0_1Recorder In timer1_Tick() -->> lines: " + line);
                                        if (CoderParse(line.Substring(1).Trim()))
                                        {
                                            _l.Log(LogType.FILE, LogLevel.DEBUG,
                                                   " CryptTechHotspotV_1_0_1Recorder In timer1_Tick() -->> Date inserted. ");
                                            lineCounter++;
                                            break;
                                        }
                                    }
                                    else
                                    {
                                        _l.Log(LogType.FILE, LogLevel.ERROR,
                                               " CryptTechHotspotV_1_0_1Recorder In timer1_Tick() -->> State 7 Error Unknown Line. " +
                                               line);
                                    }
                                    return;
                            }
                        }
                        _l.Log(LogType.FILE, LogLevel.DEBUG,
                               " CryptTechHotspotV_1_0_1Recorder In timer1_Tick() -->> Inform." + state + " / " +
                               lineCounter + " / " + _maxRecordSend);

                        if (state > 1)
                        {
                            if (lineCounter < _maxRecordSend)
                            {
                                CheckEof(se, fileId, fileName, filePattern, fileCreateTime);
                            }
                        }
                        else
                        {
                            _l.Log(LogType.FILE, LogLevel.ERROR,
                                   " CryptTechHotspotV_1_0_1Recorder In timer1_Tick() -->> State 0 Error Unexpected end of stream.");
                        }
                    }
                }
                finally
                {
                    if (se != null)
                        se.Close();
                }
            }
            catch (Exception exception)
            {
                _l.Log(LogType.FILE, LogLevel.ERROR, " timer1_Tick -->> Error : " + exception);
            }
            finally
            {
                _timer1.Enabled = true;
                _l.Log(LogType.FILE, LogLevel.INFORM, " timer1_Tick -->> Timer is finished.");
            }
        }