프로젝트

일반

사용자정보

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

markus / CommonLib / Common.cs @ master

이력 | 보기 | 이력해설 | 다운로드 (7.5 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
        public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
17

    
18
        [DllImport("kernel32")]
19
        public 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
            string FilePath = Path.Combine(AppStartFolder, "MARKUS.ini");
68
            GetPrivateProfileString(section, key, def, strbuilder, 512, FilePath);
69

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

    
81
                //internalport:externalport
82
                stringBuilder = new StringBuilder();
83
                GetPrivateProfileString("PortForwarding", "HUB", "", stringBuilder, 512, FilePath);
84
                string[] forwarding_hub = stringBuilder.ToString().Split(':');
85
                stringBuilder = new StringBuilder();
86
                GetPrivateProfileString("PortForwarding", "RESOURCE", "", stringBuilder, 512, FilePath);
87
                string[] forwarding_resource = stringBuilder.ToString().Split(':');
88
                stringBuilder = new StringBuilder();
89
                GetPrivateProfileString("PortForwarding", "BASE", "", stringBuilder, 512, FilePath);
90
                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
            }
101
            return result;
102
        }
103

    
104
        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
                    GetPrivateProfileString("External", "IP", "", stringBuilder, 512, Path.Combine(AppStartFolder, "MARKUS.ini"));
113
                    string external_ip = stringBuilder.ToString();
114
                    stringBuilder = new StringBuilder();
115
                    GetPrivateProfileString("Internal", "IP", "", stringBuilder, 512, Path.Combine(AppStartFolder, "MARKUS.ini"));
116
                    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
        public static string GetAlertMessageString(string section, string key, string def)
128
        {
129
            System.Text.StringBuilder strbuilder = new System.Text.StringBuilder(1024);
130
            GetPrivateProfileString(section, key, def, strbuilder, 1024, Path.Combine(AppStartFolder, "MARKUS.ini"));
131
            byte[] byte64 = Convert.FromBase64String(strbuilder.ToString());
132
            
133
            return Regex.Unescape(Encoding.UTF8.GetString(byte64));
134
        }
135
        /// <summary>
136
        /// 서버에 설치된 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
            GetPrivateProfileString("ConnectionString", "STRING", "", strbuilder, 512, Path.Combine(AppStartFolder, "FinalService.ini"));
143
            return Decrypt(strbuilder.ToString(), "Doftech1073#");
144
        }
145

    
146
        public static string Decrypt(string textToDecrypt, string key)
147
        {
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)