|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.IO; |
| 4 | +using System.Reflection; |
| 5 | + |
| 6 | +static class App { |
| 7 | + |
| 8 | + private static bool StartPHP(string phpPath, UInt16 port) { |
| 9 | + try { |
| 10 | + ProcessStartInfo psi = new ProcessStartInfo(); |
| 11 | + psi.FileName = phpPath; |
| 12 | + psi.Arguments = " -b 127.0.0.1:" + port; |
| 13 | + psi.CreateNoWindow = true; |
| 14 | + psi.LoadUserProfile = false; |
| 15 | + psi.RedirectStandardError = false; |
| 16 | + psi.RedirectStandardOutput = false; |
| 17 | + psi.UseShellExecute = false; |
| 18 | + psi.WindowStyle = ProcessWindowStyle.Hidden; |
| 19 | + //psi.WorkingDirectory = "."; |
| 20 | + Process process = new Process(); |
| 21 | + process.StartInfo = psi; |
| 22 | + process.Start(); |
| 23 | + return true; |
| 24 | + } catch (Exception ex) { |
| 25 | + Console.WriteLine(ex.Message); |
| 26 | + return false; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + [STAThread] |
| 31 | + static int Main(string[] args) { |
| 32 | + if (args.Length < 1) { |
| 33 | + Console.WriteLine("Starts instance(s) of php in background."); |
| 34 | + Console.WriteLine("Usage:"); |
| 35 | + Console.WriteLine(" HiddenPHP {Start Port[;End Port[;Increment]]"); |
| 36 | + Console.WriteLine("Example:"); |
| 37 | + Console.WriteLine(" HiddenPHP 9000 # starts one instance of php on port 9000"); |
| 38 | + Console.WriteLine(" HiddenPHP 9000;9500;100 # starts 5 instances of php on ports 9000,9100,9200,9300,9400"); |
| 39 | + return 0; |
| 40 | + } |
| 41 | + |
| 42 | + UInt16 start_port = 0; |
| 43 | + UInt16 end_port = 0; |
| 44 | + UInt16 increment = 1; |
| 45 | + |
| 46 | + string[] parts = args[0].Split(';'); |
| 47 | + if (parts.Length < 1 || parts.Length > 3) { |
| 48 | + Console.WriteLine("Error: invalid usage"); |
| 49 | + return -1; |
| 50 | + } |
| 51 | + |
| 52 | + try { |
| 53 | + start_port = Convert.ToUInt16(parts[0]); |
| 54 | + } catch { |
| 55 | + Console.WriteLine(args[0] + " is not a valid port number"); |
| 56 | + return -1; |
| 57 | + } |
| 58 | + |
| 59 | + if (parts.Length > 1) { |
| 60 | + try { |
| 61 | + end_port = Convert.ToUInt16(parts[1]); |
| 62 | + } catch { |
| 63 | + Console.WriteLine(args[0] + " is not a valid port number"); |
| 64 | + return -1; |
| 65 | + } |
| 66 | + } else { |
| 67 | + end_port = (ushort) (start_port + 1); |
| 68 | + } |
| 69 | + if (parts.Length > 2) { |
| 70 | + try { |
| 71 | + increment = Convert.ToUInt16(parts[2]); |
| 72 | + if (increment < 1) |
| 73 | + throw new Exception(); |
| 74 | + } catch { |
| 75 | + Console.WriteLine("is not a valid increment number"); |
| 76 | + return -1; |
| 77 | + } |
| 78 | + } |
| 79 | + if ((end_port - start_port) / increment > 32) { |
| 80 | + Console.WriteLine("This utility can spawn maximum 32"); |
| 81 | + return -1; |
| 82 | + |
| 83 | + } |
| 84 | + string phpPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\php-cgi.exe"; |
| 85 | + if (!File.Exists(phpPath)) { |
| 86 | + Console.WriteLine("php-cgi not found at '" + phpPath + "', this executable should be in same php folder that you want to run"); |
| 87 | + return 1; |
| 88 | + } |
| 89 | + Console.WriteLine("Executing PHP at " + phpPath); |
| 90 | + |
| 91 | + for (UInt16 port = start_port;port < end_port;port += increment) { |
| 92 | + Console.WriteLine("starting php on port " + port); |
| 93 | + StartPHP(phpPath, port); |
| 94 | + } |
| 95 | + return 0; |
| 96 | + } |
| 97 | +} |
0 commit comments