-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
278 lines (220 loc) · 8.57 KB
/
Copy pathForm1.cs
File metadata and controls
278 lines (220 loc) · 8.57 KB
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tethering_Reverse_Easy_Start_Progam.Helpers;
using Tethering_Reverse_Easy_Start_Progam.Services;
namespace Tethering_Reverse_Easy_Start_Progam
{
public partial class Form1 : Form
{
private readonly AdbService adbService;
private readonly GnirehtetService gnirehtetService;
private const string CaminhoGnirehtet = @"gnirehtet";
private Process _gnirehtetProcess;
public Form1()
{
InitializeComponent();
adbService = new AdbService(CaminhoGnirehtet);
gnirehtetService = new GnirehtetService(CaminhoGnirehtet);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
progressBarStatus.Value = 10;
if (!adbService.DispositivoConectado())
{
MessageBox.Show("Nenhum dispositivo conectado.", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Warning);
progressBarStatus.Value = 0;
return;
}
if (adbService.GnirehtetInstalado())
{
MessageBox.Show("Gnirehtet já está instalado — não é necessário reinstalar.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
progressBarStatus.Value = 30;
string resultado = gnirehtetService.Instalar();
progressBarStatus.Value = 70;
if (resultado.ToLower().Contains("success"))
{
MessageBox.Show("Gnirehtet instalado com sucesso!", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information);
progressBarStatus.Value = 100;
}
else
{
MessageBox.Show("Falha ao instalar gnirehtet.\nDetalhes:\n" + resultado, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
progressBarStatus.Value = 0;
}
AtualizarStatus();
AtualizarStatusGnirehtet();
}
private void AtualizarStatus()
{
bool temDispositivo = adbService.DispositivoConectado();
bool gnirehtetInstalado = temDispositivo && adbService.GnirehtetInstalado();
labelDispositivo.Text = "Dispositivo: " + (temDispositivo ? "Conectado" : "Desconectado");
labelGnirehtet.Text = "Gnirehtet: " + (gnirehtetInstalado ? "Instalado" : "Não instalado");
}
private void btnAtualizar_Click(object sender, EventArgs e)
{
AtualizarStatus();
AtualizarStatusGnirehtet();
}
private void button2_Click(object sender, EventArgs e)
{
progressBarStatus.Value = 10;
if (!adbService.GnirehtetInstalado())
{
MessageBox.Show("Gnirehtet não está instalado, impossivel desinstalar.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
progressBarStatus.Value = 0;
return;
}
if (adbService.DispositivoConectado())
{
progressBarStatus.Value = 30;
adbService.DesinstalarGnirehtet();
progressBarStatus.Value = 70;
AtualizarStatus();
progressBarStatus.Value = 100;
}
else
{
MessageBox.Show("Nenhum dispositivo conectado.", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Warning);
progressBarStatus.Value = 0;
}
AtualizarStatusGnirehtet();
}
private void btnRun_Click(object sender, EventArgs e)
{
progressBarStatus.Value = 10;
if (!adbService.DispositivoConectado())
{
MessageBox.Show("Nenhum dispositivo conectado.", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Warning);
progressBarStatus.Value = 0;
return;
}
if (!adbService.GnirehtetInstalado())
{
MessageBox.Show("Gnirehtet não está instalado. Instale primeiro!", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Warning);
progressBarStatus.Value = 0;
return;
}
if (gnirehtetService.GnirehtetAtivo())
{
MessageBox.Show("Gnirehtet já está rodando!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
btnRun.Enabled = false;
progressBarStatus.Value = 30;
// Exibir mensagem pop-up
MessageBox.Show("Gnirehtet Activo!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
var psi = new ProcessStartInfo("cmd.exe", "/c gnirehtet run")
{
WorkingDirectory = CaminhoGnirehtet,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
_gnirehtetProcess = new Process();
_gnirehtetProcess.StartInfo = psi;
_gnirehtetProcess.OutputDataReceived += (s, ea) =>
{
if (!string.IsNullOrEmpty(ea.Data))
this.BeginInvoke(new Action(() =>
{
txtOutput.AppendText(ea.Data + Environment.NewLine);
}));
};
_gnirehtetProcess.ErrorDataReceived += (s, ea) =>
{
if (!string.IsNullOrEmpty(ea.Data))
this.BeginInvoke(new Action(() =>
{
txtOutput.AppendText("[ERRO] " + ea.Data + Environment.NewLine);
}));
};
_gnirehtetProcess.Start();
_gnirehtetProcess.BeginOutputReadLine();
_gnirehtetProcess.BeginErrorReadLine();
progressBarStatus.Value = 100;
AtualizarStatusGnirehtet();
}
private void btnStop_Click(object sender, EventArgs e)
{
progressBarStatus.Value = 10;
if (!gnirehtetService.GnirehtetAtivo())
{
MessageBox.Show("Gnirehtet não está rodando. Nada para parar!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
progressBarStatus.Value = 0;
return;
}
try
{
if (_gnirehtetProcess != null && !_gnirehtetProcess.HasExited)
{
_gnirehtetProcess.Kill();
_gnirehtetProcess.Dispose();
_gnirehtetProcess = null;
}
progressBarStatus.Value = 50;
foreach (var proc in Process.GetProcessesByName("gnirehtet"))
{
proc.Kill();
proc.WaitForExit();
proc.Dispose();
}
progressBarStatus.Value = 100;
btnRun.Enabled = true;
MessageBox.Show("Gnirehtet parado.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show("Erro ao tentar parar o Gnirehtet: " + ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
progressBarStatus.Value = 0;
}
AtualizarStatusGnirehtet();
}
private void AtualizarStatusGnirehtet()
{
bool ativo = gnirehtetService.GnirehtetAtivo();
lblStatus.Text = ativo ? "Status: Ativo" : "Status: Inativo";
}
private void labelDispositivo_Click(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label5_Click(object sender, EventArgs e)
{
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void label7_Click(object sender, EventArgs e)
{
}
private void label9_Click(object sender, EventArgs e)
{
}
private void label8_Click(object sender, EventArgs e)
{
}
private void label10_Click(object sender, EventArgs e)
{
}
private void label6_Click(object sender, EventArgs e)
{
}
}
}