프로젝트

일반

사용자정보

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

markus / CommonLib / Common.cs @ f3c48db7

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