Example #1
0
        public async Task <string> GetLocalIp()
        {
            Log.Info("开始获取当前网络环境外网IP...");
            List <LocalIpApiListItem> localIpApiList = ConfigManager.Now.LocalIpApiList;

            if (localIpApiList == null || localIpApiList.Count == 0)
            {
                throw new Exception("获取外网IP接口不存在,请先添加获取IP接口。");
            }
            if (!await NetworkTools.NetworkCheck())
            {
                throw new Exception("当前网络故障,无法连接互联网。");
            }
            string ipAddress = null;

            foreach (var item in localIpApiList)
            {
                try
                {
                    (bool, string)result = await ReuqestApi(item);

                    if (result.Item1)
                    {
                        ipAddress = result.Item2;
                        if (!string.IsNullOrEmpty(ipAddress))
                        {
                            Log.Info($"通过接口【{item.Url}】获取IP地址成功,IP地址:{ipAddress}。");
                            break;
                        }
                    }
                    else
                    {
                        Log.Warn(result.Item2);
                    }
                }
                catch (Exception ex)
                {
                    Log.Warn($"请求接口:{item.Url}失败,错误:{ex.Message}");
                }
            }
            if (string.IsNullOrEmpty(ipAddress))
            {
                throw new Exception("获取当前网络外网IP失败。");
            }

            return(ipAddress);
        }
Example #2
0
        /// <summary>
        /// 开始推流
        /// </summary>
        /// <param name="setting"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        private static async Task <bool> StartPublish(User user, LiveSetting setting, string url)
        {
            try
            {
                if (string.IsNullOrEmpty(setting.CmdString))
                {
                    throw new Exception("CMD string can not null.");
                }
                if (setting.CmdString.IndexOf("[[URL]]") < 0)
                {
                    throw new Exception("Cmd args cannot find '[[URL]]' mark.");
                }
                setting.CmdString = setting.CmdString.Replace("[[URL]]", $"\"{url}\"");
                int firstNullChar = setting.CmdString.IndexOf((char)32);
                if (firstNullChar < 0)
                {
                    throw new Exception("Cannot find cmd process name(look like 'ping 127.0.0.1','ping' is process name).");
                }
                string cmdName = setting.CmdString.Substring(0, firstNullChar);
                string cmdArgs = setting.CmdString.Substring(firstNullChar);
                if (string.IsNullOrEmpty(cmdArgs))
                {
                    throw new Exception("Cmd args cannot null.");
                }
                var psi = new ProcessStartInfo
                {
                    FileName  = cmdName,
                    Arguments = cmdArgs,
                    RedirectStandardOutput = true,
                    UseShellExecute        = false,
                    CreateNoWindow         = RuntimeInformation.IsOSPlatform(OSPlatform.Linux),
                };
                bool isAutoRestart = true;
                while (isAutoRestart)
                {
                    isAutoRestart = setting.AutoRestart;
                    while (!await NetworkTools.NetworkCheck())
                    {
                        GlobalSettings.Logger.LogInfo($"Wait for network...");
                        await Task.Delay(3000);
                    }
                    if (!await LiveRoomStateCheck(user))
                    {
                        GlobalSettings.Logger.LogInfo($"Start live failed...");
                        return(false);
                    }
                    StartLiveDataInfo liveInfo = await StartLive(user, setting);

                    if (liveInfo == null)
                    {
                        GlobalSettings.Logger.LogInfo($"Start live failed...");
                        return(false);
                    }
                    //启动
                    var proc = Process.Start(psi);
                    if (proc == null)
                    {
                        throw new Exception("Can not exec set cmd.");
                    }
                    else
                    {
                        //开始读取命令输出
                        using (var sr = proc.StandardOutput)
                        {
                            while (!sr.EndOfStream)
                            {
                                GlobalSettings.Logger.LogInfo(sr.ReadLine());
                            }
                            if (!proc.HasExited)
                            {
                                proc.Kill();
                            }
                        }
                        //退出检测
                        if (!Console.IsInputRedirected)
                        {
                            Console.CancelKeyPress += (object sender, ConsoleCancelEventArgs eventArgs) =>
                            {
                                isAutoRestart = false;
                            };
                        }
                        if (isAutoRestart)
                        {
                            GlobalSettings.Logger.LogInfo($"Cmd exited. Auto restart.");
                        }
                        else
                        {
                            GlobalSettings.Logger.LogInfo($"Cmd exited.");
                        }
                    }
                    if (isAutoRestart)
                    {
                        GlobalSettings.Logger.LogInfo($"Wait for restart...");
                        //如果开启了自动重试,那么等待60s后再次尝试
                        await Task.Delay(60000);
                    }
                }
                return(true);
            }
            catch (Exception ex)
            {
                GlobalSettings.Logger.LogError(ex.Message);
                return(false);
            }
        }