static int Main(string[] args) { ShowVersion(); if (1 > args.Length || 5 < args.Length) { return ShowUsage(); } if (_bIsEnableLog) { Console.WriteLine("Press any key to exit..."); Console.WriteLine(); } int ndx=0; string strMrl = args[ndx++]; string strFilePath = (args.Length > ndx)?args[ndx++]:string.Empty; string strSize = (args.Length > ndx) ? args[ndx++] : string.Empty; IPAddress mifaceIP = IPAddress.None; if (args.Length > ndx) { string strStr = args[ndx++]; if (strStr.Length > 0) { mifaceIP = IPAddress.Parse(strStr); } } string strCaUri = "http://localhost:7781/ca/";// = string.Empty; if (args.Length > ndx) { strCaUri = args[ndx++]; } DateTime stopDump = new DateTime(0); ndx = strSize.IndexOf(':'); if (ndx >= 0) { string strTime = strSize.Substring(ndx + 1); strSize = strSize.Substring(0, ndx); if (strTime.Length > 0 && '.' == strTime[0]) { stopDump = DateTime.Now.AddMinutes(double.Parse(strTime.Substring(1))); } else { stopDump = DateTime.Parse(strTime); } } CultureInfo ci = CultureInfo.InvariantCulture;//new CultureInfo("en-US"); Int64 sizeMax=0; if (strSize.Length > 0) { if (strSize.EndsWith("G", true, ci)) { sizeMax = Int64.Parse(strSize.TrimEnd(new Char[] { 'G', 'g' })); sizeMax <<= 30; } else { sizeMax = Int64.Parse(strSize); sizeMax <<= 20; } } bool bFileStdOutput = false; if (_bIsFileLog) { _bIsEnableLog = !(bFileStdOutput = (0 == strFilePath.Length || "-" == strFilePath)); } else if (0 == strFilePath.Length) { Uri uri = new Uri(strMrl); strFilePath = uri.Segments[uri.Segments.Length-1]; } LibVLCModule moduleVLC=null; WebClient httpClientSrc = null; WebClient httpClientDst = null; int iExitCode = 0; try { string strFileName = strMrl; ndx = strMrl.IndexOf('?'); if(ndx > 0) { strFileName = strMrl.Substring(0, ndx); } if (strFileName.EndsWith(".m3u")) { WebClient httpClient = new WebClient(); httpClient.Headers.Add(HttpRequestHeader.UserAgent, GetUserAgentString()); using (StreamReader readerM3U = new StreamReader(httpClient.OpenRead(strMrl))) { string strLine; while (null != (strLine = readerM3U.ReadLine()) && !strLine.StartsWith("#EXTINF:")) ; if (null != strLine && null != (strLine = readerM3U.ReadLine())) { strMrl = strLine; if (strFilePath.EndsWith(".m3u")) { strFilePath = strFilePath.Substring(0, strFilePath.Length - 4) + ".ts"; } } } } if (strMrl.StartsWith("udp:", true, ci) || strMrl.StartsWith("ca:", true, ci)) { if (bFileStdOutput) { throw new Exception("Dump not supported for standard output, use http source."); } if (strFilePath.StartsWith(Uri.UriSchemeHttp) || strFilePath.StartsWith(Uri.UriSchemeHttps)) { throw new Exception("Dump not supported from udp source to http destination."); } using (Socket sockFake = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) { moduleVLC = new LibVLCModule(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), strCaUri, mifaceIP); Thread.Sleep(4000); //Console.WriteLine("Press any key to continue..."); //Console.ReadKey(); } } else { httpClientSrc = new WebClient(); httpClientSrc.Headers.Add(HttpRequestHeader.UserAgent, GetUserAgentString()); //Console.WriteLine(strFilePath); if (strFilePath.StartsWith(Uri.UriSchemeHttp) || strFilePath.StartsWith(Uri.UriSchemeHttps)) { Uri uri = new Uri(strFilePath); string[] auth = new string[] { string.Empty, string.Empty }; if (null != uri.UserInfo) { auth = uri.UserInfo.Split(new char[] { ':' }); } httpClientDst = new WebClientEx(auth[0], auth[1]); httpClientDst.Headers.Add(HttpRequestHeader.UserAgent, GetUserAgentString()); } } byte[] buffer = new byte[0x4000]; using ( Stream streamSrc = (null != httpClientSrc) ? httpClientSrc.OpenRead(strMrl) : moduleVLC.NewAccess(strMrl), streamDst = (null != httpClientDst) ? httpClientDst.OpenWrite(strFilePath, "PUT") : (bFileStdOutput) ? Console.OpenStandardOutput() : File.Open(strFilePath, FileMode.OpenOrCreate) ) { IPTVDump dump = new IPTVDump(strMrl, stopDump, sizeMax); Int64 sizeDumped = 0; int iLength; while ((iLength = streamSrc.Read(buffer, 0, buffer.Length)) > 0) { streamDst.Write(buffer, 0, iLength); sizeDumped += iLength; bool bIsLast = !dump.IsContinue(sizeDumped); dump.WriteLogMessage(sizeDumped, bIsLast); if (Console.KeyAvailable || bIsLast) { break; } } } } catch(Exception e) { Console.Error.WriteLine("Dump failed, error: {0}", e.Message); iExitCode = 1; } if (null != moduleVLC) { moduleVLC.LibVLCRelease(); } return iExitCode; }
protected void Start(IPAddress mifaceIP) { ServicePointManager.ServerCertificateValidationCallback = (obj, certificate, chain, errors) => true; ProcessTuneSection process = ProcessTuneSection.GetSection(); if (null != process && process.HighPriority) { Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High; } PrintConfiguration(mifaceIP); _listener = new TcpListener(_Address, _iPort); _listener.Start(); #if NET_45_OR_GREATER AcceptSocketAsync(_listener); #else _listener.BeginAcceptSocket(OnAcceptNewClient, _listener); #endif // NET_45_OR_GREATER Console.WriteLine("Listening, hit enter to stop"); Console.WriteLine(); CAWebClient.CAAuthData auth = CAWebClient.CAAuthData.GetSection(); _moduleVLC = new LibVLCModule( Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), (null == auth) ? string.Format("http://localhost:{0}/ca/", _iPort) : auth.CaUri, mifaceIP ); string strBestMac = _moduleVLC.GetBestMacAddress(); if (strBestMac.Length > 0) { MacAddress = strBestMac; } if (null != process && process.UseAuxLowPriorityThreadPool) { _lowThreadPool = new LowPriorityThreadPool(); } }