프로젝트

일반

사용자정보

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

markus / CommonLib / Common.cs @ f2c3d74c

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

1
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
using System.Text.RegularExpressions;
9
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
        [DllImport("kernel32")]
19
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
20

    
21
        public static string AppStartFolder
22
        {
23
            get
24
            {
25
                return AppDomain.CurrentDomain.BaseDirectory;
26
                //return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MARKUS");
27
            }
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
            GetPrivateProfileString(section, key, def, strbuilder, 512, Path.Combine(AppStartFolder, "MARKUS.ini"));
41
            return strbuilder.ToString();
42
        }
43

    
44
        /// <summary>
45
        /// 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
            WritePrivateProfileString(section, key, value, Path.Combine(AppStartFolder, "MARKUS.ini"));            
54
        }
55

    
56
        /// <summary>
57
        /// 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
            GetPrivateProfileString(section, key, def, strbuilder, 512, Path.Combine(AppStartFolder, "MARKUS.ini"));
68

    
69
            string result = strbuilder.ToString();
70
            //internal 이면 result return
71
            if (isExternal)
72
            {
73
                System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(512);
74
                GetPrivateProfileString("External", "IP", "", stringBuilder, 512, Path.Combine(AppStartFolder, "MARKUS.ini"));
75
                string external_ip = stringBuilder.ToString();
76
                stringBuilder = new StringBuilder();
77
                GetPrivateProfileString("Internal", "IP", "", stringBuilder, 512, Path.Combine(AppStartFolder, "MARKUS.ini"));
78
                string internal_ip = stringBuilder.ToString();
79

    
80
                //internalport:externalport
81
                stringBuilder = new StringBuilder();
82
                GetPrivateProfileString("PortForwarding", "HUB", "", stringBuilder, 512, Path.Combine(AppStartFolder, "MARKUS.ini"));
83
                string[] forwarding_hub = stringBuilder.ToString().Split(':');
84
                stringBuilder = new StringBuilder();
85
                GetPrivateProfileString("PortForwarding", "RESOURCE", "", stringBuilder, 512, Path.Combine(AppStartFolder, "MARKUS.ini"));
86
                string[] forwarding_resource = stringBuilder.ToString().Split(':');
87
                stringBuilder = new StringBuilder();
88
                GetPrivateProfileString("PortForwarding", "BASE", "", stringBuilder, 512, Path.Combine(AppStartFolder, "MARKUS.ini"));
89
                string[] forwarding_base = stringBuilder.ToString().Split(':');
90

    
91
                if(result.Contains(internal_ip))
92
                    result = result.Replace(internal_ip, external_ip);
93
                if(result.Contains(":" + forwarding_hub[0]))
94
                    result = result.Replace(":" + forwarding_hub[0], ":" + forwarding_hub[1]);
95
                if (result.Contains(":" + forwarding_resource[0]))
96
                    result = result.Replace(":" + forwarding_resource[0], ":" + forwarding_resource[1]);
97
                if (result.Contains(":" + forwarding_base[0]))
98
                    result = result.Replace(":" + forwarding_base[0], ":" + forwarding_base[1]);
99
            }
100
            return result;
101
        }
102
        public static string IPReplace(string url, bool isExternal)
103
        {
104
            string result = url;
105
            try
106
            {
107
                if (isExternal)
108
                {
109
                    System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(512);
110
                    GetPrivateProfileString("External", "IP", "", stringBuilder, 512, Path.Combine(AppStartFolder, "MARKUS.ini"));
111
                    string external_ip = stringBuilder.ToString();
112
                    stringBuilder = new StringBuilder();
113
                    GetPrivateProfileString("Internal", "IP", "", stringBuilder, 512, Path.Combine(AppStartFolder, "MARKUS.ini"));
114
                    string internal_ip = stringBuilder.ToString();
115
                    result = result.Replace(internal_ip, external_ip);
116
                }
117
            }
118
            catch (Exception)
119
            {
120
                throw;
121
            }
122
            
123
            return result;
124
        }
125
        public static string GetAlertMessageString(string section, string key, string def)
126
        {
127
            System.Text.StringBuilder strbuilder = new System.Text.StringBuilder(1024);
128
            GetPrivateProfileString(section, key, def, strbuilder, 1024, Path.Combine(AppStartFolder, "MARKUS.ini"));
129
            byte[] byte64 = Convert.FromBase64String(strbuilder.ToString());
130
            
131
            return Regex.Unescape(Encoding.UTF8.GetString(byte64));
132
        }
133
        /// <summary>
134
        /// 서버에 설치된 Service ini 의 Connection String 을 참조
135
        /// </summary>
136
        /// <returns></returns>
137
        public static string GetConnectionString()
138
        {
139
            System.Text.StringBuilder strbuilder = new System.Text.StringBuilder(512);
140
            GetPrivateProfileString("ConnectionString", "STRING", "", strbuilder, 512, Path.Combine(AppStartFolder, "FinalService.ini"));
141
            return Decrypt(strbuilder.ToString(), "Doftech1073#");
142
        }
143
        private static string Decrypt(string textToDecrypt, string key)
144
        {
145
            RijndaelManaged rijndaelCipher = new RijndaelManaged();
146
            rijndaelCipher.Mode = CipherMode.CBC;
147
            rijndaelCipher.Padding = PaddingMode.PKCS7;
148
            rijndaelCipher.KeySize = 128;
149
            rijndaelCipher.BlockSize = 128;
150

    
151
            byte[] encryptedData = Convert.FromBase64String(textToDecrypt);
152
            byte[] pwdBytes = Encoding.UTF8.GetBytes(key);
153
            byte[] keyBytes = new byte[16];
154

    
155
            int len = pwdBytes.Length;
156
            if (len > keyBytes.Length)
157
            {
158
                len = keyBytes.Length;
159
            }
160

    
161
            Array.Copy(pwdBytes, keyBytes, len);
162
            rijndaelCipher.Key = keyBytes;
163
            rijndaelCipher.IV = keyBytes;
164

    
165
            byte[] plainText = rijndaelCipher.CreateDecryptor().TransformFinalBlock(encryptedData, 0, encryptedData.Length);
166
            return Encoding.UTF8.GetString(plainText);
167
        }
168
    }
169
}
클립보드 이미지 추가 (최대 크기: 500 MB)