protected override void OnStop()
        {
            // TODO: Add code here to perform any tear-down necessary to stop your service.

            m_Proxy.Stop();

            CommonLog.Info("=== " + Program.SVC_NAME + " stopped ===");
        }
        protected override void OnStart(string[] args)
        {
            // TODO: Add code here to start your service.

            CommonLog.Info("=== " + Program.SVC_NAME + " is starting ===");

            m_Proxy.Start();
        }
Exemple #3
0
        public void Start()
        {
            Stop();

            ConfigurationManager.RefreshSection("appSettings");

            var proxySettings  = new Dictionary <int, Dictionary <string, List <string> > >();
            var keys           = ConfigurationManager.AppSettings.AllKeys;
            var inputWhiteList = new List <string>();

            var maxRecvIdleSeconds = 0;

            foreach (var key in keys)
            {
                if (key == "InputWhitelist")
                {
                    var ips = ConfigurationManager.AppSettings[key].ToString().Split(',');
                    foreach (var ip in ips)
                    {
                        inputWhiteList.Add(ip.Trim());
                    }
                    continue;
                }

                if (key == "MaxRecvIdleSeconds")
                {
                    int maxIdleTime = 0;
                    if (Int32.TryParse(ConfigurationManager.AppSettings[key].ToString(), out maxIdleTime))
                    {
                        maxRecvIdleSeconds = maxIdleTime > 0 ? maxIdleTime : 0;
                    }
                    continue;
                }

                var mainParts = key.Split('/');
                if (mainParts == null || mainParts.Length < 2)
                {
                    continue;
                }

                int port = 0;
                if (Int32.TryParse(mainParts[0], out port))
                {
                    try
                    {
                        var reqPath = mainParts[1].Trim();

                        var list    = new List <string>();
                        var targets = ConfigurationManager.AppSettings[key].ToString().Split(',');
                        foreach (var target in targets)
                        {
                            list.Add(target.Trim());
                        }

                        Dictionary <string, List <string> > clients = null;
                        if (proxySettings.ContainsKey(port))
                        {
                            clients = proxySettings[port];
                        }
                        else
                        {
                            clients = new Dictionary <string, List <string> >();
                            proxySettings.Add(port, clients);
                        }

                        if (clients != null)
                        {
                            if (clients.ContainsKey(reqPath))
                            {
                                clients.Remove(reqPath);
                            }
                            clients.Add(reqPath, list);
                        }
                    }
                    catch (Exception ex)
                    {
                        CommonLog.Error("Failed to load config for listening port " + port);
                        CommonLog.Error(ex.ToString());
                    }
                }
            }

            CommonLog.Info("Max receiving idle seconds: " + maxRecvIdleSeconds);

            lock (m_InputWhiteList)
            {
                if (inputWhiteList != null)
                {
                    m_InputWhiteList.Clear();
                    m_InputWhiteList.AddRange(inputWhiteList);
                }
                if (m_InputWhiteList.Count <= 0)
                {
                    m_InputWhiteList.Add("127.0.0.1");
                }
            }

            var ipWhitelist = GetSourceWhitelist();

            CommonLog.Info("Input source whitelist: " + String.Join(", ", ipWhitelist.ToArray()));

            lock (m_Servers)
            {
                foreach (var item in proxySettings)
                {
                    var targets = item.Value;
                    var server  = new BroadcastServer(item.Key, maxRecvIdleSeconds);
                    server.LoadTargets(targets);
                    if (server.Start(ipWhitelist))
                    {
                        m_Servers.Add(item.Key, server);
                        foreach (var target in targets)
                        {
                            CommonLog.Info("Added proxy [" + item.Key + "/" + target.Key + "] => " + String.Join(", ", target.Value.ToArray()));
                        }
                    }
                }
            }
        }
