123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- using System;
- using System.Windows;
- using System.Text;
- using System.IO;
- using System.Security.Cryptography;
- using log4net;
- using Quartz;
- using Quartz.Impl;
- using Agent.Services;
- using System.Threading;
- using System.Configuration;
- using Quartz.Impl.Matchers;
- namespace Agent
- {
- /// <summary>
- /// App.xaml에 대한 상호 작용 논리
- /// </summary>
- public partial class App : Application
- {
- public static readonly IScheduler Scheduler = StdSchedulerFactory.GetDefaultScheduler();
- public static readonly string _sysPlantCd = @ConfigurationManager.AppSettings["SysPlantCd"];
- public static readonly string _userCd = @ConfigurationManager.AppSettings["UserCd"];
- public static readonly string _equipCd = @ConfigurationManager.AppSettings["EquipmentCd"];
- public static readonly string _exceptExtension = @ConfigurationManager.AppSettings["ExceptExtension"];
- public static readonly string _dbServer = @ConfigurationManager.AppSettings["DbServer"];
- public static readonly string _dbName = @ConfigurationManager.AppSettings["DbName"];
- public static readonly string _dbUser = Decrypt(@ConfigurationManager.AppSettings["DbUser"], "ZR");
- public static readonly string _dbPwd = Decrypt(@ConfigurationManager.AppSettings["DbPwd"], "ZR");
-
- //string dbId = Decrypt(GetIni_Values("CONFIG", "dbId", ""), "ZR");
- //string dbPw = Decrypt(GetIni_Values("CONFIG", "dbPw", ""), "ZR");
- public static readonly string _myConnection = "Server=" + _dbServer + "; Database=" + _dbName + "; User Id=" + _dbUser + "; Password=" + _dbPwd + "; persist security info=true;packet size = 4096; Connect Timeout = 120";
- private static readonly ILog log = LogManager.GetLogger(typeof(App));
- private readonly string _programName = @ConfigurationManager.AppSettings["ProgramName"];
- private readonly HealthService _healthService = new HealthService();
- private readonly TrayService _trayService = new TrayService();
-
- private readonly PolicyService _policyService = new PolicyService();
- private readonly UserService _userService = new UserService();
- public static string _jobuserCd = @ConfigurationManager.AppSettings["UserCd"];
- private MainWindow _mainWindow;
- private Mutex _mutex;
- private void Application_Startup(object sender, StartupEventArgs e)
- {
- PreventDuplicatedApplication();
- if (!_healthService.IsConnectionSetting())
- {
- MessageBox.Show("서버 연결 정보가 없습니다.\r\n설정 파일을 확인해 주세요.", "오류", MessageBoxButton.OK, MessageBoxImage.Error);
- Shutdown();
- return;
- }
- _healthService.Check();
- if (!"OK".Equals(Current.Properties["db_status"]))
- {
- MessageBox.Show("서버와의 연결이 원활하지 않습니다.\r\n연결 상태를 확인해 주세요.", "오류", MessageBoxButton.OK, MessageBoxImage.Error);
- Shutdown();
- return;
- }
- var user = _userService.GetUserWithJoin();
- if (null == user)
- {
- MessageBox.Show("에이전트 정보 생성에 실패하였습니다.", "오류", MessageBoxButton.OK, MessageBoxImage.Error);
- Shutdown();
- }
- _userService.SaveUserProperties(user);
- _mainWindow = new MainWindow()
- {
- Title = _programName
- };
- _mainWindow.Show();
- _trayService.GetNotifyIcon().Visible = true;
- _trayService.GetNotifyIcon().DoubleClick += NotifyIcon_DoubleClick;
- Scheduler.ListenerManager.AddJobListener(_mainWindow, KeyMatcher<JobKey>.KeyEquals(_healthService.JobKey));
- _healthService.ScheduledHealthJob();
- _policyService.ScheduledPolicyJob();
- Scheduler.Start();
- log.Info($"{_programName} 시작 (UserTid={Current.Properties["user_tid"]})");
- }
- private void Application_Exit(object sender, ExitEventArgs e)
- {
- Scheduler.Shutdown();
- _trayService.DisposeNotifyIcon();
- log.Info($"{_programName} 종료 (UserTid={Current.Properties["user_tid"]})");
- }
- private void LoginWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
- {
- if (null != Current.Properties["user_tid"])
- {
- var Main = new MainWindow();
- Main.Show();
- Scheduler.Start();
- }
- }
- private void NotifyIcon_DoubleClick(object sender, System.EventArgs e)
- {
- foreach (Window window in Current.Windows)
- {
- if (1 == Current.Windows.Count && _programName.Equals(window.Title))
- {
- window.Show();
- window.WindowState = WindowState.Normal;
- return;
- }
- }
- }
- private void PreventDuplicatedApplication()
- {
- try
- {
- _mutex = new Mutex(false, _programName);
- if (!_mutex.WaitOne(0, false))
- {
- MessageBox.Show($"{_programName}가 이미 실행중입니다.", "오류", MessageBoxButton.OK, MessageBoxImage.Error);
- Current.Shutdown();
- }
- }
- catch (Exception)
- {
- Current.Shutdown();
- }
- }
- public static string Decrypt(string cipherText, string Password)
- {
- try
- {
- byte[] cipherBytes = Convert.FromBase64String(cipherText);
- var pdb = new PasswordDeriveBytes(Password, new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65,
- 0x64, 0x76, 0x65, 0x64, 0x65, 0x76
- });
- byte[] decryptedData = Decrypt(cipherBytes, pdb.GetBytes(32), pdb.GetBytes(16));
- return Encoding.Unicode.GetString(decryptedData);
- }
- catch (FormatException e)
- {
- return "입력된 값이 정상적으로 암호화 되지 않은 값 입니다";
- }
- }
- public static byte[] Decrypt(byte[] cipherData, byte[] Key, byte[] IV)
- {
- var ms = new MemoryStream();
- Rijndael alg = Rijndael.Create();
- alg.Key = Key;
- alg.IV = IV;
- var cs = new CryptoStream(ms, alg.CreateDecryptor(), CryptoStreamMode.Write);
- cs.Write(cipherData, 0, cipherData.Length);
- cs.Close();
- byte[] decryptedData = ms.ToArray();
- return decryptedData;
- }
- }
- }
|