-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cs
318 lines (289 loc) · 12.3 KB
/
Main.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
using System;
using System.Windows.Forms;
using System.IO; //FileSystemWatcher
using Microsoft.WindowsAPICodePack.Dialogs;
using System.Net;
using Renci.SshNet;
using Renci.SshNet.Sftp;
using System.Diagnostics;
namespace Send_And_Delete
{
public partial class Main : Form
{
public Main()
{
InitializeComponent();
this.MaximizeBox = false;
//중복실행방지
Process[] processes = null;
string CurrentProcess = Process.GetCurrentProcess().ProcessName.ToUpper();
processes = Process.GetProcessesByName(CurrentProcess);
if (processes.Length > 1)
{
MessageBox.Show("이미 SAD가 실행중입니다.\n작업표시줄 오른쪽 아이콘을 확인해보세요!", "파란대나무숲 SAD :(", MessageBoxButtons.OK, MessageBoxIcon.Error);
//Application.Exit();
Environment.Exit(0);
}
}
private string IP;
private bool Run = false, setup = false, ipf = false;
private FileSystemWatcher watcher = new FileSystemWatcher();
private string SelectFolder() //폴더 선택
{
CommonOpenFileDialog SF = new CommonOpenFileDialog();
SF.IsFolderPicker = true; //폴더 지정
SF.Title = "보낼 파일들이 있는 폴더";
if (SF.ShowDialog() == CommonFileDialogResult.Ok) return SF.FileName; //폴더가 정상적으로 선택됐으면 지정
return Properties.Settings.Default.Path;
}
private void TextBox_path_Click(object sender, EventArgs e)
{
if (!Run)
{
TextBox_path.Text = SelectFolder();
Properties.Settings.Default.Path = TextBox_path.Text;
Properties.Settings.Default.Save();
}
else MessageBox.Show("SAD가 폴더를 감시중입니다.\n감시를 해제한 후 다시 눌러보세요!", "파란대나무숲 SAD :(", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
private void Main_Load(object sender, EventArgs e)
{
try
{
WebClient wc = new WebClient();
string new_ver = wc.DownloadString("http://sad.bbforest.net/ver.txt");
ver.Text = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
if (ver.Text != new_ver)
{
DialogResult result = MessageBox.Show("업데이트가 있습니다! 업데이트 할까요?", "파란대나무숲 SAD :(", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (result == DialogResult.Yes)
{
wc.DownloadFile("http://sad.bbforest.net/sad.msi", Environment.GetEnvironmentVariable("temp") + "\\sad.msi");
Process.Start(Environment.GetEnvironmentVariable("temp") + "\\sad.msi");
Environment.Exit(0);
}
}
}
catch (Exception)
{
ListBox($"[Error] 버전 확인 실패!");
}
TextBox_path.Text = Properties.Settings.Default.Path;
Autodelete.Checked = Properties.Settings.Default.AD;
AutoStart.Checked = Properties.Settings.Default.AutoRun;
WindowsRun.Checked = Properties.Settings.Default.WinStart;
ST_Tray.Checked = Properties.Settings.Default.ST_Tray;
if (Properties.Settings.Default.ST_Tray)
{
this.Opacity = 0;
this.ShowInTaskbar = false;
Tray.Visible = true;
}
IP = new WebClient().DownloadString("http://ipinfo.io/ip").Trim();
TextBox_URL.Text = "http://sad.bbforest.net?dir=" + IP;
ListBox($"[SAD] 현재 설정");
ListBox($" 설정 폴더 : {TextBox_path.Text}");
ListBox($" 윈도우 시작시 자동 실행 : {WindowsRun.Checked}");
ListBox($" 시작시 자동 감시 : {AutoStart.Checked}");
ListBox($" 시작시 트레이로 : {ST_Tray.Checked}");
ListBox($" 업로드 후 삭제 : {Autodelete.Checked}");
ListBox($" 업로드 URL : {TextBox_URL.Text}");
if (AutoStart.Checked) RunSet();
Upload("","");
}
private void Tray_Click(object sender, EventArgs e)
{
this.Opacity = 1;
this.ShowInTaskbar = true;
Tray.Visible = false;
this.WindowState = FormWindowState.Normal;
}
private void Button_start_Click(object sender, EventArgs e)
{
RunSet();
}
private void RunSet()
{
Run = !Run;
ListBox($"[SAD] 감시 상태 : {Run}");
if (Run == true)
{
FileWatcher();
}
else
{
watcher.EnableRaisingEvents = false;
this.Text = "SAD(전송 및 삭제, Send And Delete)";
}
}
private void Upload(string name, string link)
{
try
{
var protocol = new ConnectionInfo("Address", "ID", new PasswordAuthenticationMethod("ID", "PW"));
var sftp = new SftpClient(protocol);
// SFTP 서버 연결
sftp.Connect();
if (!ipf)
{
try
{
sftp.CreateDirectory($"/upload/{IP}"); //폴더 생성 시도
ListBox($"[SAD] 서버에 폴더 생성");
ipf = true;
return;
}
catch (Exception)
{
ipf = true; //실패시 폴더가 이미 있는 것으로 간주
return;
}
}
using (var infile = File.Open(link, FileMode.Open))
{
sftp.UploadFile(infile, $"/upload/{IP}/{name}"); //업로드
}
sftp.Disconnect();
if (name != "index.php") ListBox($"[SAD] 업로드 완료 : {TextBox_URL.Text}/{name}");
if(Autodelete.Checked) File.Delete(link); //자동삭제 체크시 삭제
}
catch (Exception)
{
ListBox($"[Error] 업로드 서버 연결 실패!");
}
}
private void FileWatcher()
{
try
{
if (setup == false)
{
setup = true;
watcher.Path = Properties.Settings.Default.Path;
watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.Size;
watcher.Filter = "*.*";
watcher.IncludeSubdirectories = true;
watcher.Created += new FileSystemEventHandler(Event);
//watcher.Changed += new FileSystemEventHandler(Event);
watcher.Deleted += new FileSystemEventHandler(Event);
watcher.Renamed += new RenamedEventHandler(Rename);
}
watcher.EnableRaisingEvents = true;
this.Text = "SAD(전송 및 삭제, Send And Delete) 감시중";
}
catch (Exception)
{
MessageBox.Show("폴더를 못 찾았어요. :(\n올바른 폴더를 지정해주세요.", "파란대나무숲 SAD :(", MessageBoxButtons.OK, MessageBoxIcon.Error);
Run = false;
ListBox($"[SAD] 감시 상태 : {Run}");
this.Text = "SAD(전송 및 삭제, Send And Delete)";
watcher.EnableRaisingEvents = false;
}
}
private void Event(object source, FileSystemEventArgs e)
{
ListBox($"[FILE] {e.ChangeType} : {e.Name}");
if (e.ChangeType.ToString() == "Created") Upload(e.Name, e.FullPath);
}
private void TextBox_URL_Click(object sender, EventArgs e)
{
Clipboard.SetText(TextBox_URL.Text);
MessageBox.Show("복사되었습니다!", "파란대나무숲 Sad :(", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void Rename(object source, RenamedEventArgs e)
{
//string sub = e.FullPath.Replace(Properties.Settings.Default.Path, "");
ListBox($"[FILE] 이름 변경 : {e.Name}");
}
private void Autodelete_Click(object sender, EventArgs e)
{
Properties.Settings.Default.AD = Autodelete.Checked;
Properties.Settings.Default.Save();
ListBox($"[SAD] 업로드 후 삭제 : {Autodelete.Checked}");
}
private void ListBox(string msg)
{
this.Invoke(new Action(delegate () {
listBox.Items.Add(msg);
listBox.SelectedIndex = listBox.Items.Count - 1;
listBox.SelectedIndex = -1;
}));
}
private void UploadView_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start(TextBox_URL.Text);
}
private void AutoStart_Click(object sender, EventArgs e)
{
Properties.Settings.Default.AutoRun = AutoStart.Checked;
Properties.Settings.Default.Save();
ListBox($"[SAD] 시작시 자동 감시 : {AutoStart.Checked}");
}
private void WindowsRun_Click(object sender, EventArgs e)
{
Properties.Settings.Default.WinStart = WindowsRun.Checked;
Properties.Settings.Default.Save();
ListBox($"[SAD] 윈도우 시작시 자동 실행 : {WindowsRun.Checked}");
if (WindowsRun.Checked) AddStartupProgram("net.bbforest.sad", Application.ExecutablePath);
else if (!WindowsRun.Checked) RemoveStartupProgram("net.bbforest.sad");
}
private static readonly string _startupRegPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
private Microsoft.Win32.RegistryKey GetRegKey(string regPath, bool writable)
{
return Microsoft.Win32.Registry.CurrentUser.OpenSubKey(regPath, writable);
}
public void AddStartupProgram(string programName, string executablePath)
{
using (var regKey = GetRegKey(_startupRegPath, true))
{
try
{
// 키가 이미 등록돼 있지 않을때만 등록
if (regKey.GetValue(programName) == null)
regKey.SetValue(programName, executablePath);
regKey.Close();
}
catch (Exception)
{
}
}
}
private void ST_Tray_Click(object sender, EventArgs e)
{
Properties.Settings.Default.ST_Tray = ST_Tray.Checked;
Properties.Settings.Default.Save();
ListBox($"[SAD] 시작시 트레이로 : {ST_Tray.Checked}");
}
private void Main_Move(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Opacity = 0;
this.ShowInTaskbar = false;
Tray.Visible = true;
}
}
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult result = MessageBox.Show("프로그램을 종료할까요?\n최소화를 누르면 트레이에서 동작해요!", "파란대나무숲 SAD :(", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (result == DialogResult.No) e.Cancel = true;
}
// 등록된 프로그램 제거
public void RemoveStartupProgram(string programName)
{
using (var regKey = GetRegKey(_startupRegPath, true))
{
try
{
// 키가 이미 존재할때만 제거
if (regKey.GetValue(programName) != null)
regKey.DeleteValue(programName, false);
regKey.Close();
}
catch (Exception)
{
}
}
}
}
}