프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / CommonLib / Common.cs @ master

이력 | 보기 | 이력해설 | 다운로드 (7.5 KB)

1 de4c7a4a djkim
using System;
2
using System.Collections.Generic;
3
using System.IO;
4
using System.Linq;
5
using System.Runtime.InteropServices;
6
using System.Security.Cryptography;
7
using System.Text;
8 99e8e943 djkim
using System.Text.RegularExpressions;
9 de4c7a4a djkim
using System.Threading.Tasks;
10
11
namespace CommonLib
12
{
13
    public class Common
14
    {
15
        [DllImport("kernel32")]
16 cbcc1a75 taeseongkim
        public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
17 de4c7a4a djkim
18 d0eda156 ljiyeon
        [DllImport("kernel32")]
19 cbcc1a75 taeseongkim
        public static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
20 d0eda156 ljiyeon
21 f2c3d74c taeseongkim
        public static string AppStartFolder
22 de4c7a4a djkim
        {
23
            get
24
            {
25 f2c3d74c taeseongkim
                return AppDomain.CurrentDomain.BaseDirectory;
26
                //return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MARKUS");
27 de4c7a4a djkim
            }
28
        }
29
30
        /// <summary>
31
        /// Client 에 설치된 MARKUS.ini 를 참조.
32
        /// </summary>
33
        /// <param name="section"></param>
34
        /// <param name="key"></param>
35
        /// <param name="def"></param>
36
        /// <returns></returns>
37
        public static string GetConfigString(string section, string key, string def)
38
        {
39
            System.Text.StringBuilder strbuilder = new System.Text.StringBuilder(512);
40 f2c3d74c taeseongkim
            GetPrivateProfileString(section, key, def, strbuilder, 512, Path.Combine(AppStartFolder, "MARKUS.ini"));
41 de4c7a4a djkim
            return strbuilder.ToString();
42
        }
43
44
        /// <summary>
45 d0eda156 ljiyeon
        /// Client 에 설치된 MARKUS.ini 를 참조.
46
        /// </summary>
47
        /// <param name="section"></param>
48
        /// <param name="key"></param>
49
        /// <param name="def"></param>
50
        /// <returns></returns>
51
        public static void WriteConfigString(string section, string key, string value)
52
        {
53 f2c3d74c taeseongkim
            WritePrivateProfileString(section, key, value, Path.Combine(AppStartFolder, "MARKUS.ini"));            
54 d0eda156 ljiyeon
        }
55
56
        /// <summary>
57 84c48033 djkim
        /// isExternal 이 True 이면 Internal IP를 External 로 치환하여 Return
58
        /// </summary>
59
        /// <param name="section"></param>
60
        /// <param name="key"></param>
61
        /// <param name="def"></param>
62
        /// <param name="isExternal"></param>
63
        /// <returns></returns>
64
        public static string GetConfigString(string section, string key, string def,bool isExternal)
65
        {
66
            System.Text.StringBuilder strbuilder = new System.Text.StringBuilder(512);
67 b2d0f316 humkyung
            string FilePath = Path.Combine(AppStartFolder, "MARKUS.ini");
68
            GetPrivateProfileString(section, key, def, strbuilder, 512, FilePath);
69 84c48033 djkim
70
            string result = strbuilder.ToString();
71
            //internal 이면 result return
72
            if (isExternal)
73
            {
74
                System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(512);
75 b2d0f316 humkyung
                GetPrivateProfileString("External", "IP", "", stringBuilder, 512, FilePath);
76 84c48033 djkim
                string external_ip = stringBuilder.ToString();
77
                stringBuilder = new StringBuilder();
78 b2d0f316 humkyung
                GetPrivateProfileString("Internal", "IP", "", stringBuilder, 512, FilePath);
79 84c48033 djkim
                string internal_ip = stringBuilder.ToString();
80 e19522be djkim
81
                //internalport:externalport
82
                stringBuilder = new StringBuilder();
83 b2d0f316 humkyung
                GetPrivateProfileString("PortForwarding", "HUB", "", stringBuilder, 512, FilePath);
84 e19522be djkim
                string[] forwarding_hub = stringBuilder.ToString().Split(':');
85
                stringBuilder = new StringBuilder();
86 b2d0f316 humkyung
                GetPrivateProfileString("PortForwarding", "RESOURCE", "", stringBuilder, 512, FilePath);
87 e19522be djkim
                string[] forwarding_resource = stringBuilder.ToString().Split(':');
88
                stringBuilder = new StringBuilder();
89 b2d0f316 humkyung
                GetPrivateProfileString("PortForwarding", "BASE", "", stringBuilder, 512, FilePath);
90 e19522be djkim
                string[] forwarding_base = stringBuilder.ToString().Split(':');
91
92
                if(result.Contains(internal_ip))
93
                    result = result.Replace(internal_ip, external_ip);
94
                if(result.Contains(":" + forwarding_hub[0]))
95
                    result = result.Replace(":" + forwarding_hub[0], ":" + forwarding_hub[1]);
96
                if (result.Contains(":" + forwarding_resource[0]))
97
                    result = result.Replace(":" + forwarding_resource[0], ":" + forwarding_resource[1]);
98
                if (result.Contains(":" + forwarding_base[0]))
99
                    result = result.Replace(":" + forwarding_base[0], ":" + forwarding_base[1]);
100 84c48033 djkim
            }
101
            return result;
102
        }
103 b2d0f316 humkyung
104 19391ad3 djkim
        public static string IPReplace(string url, bool isExternal)
105
        {
106
            string result = url;
107
            try
108
            {
109
                if (isExternal)
110
                {
111
                    System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(512);
112 f2c3d74c taeseongkim
                    GetPrivateProfileString("External", "IP", "", stringBuilder, 512, Path.Combine(AppStartFolder, "MARKUS.ini"));
113 19391ad3 djkim
                    string external_ip = stringBuilder.ToString();
114
                    stringBuilder = new StringBuilder();
115 f2c3d74c taeseongkim
                    GetPrivateProfileString("Internal", "IP", "", stringBuilder, 512, Path.Combine(AppStartFolder, "MARKUS.ini"));
116 19391ad3 djkim
                    string internal_ip = stringBuilder.ToString();
117
                    result = result.Replace(internal_ip, external_ip);
118
                }
119
            }
120
            catch (Exception)
121
            {
122
                throw;
123
            }
124
            
125
            return result;
126
        }
127 84c48033 djkim
        public static string GetAlertMessageString(string section, string key, string def)
128
        {
129
            System.Text.StringBuilder strbuilder = new System.Text.StringBuilder(1024);
130 f2c3d74c taeseongkim
            GetPrivateProfileString(section, key, def, strbuilder, 1024, Path.Combine(AppStartFolder, "MARKUS.ini"));
131 84c48033 djkim
            byte[] byte64 = Convert.FromBase64String(strbuilder.ToString());
132
            
133 99e8e943 djkim
            return Regex.Unescape(Encoding.UTF8.GetString(byte64));
134 84c48033 djkim
        }
135
        /// <summary>
136 de4c7a4a djkim
        /// 서버에 설치된 Service ini 의 Connection String 을 참조
137
        /// </summary>
138
        /// <returns></returns>
139
        public static string GetConnectionString()
140
        {
141
            System.Text.StringBuilder strbuilder = new System.Text.StringBuilder(512);
142 f2c3d74c taeseongkim
            GetPrivateProfileString("ConnectionString", "STRING", "", strbuilder, 512, Path.Combine(AppStartFolder, "FinalService.ini"));
143 de4c7a4a djkim
            return Decrypt(strbuilder.ToString(), "Doftech1073#");
144
        }
145 ab7fe8c0 humkyung
146 cbcc1a75 taeseongkim
        public static string Decrypt(string textToDecrypt, string key)
147 de4c7a4a djkim
        {
148
            RijndaelManaged rijndaelCipher = new RijndaelManaged();
149
            rijndaelCipher.Mode = CipherMode.CBC;
150
            rijndaelCipher.Padding = PaddingMode.PKCS7;
151
            rijndaelCipher.KeySize = 128;
152
            rijndaelCipher.BlockSize = 128;
153
154
            byte[] encryptedData = Convert.FromBase64String(textToDecrypt);
155
            byte[] pwdBytes = Encoding.UTF8.GetBytes(key);
156
            byte[] keyBytes = new byte[16];
157
158
            int len = pwdBytes.Length;
159
            if (len > keyBytes.Length)
160
            {
161
                len = keyBytes.Length;
162
            }
163
164
            Array.Copy(pwdBytes, keyBytes, len);
165
            rijndaelCipher.Key = keyBytes;
166
            rijndaelCipher.IV = keyBytes;
167
168
            byte[] plainText = rijndaelCipher.CreateDecryptor().TransformFinalBlock(encryptedData, 0, encryptedData.Length);
169
            return Encoding.UTF8.GetString(plainText);
170
        }
171
    }
172
}
클립보드 이미지 추가 (최대 크기: 500 MB)