private void FlushProxyStream(bool end) { IBuffer buffer = mProxyStream.GetWriteCacheBufers(); if (buffer != null) { if (Request.Session.SSL) { mProxyStream.Import(buffer); var length = (int)mProxyStream.Length; var data = System.Buffers.ArrayPool <byte> .Shared.Rent(length); mProxyStream.Read(data, 0, length); ProxyDataBuffer proxyData = new ProxyDataBuffer(new ArraySegment <byte>(data, 0, length)); Request.Session.Send(proxyData); } else { Request.Session.Send(buffer); } } if (end) { mProxyStream.Dispose(); } }
/// <summary> /// If the remote IP address is that of a known Acurite Access device, we will sniff whatever data we want from it and proxy the request to Acurite. /// </summary> /// <param name="p"></param> /// <returns></returns> private bool HandleRequestFromAcuriteAccessDevice(HttpProcessor p) { if (Program.settings.GetAcuriteAccessIPs().Contains(p.RemoteIPAddressStr)) { ProxyDataBuffer proxiedDataBuffer = new ProxyDataBuffer(); p.ProxyTo("https://atlasapi.myacurite.com" + p.request_url.PathAndQuery, 30000, true, proxiedDataBuffer); lastAcuriteAccessRequests.Enqueue(proxiedDataBuffer); if (lastAcuriteAccessRequests.Count > 10) { lastAcuriteAccessRequests.TryDequeue(out ProxyDataBuffer removed); } foreach (string str in proxiedDataBuffer.Items.Select(i => i.PayloadAsString)) { Match m = rxGetURL.Match(str); if (m.Success) { SensorBase sensorData = new SensorBase(m.Groups[2].Value); sensorDataCollection[sensorData.UniqueID] = sensorData; List <DataFileTemplate> templates = Program.settings.GetSensorDataTemplates(); foreach (DataFileTemplate template in templates) { if (sensorData.UniqueID == template.UniqueID || sensorData.DeviceName == template.UniqueID) { sensorData.WriteFile(template); } } } } return(true); } return(false); }