-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
175 lines (144 loc) · 5.79 KB
/
Program.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
using System;
using System.Text;
using Topshelf;
using System.Net.Sockets;
using System.Net;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace Servicio061022
{
internal class Program
{
static void Main(string[] args)
{
var exitcode = HostFactory.Run(x =>
{
x.Service<Updator>(s =>
{
s.ConstructUsing(updator => new Updator());
s.WhenStarted(updator => updator.Start(args));
s.WhenStopped(updator => updator.Stop());
});
x.RunAsLocalSystem();
x.SetServiceName("CuevAppsUpdatorService");
x.SetDisplayName("CuevApps Updator Service");
x.SetDescription("This service will update your CuevApps");
StartServer();
//return 0;
});
}
public static void StartServer()
{
// Get Host IP Address that is used to establish a connection
// In this case, we get one IP address of localhost that is IP : 127.0.0.1
// If a host has multiple addresses, you will get a list of addresses
IPHostEntry host = Dns.GetHostEntry("localhost");
IPAddress ipAddress = host.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
try
{
// Create a Socket that will use Tcp protocol
Socket listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
// A Socket must be associated with an endpoint using the Bind method
listener.Bind(localEndPoint);
// Specify how many requests a Socket can listen before it gives Server busy response.
// We will listen 10 requests at a time
listener.Listen(10);
Console.WriteLine("Waiting for a connection...");
Socket handler = listener.Accept();
// Incoming data from the client.
string data = null;
byte[] bytes = null;
while (true)
{
bytes = new byte[1024];
int bytesRec = handler.Receive(bytes);
//_installerProcess = (Process) BinarySerialization.Deserializate(bytes);
//Console.WriteLine("Proceso recibido");
data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
if (data.IndexOf("<EOF>") > -1)
{
break;
}
break;
}
Console.WriteLine("Text received : {0}", data);
//LogWriter.PrintMessage("Starting the installer process at {0}", batchFilePath);
//_installerProcess.Start();
installBatch(data);
byte[] msg = Encoding.ASCII.GetBytes(data);
handler.Send(msg);
//handler.Shutdown(SocketShutdown.Both);
//handler.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
//Console.WriteLine("\n Press any key to continue...");
//Console.ReadKey();
}
private static void installBatch(string batchFilePath)
{
if (isWindows)
{
//Revisar
Process _installerProcess = new Process
{
StartInfo =
{
FileName = batchFilePath,
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = false,
CreateNoWindow = true
}
};
// start the installer process. the batch file will wait for the host app to close before starting.
Console.WriteLine("Starting the installer process at {0}", batchFilePath);
_installerProcess.Start();
}
else
{
// on macOS need to use bash to execute the shell script
Console.WriteLine("Starting the installer script process at {0} via shell exec", batchFilePath);
Exec(batchFilePath, false);
}
}
private static bool isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
protected static void Exec(string cmd, bool waitForExit = true)
{
var escapedArgs = cmd.Replace("\"", "\\\"");
var shell = "";
try
{
// leave nothing up to chance :)
shell = System.Environment.GetEnvironmentVariable("SHELL");
}
catch { }
if (string.IsNullOrWhiteSpace(shell))
{
shell = "/bin/sh";
}
Console.WriteLine("Shell is {0}", shell);
Process _installerProcess = new Process
{
StartInfo = new ProcessStartInfo
{
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
FileName = shell,
Arguments = $"-c \"{escapedArgs}\""
}
};
Console.WriteLine("Starting the process via {1} -c \"{0}\"", escapedArgs, shell);
_installerProcess.Start();
if (waitForExit)
{
Console.WriteLine("Waiting for exit...");
_installerProcess.WaitForExit();
}
}
}
}