Exemple #4
0
        public static int Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException;

            // start the program ...

            if (Environment.UserInteractive)
            {
                string parameter   = "";
                bool   needDetails = false;

                if (args != null && args.Length >= 1)
                {
                    parameter = args[0];
                    parameter = parameter.Trim();

                    if (parameter == "install" || parameter == "uninstall" ||
                        parameter == "silent-install" || parameter == "silent-uninstall")
                    {
                        if (parameter == "silent-install" || parameter == "silent-uninstall")
                        {
                            if (parameter == "silent-install")
                            {
                                parameter = "install";
                            }
                            else if (parameter == "silent-uninstall")
                            {
                                parameter = "uninstall";
                            }
                        }
                        else
                        {
                            // redirect console output to parent process, normally it should be "cmd"
                            AttachConsole(ATTACH_PARENT_PROCESS);
                            needDetails = true;
                        }

                        string svcName = "";

                        if (args.Length >= 2)
                        {
                            string[] svcNameArgs = new string[args.Length - 1];
                            for (int i = 1; i < args.Length; i++)
                            {
                                svcNameArgs[i - 1] = args[i];
                            }

                            for (int i = 0; i < svcNameArgs.Length; i++)
                            {
                                if (svcName.Length == 0)
                                {
                                    svcName = svcNameArgs[i];
                                }
                                else
                                {
                                    svcName = svcName + " " + svcNameArgs[i];
                                }
                            }

                            svcName = svcName.Trim();
                        }

                        if (svcName.Length > 0)
                        {
                            SVC_NAME = svcName;
                        }
                    }
                    else
                    {
                        parameter = "";
                    }
                }

                parameter = parameter.Trim();
                if (parameter == "install" && SVC_NAME.Length > 0)
                {
                    Console.WriteLine("Start to install service with name [" + SVC_NAME + "]");
                    CommonLog.Info("Installing service...");
                    try
                    {
                        ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
                        CommonLog.Info("OK");
                        Console.WriteLine("Installed service [" + SVC_NAME + "] successfully");
                        if (needDetails)
                        {
                            Console.WriteLine("You might need to press enter to end the process");
                        }
                    }
                    catch (Exception ex)
                    {
                        CommonLog.Error("Error: " + ex.Message);
                        Console.WriteLine("Failed to install service [" + SVC_NAME + "]");
                        if (needDetails)
                        {
                            Console.WriteLine("You might need to press enter to end the process");
                        }
                        return(-1);
                    }
                }
                else if (parameter == "uninstall" && SVC_NAME.Length > 0)
                {
                    Console.WriteLine("Start to uninstall service [" + SVC_NAME + "]");
                    CommonLog.Info("Uninstalling service...");
                    try
                    {
                        ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
                        CommonLog.Info("OK");
                        Console.WriteLine("Uninstalled service [" + SVC_NAME + "] successfully");
                        if (needDetails)
                        {
                            Console.WriteLine("You might need to press enter to end the process");
                        }
                    }
                    catch (Exception ex)
                    {
                        CommonLog.Error("Error: " + ex.Message);
                        //Console.WriteLine("Failed to uninstall service [" + SVC_NAME + "]");
                        if (needDetails)
                        {
                            Console.WriteLine("You might need to press enter to end the process");
                        }
                        return(-1);
                    }
                }
                else // Run as a console app normally ...
                {
                    bool canRun = false;
                    mutex = new Mutex(true, "BroadcastProxyServer.Instance", out canRun);

                    if (!canRun)
                    {
                        MessageBox.Show("Another application instance is already running.");
                    }
                    else
                    {
                        Application.EnableVisualStyles();
                        Application.SetCompatibleTextRenderingDefault(false);
                        Application.Run(new MainForm());
                        Environment.Exit(0);
                    }
                }
            }
            else
            {
                bool canRun = false;
                mutex = new Mutex(true, "BroadcastProxyServer.Instance", out canRun);

                if (!canRun)
                {
                    throw new Exception("Another application instance is already running.");
                }
                else
                {
                    ServiceBase[] ServicesToRun;
                    ServicesToRun = new ServiceBase[]
                    {
                        new ProxyService()
                    };
                    ServiceBase.Run(ServicesToRun);
                }
            }

            return(0);
        }
        private void btnStop_Click(object sender, EventArgs e)
        {
            m_Proxy.Stop();

            CommonLog.Info("=== " + Program.SVC_NAME + " stopped ===");
        }
        private void btnStart_Click(object sender, EventArgs e)
        {
            CommonLog.Info("=== " + Program.SVC_NAME + " is starting ===");

            m_Proxy.Start();
        }