-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
97 lines (88 loc) · 3.36 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
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
static class App {
private static bool StartPHP(string phpPath, UInt16 port) {
try {
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = phpPath;
psi.Arguments = " -b 127.0.0.1:" + port;
psi.CreateNoWindow = true;
psi.LoadUserProfile = false;
psi.RedirectStandardError = false;
psi.RedirectStandardOutput = false;
psi.UseShellExecute = false;
psi.WindowStyle = ProcessWindowStyle.Hidden;
//psi.WorkingDirectory = ".";
Process process = new Process();
process.StartInfo = psi;
process.Start();
return true;
} catch (Exception ex) {
Console.WriteLine(ex.Message);
return false;
}
}
[STAThread]
static int Main(string[] args) {
if (args.Length < 1) {
Console.WriteLine("Starts instance(s) of php in background.");
Console.WriteLine("Usage:");
Console.WriteLine(" HiddenPHP {Start Port[;End Port[;Increment]]");
Console.WriteLine("Example:");
Console.WriteLine(" HiddenPHP 9000 # starts one instance of php on port 9000");
Console.WriteLine(" HiddenPHP 9000;9500;100 # starts 5 instances of php on ports 9000,9100,9200,9300,9400");
return 0;
}
UInt16 start_port = 0;
UInt16 end_port = 0;
UInt16 increment = 1;
string[] parts = args[0].Split(';');
if (parts.Length < 1 || parts.Length > 3) {
Console.WriteLine("Error: invalid usage");
return -1;
}
try {
start_port = Convert.ToUInt16(parts[0]);
} catch {
Console.WriteLine(args[0] + " is not a valid port number");
return -1;
}
if (parts.Length > 1) {
try {
end_port = Convert.ToUInt16(parts[1]);
} catch {
Console.WriteLine(args[0] + " is not a valid port number");
return -1;
}
} else {
end_port = (ushort) (start_port + 1);
}
if (parts.Length > 2) {
try {
increment = Convert.ToUInt16(parts[2]);
if (increment < 1)
throw new Exception();
} catch {
Console.WriteLine("is not a valid increment number");
return -1;
}
}
if ((end_port - start_port) / increment > 32) {
Console.WriteLine("This utility can spawn maximum 32");
return -1;
}
string phpPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\php-cgi.exe";
if (!File.Exists(phpPath)) {
Console.WriteLine("php-cgi not found at '" + phpPath + "', this executable should be in same php folder that you want to run");
return 1;
}
Console.WriteLine("Executing PHP at " + phpPath);
for (UInt16 port = start_port;port < end_port;port += increment) {
Console.WriteLine("starting php on port " + port);
StartPHP(phpPath, port);
}
return 0;
}
}