Ejemplo n.º 1
0
        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 ===");
        }
Ejemplo n.º 2
0
        protected override void OnStart(string[] args)
        {
            // TODO: Add code here to start your service.

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

            m_Proxy.Start();
        }
Ejemplo n.º 3
0
        public override void OnConnect(Session session)
        {
            base.OnConnect(session);

            var remoteIp       = session.GetRemoteIp();
            var inputWhiteList = m_Server.GetSourceWhitelist();

            bool isValidAddress = false; // must set whitelist ...

            if (inputWhiteList.Count > 0 && remoteIp.Length > 0)
            {
                foreach (string pattern in inputWhiteList)
                {
                    if (Match(pattern, remoteIp))
                    {
                        isValidAddress = true;
                        break;
                    }
                }
            }

            if (!isValidAddress)
            {
                CommonLog.Error("Stream source IP is not on white list: " + remoteIp);
                session.Close();
                return;
            }

            session.FitReadQueueAction  = Session.ACT_KEEP_OLD;
            session.FitWriteQueueAction = Session.ACT_KEEP_OLD;

            //m_DataFileName = AppDomain.CurrentDomain.BaseDirectory + "/"
            //                    + DateTime.Now.ToString("yyMMddHHmmss") + "_" + m_Random.Next(10, 100) + ".data";

            HttpMessage.GetSessionBuffer(session, true);
            HttpMessage.GetSessionData(session, true);

            HttpMessage.SetSessionData(session, HttpMessage.MSG_KEY_HTTP_HEADER, "");
            HttpMessage.SetSessionData(session, HttpMessage.MSG_KEY_CHANNEL, "");
            HttpMessage.SetSessionData(session, HttpMessage.MSG_KEY_PATH, "");
        }
Ejemplo n.º 4
0
        public bool Start(List <string> inputWhiteList = null)
        {
            Stop();

            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");
                }
            }

            bool isServerOK = false;

            try
            {
                if (m_Server != null)
                {
                    if (m_MaxRecvIdleSeconds > 0)
                    {
                        m_Server.SetIdleTime(Session.IO_RECEIVE, m_MaxRecvIdleSeconds);
                    }
                    m_Server.Start(m_Port);
                    isServerOK = true;
                }
            }
            catch (Exception ex)
            {
                CommonLog.Error("Failed to listen on port: " + m_Port);
                CommonLog.Error(ex.ToString());
            }

            m_Timer = new Timer(TryToKeepTargetsOnline, m_Targets, 1000, 1000 * m_CheckInterval);

            return(isServerOK);
        }
Ejemplo n.º 5
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()));
                        }
                    }
                }
            }
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
0
 private static void CurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
 {
     CommonLog.Error(((Exception)e.ExceptionObject).Message + ((Exception)e.ExceptionObject).InnerException.Message);
 }
Ejemplo n.º 8
0
        public void LoadTargets(Dictionary <string, List <string> > urlsMap)
        {
            var oldTargets = m_Targets;

            foreach (var item in oldTargets)
            {
                foreach (var subItem in item.Value)
                {
                    try
                    {
                        if (subItem.Value != null)
                        {
                            subItem.Value.SetClientId(0);
                            subItem.Value.Disconnect(true);
                        }
                    }
                    catch { }
                }
            }

            var newTargets = new Dictionary <string, Dictionary <string, Client> >();

            int count = 0;
            var paths = urlsMap.Keys;

            foreach (string path in paths)
            {
                Dictionary <string, Client> clients = null;
                if (newTargets.ContainsKey(path))
                {
                    clients = newTargets[path];
                }
                else
                {
                    clients = new Dictionary <string, Client>();
                    newTargets.Add(path, clients);
                }

                var urls = urlsMap[path];
                foreach (var url in urls)
                {
                    try
                    {
                        if (url != null && url.Length > 0)
                        {
                            count++;
                            var address = url.StartsWith("http://") ? url : "http://" + url;
                            var uri     = new Uri(address, UriKind.Absolute);
                            var client  = new Client();
                            client.SetClientId(count);
                            client.SetIoFilter(new ClientDataCodec());
                            client.SetIoHandler(new ClientNetworkEventHandler(this));
                            client.Connect(uri.Host, uri.Port);
                            clients.Add(url, client);
                        }
                    }
                    catch (Exception ex)
                    {
                        CommonLog.Error("Failed to load target URL: " + m_Port + "/" + path + " => " + url);
                        CommonLog.Error(ex.ToString());
                    }
                }
            }

            m_Targets = newTargets;
        }
Ejemplo n.º 9
0
        private void btnStop_Click(object sender, EventArgs e)
        {
            m_Proxy.Stop();

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

            m_Proxy.Start();
        }
Ejemplo n.º 11
0
 private void MainForm_Load(object sender, EventArgs e)
 {
     CommonLog.SetGuiControl(this, mmLog);
 }