markus / CommonLib / Common.cs @ ab7fe8c0
이력 | 보기 | 이력해설 | 다운로드 (7.63 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 | 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 | f2c3d74c | taeseongkim | GetPrivateProfileString(section, key, def, strbuilder, 512, Path.Combine(AppStartFolder, "MARKUS.ini")); |
68 | 84c48033 | djkim | |
69 | string result = strbuilder.ToString(); |
||
70 | //internal 이면 result return |
||
71 | if (isExternal) |
||
72 | { |
||
73 | System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(512); |
||
74 | f2c3d74c | taeseongkim | GetPrivateProfileString("External", "IP", "", stringBuilder, 512, Path.Combine(AppStartFolder, "MARKUS.ini")); |
75 | 84c48033 | djkim | string external_ip = stringBuilder.ToString(); |
76 | stringBuilder = new StringBuilder(); |
||
77 | f2c3d74c | taeseongkim | GetPrivateProfileString("Internal", "IP", "", stringBuilder, 512, Path.Combine(AppStartFolder, "MARKUS.ini")); |
78 | 84c48033 | djkim | string internal_ip = stringBuilder.ToString(); |
79 | e19522be | djkim | |
80 | //internalport:externalport |
||
81 | stringBuilder = new StringBuilder(); |
||
82 | f2c3d74c | taeseongkim | GetPrivateProfileString("PortForwarding", "HUB", "", stringBuilder, 512, Path.Combine(AppStartFolder, "MARKUS.ini")); |
83 | e19522be | djkim | string[] forwarding_hub = stringBuilder.ToString().Split(':'); |
84 | stringBuilder = new StringBuilder(); |
||
85 | f2c3d74c | taeseongkim | GetPrivateProfileString("PortForwarding", "RESOURCE", "", stringBuilder, 512, Path.Combine(AppStartFolder, "MARKUS.ini")); |
86 | e19522be | djkim | string[] forwarding_resource = stringBuilder.ToString().Split(':'); |
87 | stringBuilder = new StringBuilder(); |
||
88 | f2c3d74c | taeseongkim | GetPrivateProfileString("PortForwarding", "BASE", "", stringBuilder, 512, Path.Combine(AppStartFolder, "MARKUS.ini")); |
89 | e19522be | djkim | 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 | 84c48033 | djkim | } |
100 | return result; |
||
101 | } |
||
102 | 19391ad3 | djkim | 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 | f2c3d74c | taeseongkim | GetPrivateProfileString("External", "IP", "", stringBuilder, 512, Path.Combine(AppStartFolder, "MARKUS.ini")); |
111 | 19391ad3 | djkim | string external_ip = stringBuilder.ToString(); |
112 | stringBuilder = new StringBuilder(); |
||
113 | f2c3d74c | taeseongkim | GetPrivateProfileString("Internal", "IP", "", stringBuilder, 512, Path.Combine(AppStartFolder, "MARKUS.ini")); |
114 | 19391ad3 | djkim | 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 | 84c48033 | djkim | public static string GetAlertMessageString(string section, string key, string def) |
126 | { |
||
127 | System.Text.StringBuilder strbuilder = new System.Text.StringBuilder(1024); |
||
128 | f2c3d74c | taeseongkim | GetPrivateProfileString(section, key, def, strbuilder, 1024, Path.Combine(AppStartFolder, "MARKUS.ini")); |
129 | 84c48033 | djkim | byte[] byte64 = Convert.FromBase64String(strbuilder.ToString()); |
130 | |||
131 | 99e8e943 | djkim | return Regex.Unescape(Encoding.UTF8.GetString(byte64)); |
132 | 84c48033 | djkim | } |
133 | /// <summary> |
||
134 | de4c7a4a | djkim | /// 서버에 설치된 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 | f2c3d74c | taeseongkim | GetPrivateProfileString("ConnectionString", "STRING", "", strbuilder, 512, Path.Combine(AppStartFolder, "FinalService.ini")); |
141 | de4c7a4a | djkim | return Decrypt(strbuilder.ToString(), "Doftech1073#"); |
142 | } |
||
143 | ab7fe8c0 | humkyung | |
144 | de4c7a4a | djkim | private static string Decrypt(string textToDecrypt, string key) |
145 | { |
||
146 | RijndaelManaged rijndaelCipher = new RijndaelManaged(); |
||
147 | rijndaelCipher.Mode = CipherMode.CBC; |
||
148 | rijndaelCipher.Padding = PaddingMode.PKCS7; |
||
149 | rijndaelCipher.KeySize = 128; |
||
150 | rijndaelCipher.BlockSize = 128; |
||
151 | |||
152 | byte[] encryptedData = Convert.FromBase64String(textToDecrypt); |
||
153 | byte[] pwdBytes = Encoding.UTF8.GetBytes(key); |
||
154 | byte[] keyBytes = new byte[16]; |
||
155 | |||
156 | int len = pwdBytes.Length; |
||
157 | if (len > keyBytes.Length) |
||
158 | { |
||
159 | len = keyBytes.Length; |
||
160 | } |
||
161 | |||
162 | Array.Copy(pwdBytes, keyBytes, len); |
||
163 | rijndaelCipher.Key = keyBytes; |
||
164 | rijndaelCipher.IV = keyBytes; |
||
165 | |||
166 | byte[] plainText = rijndaelCipher.CreateDecryptor().TransformFinalBlock(encryptedData, 0, encryptedData.Length); |
||
167 | return Encoding.UTF8.GetString(plainText); |
||
168 | } |
||
169 | } |
||
170 | } |