diff --git a/RZ.Cache/RZCache/Controllers/ValuesController.cs b/RZ.Cache/RZCache/Controllers/ValuesController.cs index 4ad975b..91cda98 100644 --- a/RZ.Cache/RZCache/Controllers/ValuesController.cs +++ b/RZ.Cache/RZCache/Controllers/ValuesController.cs @@ -28,7 +28,7 @@ public string get() { string sVersion = Assembly.GetEntryAssembly().GetCustomAttribute().InformationalVersion; - return "RuckZuck-Proxy (c) 2017 by Roger Zander; Version: " + sVersion; + return "RuckZuck-Proxy (c) 2018 by Roger Zander; Version: " + sVersion; } @@ -69,21 +69,37 @@ public RZController(IConfiguration config, IMemoryCache memoryCache) RuckZuck_WCF.RZRestProxy.sURL = config.GetSection("ParentServer").Value ?? _config.GetSection("RuckZuck:ParentServer").Value; RuckZuck_WCF.RZRestProxy.CatalogTTL = int.Parse(_config.GetSection("CatalogTTL").Value ?? _config.GetSection("RuckZuck:CatalogTTL").Value); RuckZuck_WCF.RZRestProxy.localURL = config.GetSection("localURL").Value ?? _config.GetSection("RuckZuck:localURL").Value; + RuckZuck_WCF.RZRestProxy.ipfs_GW_URL = config.GetSection("IPFS_GW_URL").Value ?? _config.GetSection("RuckZuck:ipfs_GW_URL").Value; RuckZuck_WCF.RZRestProxy._cache = _cache; RuckZuck_WCF.RZRestProxy.Proxy = config.GetSection("Proxy").Value ?? _config.GetSection("RuckZuck:Proxy").Value; - RuckZuck_WCF.RZRestProxy.ProxyUserPW = config.GetSection("ProxyUserPW ").Value ?? _config.GetSection("RuckZuck:ProxyUserPW").Value; + RuckZuck_WCF.RZRestProxy.ProxyUserPW = config.GetSection("ProxyUserPW").Value ?? _config.GetSection("RuckZuck:ProxyUserPW").Value; + if (RuckZuck_WCF.RZRestProxy.RedirectToIPFS == false) + { + RuckZuck_WCF.RZRestProxy.UseIPFS = int.Parse(_config.GetSection("UseIPFS").Value ?? _config.GetSection("RuckZuck:UseIPFS").Value); + } + } [Route("AuthenticateUser")] public ActionResult AuthenticateUser() { - RuckZuck_WCF.RZRestProxy.contentType = (string)Request.Headers["Accept"] ?? "application/xml"; + RuckZuck_WCF.RZRestProxy.contentType = (string)Request.Headers["Accept"] ?? "application/json"; + if(RuckZuck_WCF.RZRestProxy.contentType == "*/*") + { + RuckZuck_WCF.RZRestProxy.contentType = "application/json"; + } string Username = _config.GetSection("RZUser").Value ?? Request.Headers["Username"]; string Password = _config.GetSection("RZPW").Value ?? Request.Headers["Password"]; if (string.IsNullOrEmpty(Username)) { - return Content("deecdc6b-ad08-42ab-a743-a3c0f9033c80", "text/xml"); ; + Username = Request.Headers["Username"]; + Password = Request.Headers["Password"]; + } + + if (string.IsNullOrEmpty(Username)) + { + return Content("", "text/xml"); } else { @@ -107,7 +123,7 @@ public ActionResult AuthenticateUser() } else { - return Content(RuckZuck_WCF.RZRestProxy.GetAuthToken(Username, Password), "text/xml") ; + return Content(RuckZuck_WCF.RZRestProxy.GetAuthToken(Username, Password), "text/xml"); ; } } @@ -118,7 +134,9 @@ public ActionResult AuthenticateUser() [Route("SWResults/{search}")] public ActionResult SWResults(string search = "") { - RuckZuck_WCF.RZRestProxy.contentType = (string)Request.Headers["Accept"] ?? "application/xml"; + //RuckZuck_WCF.RZRestProxy.contentType = (string)Request.Headers["Accept"] ?? "application/json"; + //RuckZuck_WCF.RZRestProxy.contentType = "application/json"; + string sRes = RuckZuck_WCF.RZRestProxy.SWResults(search); return Content(sRes, "text/xml"); } @@ -128,7 +146,8 @@ public ActionResult SWResults(string search = "") [Route("SWGetShort/{name}")] public ActionResult SWGet(string name = "") { - RuckZuck_WCF.RZRestProxy.contentType = (string)Request.Headers["Accept"] ?? "application/xml"; + //RuckZuck_WCF.RZRestProxy.contentType = (string)Request.Headers["Accept"] ?? "application/json"; + RuckZuck_WCF.RZRestProxy.contentType = "application/json"; return Content(RuckZuck_WCF.RZRestProxy.SWGet(name), "text/xml"); } @@ -137,7 +156,8 @@ public ActionResult SWGet(string name = "") [Route("SWGet/{name}/{ver}")] public ActionResult SWGet(string name = "", string ver = "") { - RuckZuck_WCF.RZRestProxy.contentType = (string)Request.Headers["Accept"] ?? "application/xml"; + //RuckZuck_WCF.RZRestProxy.contentType = (string)Request.Headers["Accept"] ?? "application/json"; + RuckZuck_WCF.RZRestProxy.contentType = "application/json"; return Content(RuckZuck_WCF.RZRestProxy.SWGet(name, ver), "text/xml"); } @@ -146,7 +166,8 @@ public ActionResult SWGet(string name = "", string ver = "") [Route("SWGet/{name}/{manuf}/{ver}")] public ActionResult SWGet(string name = "", string manuf = "", string ver = "") { - RuckZuck_WCF.RZRestProxy.contentType = (string)Request.Headers["Accept"] ?? "application/xml"; + //RuckZuck_WCF.RZRestProxy.contentType = (string)Request.Headers["Accept"] ?? "application/json"; + RuckZuck_WCF.RZRestProxy.contentType = "application/json"; return Content(RuckZuck_WCF.RZRestProxy.SWGet(name, manuf, ver), "text/xml"); } @@ -155,7 +176,8 @@ public ActionResult SWGet(string name = "", string manuf = "", string ver = "") [Route("GetSWDefinition/{name}/{ver}/{man}")] public ActionResult GetSWDefinition(string name = "", string ver = "", string man = "") { - RuckZuck_WCF.RZRestProxy.contentType = (string)Request.Headers["Accept"] ?? "application/xml"; + //RuckZuck_WCF.RZRestProxy.contentType = (string)Request.Headers["Accept"] ?? "application/json"; + RuckZuck_WCF.RZRestProxy.contentType = "application/json"; return Content(RuckZuck_WCF.RZRestProxy.GetSWDefinitions(name, ver, man), "text/xml"); } @@ -164,7 +186,8 @@ public ActionResult GetSWDefinition(string name = "", string ver = "", string ma [Route("Feedback/{name}/{ver}/{man}/{arch}/{ok}/{user}/{text}")] public ActionResult Feedback(string name, string ver, string man = "", string arch = "", string ok = "", string user = "", string text = "") { - RuckZuck_WCF.RZRestProxy.contentType = (string)Request.Headers["Accept"] ?? "application/xml"; + //RuckZuck_WCF.RZRestProxy.contentType = (string)Request.Headers["Accept"] ?? "application/json"; + RuckZuck_WCF.RZRestProxy.contentType = "application/json"; return Content(RuckZuck_WCF.RZRestProxy.Feedback(name, ver, man, arch, ok, user, text).Result, "text/xml"); } @@ -176,22 +199,13 @@ public Stream GetIcon(Int32 id = 575633) return RuckZuck_WCF.RZRestProxy.GetIcon(id); } - - [HttpGet] - [Route("TrackDownloads")] - [Route("TrackDownloads/{id}")] - public void TrackDownloads(string id) - { - RuckZuck_WCF.RZRestProxy.contentType = (string)Request.Headers["Accept"] ?? "application/xml"; - RuckZuck_WCF.RZRestProxy.TrackDownloads(id); - } - [HttpGet] [Route("TrackDownloadsNew")] [Route("TrackDownloadsNew/{SWId}/{arch}")] public void TrackDownloadsNew(string SWId, string arch) { - RuckZuck_WCF.RZRestProxy.contentType = (string)Request.Headers["Accept"] ?? "application/xml"; + //RuckZuck_WCF.RZRestProxy.contentType = (string)Request.Headers["Accept"] ?? "application/json"; + RuckZuck_WCF.RZRestProxy.contentType = "application/json"; RuckZuck_WCF.RZRestProxy.TrackDownloadsNew(SWId, arch); } @@ -199,7 +213,8 @@ public void TrackDownloadsNew(string SWId, string arch) [Route("CheckForUpdateXml")] public ActionResult CheckForUpdateXml() { - RuckZuck_WCF.RZRestProxy.contentType = (string)Request.Headers["Accept"] ?? "application/xml"; + //RuckZuck_WCF.RZRestProxy.contentType = (string)Request.Headers["Accept"] ?? "application/json"; + RuckZuck_WCF.RZRestProxy.contentType = "application/json"; var oGet = new StreamReader(Request.Body).ReadToEndAsync(); return Content(RuckZuck_WCF.RZRestProxy.CheckForUpdate(oGet.Result.ToString()), "text/xml"); @@ -209,7 +224,8 @@ public ActionResult CheckForUpdateXml() [Route("CheckForUpdate")] public ActionResult CheckForUpdate() { - RuckZuck_WCF.RZRestProxy.contentType = (string)Request.Headers["Accept"] ?? "application/xml"; + //RuckZuck_WCF.RZRestProxy.contentType = (string)Request.Headers["Accept"] ?? "application/json"; + RuckZuck_WCF.RZRestProxy.contentType = "application/json"; var oGet = new StreamReader(Request.Body).ReadToEndAsync(); return Content(RuckZuck_WCF.RZRestProxy.CheckForUpdate(oGet.Result.ToString()), "text/xml"); @@ -219,7 +235,8 @@ public ActionResult CheckForUpdate() [Route("UploadSWEntry")] public bool UploadSWEntry() { - RuckZuck_WCF.RZRestProxy.contentType = (string)Request.Headers["Accept"] ?? "application/xml"; + //RuckZuck_WCF.RZRestProxy.contentType = (string)Request.Headers["Accept"] ?? "application/json"; + RuckZuck_WCF.RZRestProxy.contentType = "application/json"; var oGet = new StreamReader(Request.Body).ReadToEndAsync(); return RuckZuck_WCF.RZRestProxy.UploadSWEntry(oGet.Result.ToString()); diff --git a/RZ.Cache/RZCache/Dockerfile b/RZ.Cache/RZCache/Dockerfile index 2013c7f..0a549fb 100644 --- a/RZ.Cache/RZCache/Dockerfile +++ b/RZ.Cache/RZCache/Dockerfile @@ -3,12 +3,26 @@ ARG source WORKDIR /app EXPOSE 5000:5000/tcp EXPOSE 5001:5001/udp +EXPOSE 4001:4001 +EXPOSE 4002:4002/udp +EXPOSE 5002:5002/tcp +EXPOSE 8080:8080 +EXPOSE 8081:8081 ENTRYPOINT ["dotnet", "RZCache.dll"] COPY ${source:-obj/Docker/publish} . +#RUN tar xvfz /app/wwwroot/go-ipfs_v0.4.13_linux-amd64.tar.gz +#RUN mv go-ipfs/ipfs /usr/local/bin/ipfs +RUN rm /app/wwwroot/go-ipfs_v0.4.13_linux-amd64.tar.gz ENV localURL "https://rzproxy.azurewebsites.net" ENV RZUser "" ENV RZPW "" ENV ParentServer "https://ruckzuck.azurewebsites.net/wcf/RZService.svc" ENV CatalogTTL "4" ENV Proxy "" -ENV ProxyUserPW "" \ No newline at end of file +ENV ProxyUserPW "" +ENV UseIPFS "0" +ENV WebPort "5000" +ENV UDPPort "5001" +ENV IPFS_PATH "/app/.ipfs" +ENV IPFS_LOGGING "" +#ENV IPFS_GW_URL "https://gateway.ipfs.io/ipfs" \ No newline at end of file diff --git a/RZ.Cache/RZCache/Program.cs b/RZ.Cache/RZCache/Program.cs index 8904238..4fe4a2c 100644 --- a/RZ.Cache/RZCache/Program.cs +++ b/RZ.Cache/RZCache/Program.cs @@ -8,6 +8,8 @@ using System.Net.Sockets; using System.Net; using System.Text; +using System.Diagnostics; +using System.Threading; namespace RZWCF { @@ -15,7 +17,40 @@ public class Program { public static void Main(string[] args) { - var Server = new UdpClient(5001); + var Server = new UdpClient(int.Parse(Environment.GetEnvironmentVariable("UDPPort") ?? "5001")); + + if (Environment.GetEnvironmentVariable("UseIPFS") == "1") + { + try + { + if (!Directory.Exists("/app/.ipfs")) + { + var oInit = Process.Start("ipfs", @"init"); //>/dev/null 2>/dev/null + oInit.StartInfo.RedirectStandardOutput = false; + oInit.WaitForExit(5000); + Thread.Sleep(3000); + } + Process.Start("ipfs", "config --json Experimental.FilestoreEnabled true").WaitForExit(); + Process.Start("ipfs", "config Addresses.API /ip4/0.0.0.0/tcp/5002").WaitForExit(); + Process.Start("ipfs", "config Addresses.Gateway /ip4/0.0.0.0/tcp/8080").WaitForExit(); + //Process.Start("ipfs", "config --json API.HTTPHeaders.Access-Control-Allow-Origin '[\"*\"]'").WaitForExit(); ; + //Process.Start("ipfs", "config --json API.HTTPHeaders.Access-Control-Allow-Methods '[\"PUT\",\"GET\",\"POST\"]'").WaitForExit(); + //Process.Start("ipfs", "add -r --nocopy /app/wwwroot/files").WaitForExit(); + + Thread.Sleep(2000); + Process.Start("ipfs", "daemon").WaitForExit(7000); + Thread.Sleep(2000); + + RuckZuck_WCF.RZRestProxy.IPFSAdd("/app/RZCache.dll"); + + } + catch (Exception ex) + { + RuckZuck_WCF.RZRestProxy.RedirectToIPFS = true; + RuckZuck_WCF.RZRestProxy.UseIPFS = 0; + Console.WriteLine("IPFS ERROR:" + ex.Message); + } + } if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("localURL"))) { @@ -42,7 +77,7 @@ public static void Main(string[] args) .UseStartup() //.UseApplicationInsights() .UseWebRoot("wwwroot") - .UseUrls("http://*:5000") + .UseUrls("http://*:" + Environment.GetEnvironmentVariable("WebPort") ?? "5000") .Build(); host.Run(); } diff --git a/RZ.Cache/RZCache/RZCache.csproj b/RZ.Cache/RZCache/RZCache.csproj index caf4a88..ec15c31 100644 --- a/RZ.Cache/RZCache/RZCache.csproj +++ b/RZ.Cache/RZCache/RZCache.csproj @@ -3,19 +3,22 @@ netcoreapp2.0 ..\docker-compose.dcproj - 1.0.1.0 - 1.0.1.0 + 1.0.3.0 + 1.0.3.0 Roger Zander Zander Tools RZWCF Proxy Service RuckZuck.tool Proxy and Caching Service - Copyright (c) 2017 by Roger Zander + Copyright (c) 2018 by Roger Zander https://github.com/rzander/ruckzuck https://github.com/rzander/ruckzuck/blob/master/LICENSE.md software package manager windows cache proxy 3d943e5a-c0e9-40f0-b91e-9ab682223099 - 1.0.1 + 1.0.3 false + RZWCF.Program + + Exe @@ -33,7 +36,9 @@ - + + + diff --git a/RZ.Cache/RZCache/RZRestProxy.cs b/RZ.Cache/RZCache/RZRestProxy.cs index 84b4bb0..b459235 100644 --- a/RZ.Cache/RZCache/RZRestProxy.cs +++ b/RZ.Cache/RZCache/RZRestProxy.cs @@ -15,6 +15,7 @@ using static RuckZuck_WCF.RZRestProxy; using System.Threading; using Microsoft.Extensions.Caching.Memory; +using System.Diagnostics; namespace RuckZuck_WCF { @@ -28,8 +29,13 @@ public static class RZRestProxy public static int CatalogTTL = 1; public static string contentType = "application/json"; public static string localURL = "http://localhost:5000"; + public static string ipfs_GW_URL = "https://gateway.ipfs.io/ipfs"; public static string Proxy = ""; public static string ProxyUserPW = ""; + public static int UseIPFS = 0; + public static bool RedirectToIPFS = false; + private static HttpClient oClient = new HttpClient(); + private static HttpClientHandler handler; private static byte[] bProxyUser { @@ -44,48 +50,63 @@ public static string GetAuthToken(string Username, string Password) try { - if (!_cache.TryGetValue("PW" + (Username + Password).GetHashCode(StringComparison.InvariantCultureIgnoreCase), out Token)) + + if (handler == null) { - using (HttpClientHandler handler = new HttpClientHandler()) - { - if (!string.IsNullOrEmpty(Proxy)) - { - handler.Proxy = new WebProxy(Proxy, true); - handler.UseProxy = true; - } + handler = new HttpClientHandler(); + if (!string.IsNullOrEmpty(Proxy)) + { + handler.Proxy = new WebProxy(Proxy, true); + handler.UseProxy = true; + } - using (var oClient = new HttpClient(handler)) - { - if (!string.IsNullOrEmpty(ProxyUserPW)) - { - oClient.DefaultRequestHeaders.ProxyAuthorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(bProxyUser)); - } - oClient.DefaultRequestHeaders.Add("Username", Username); - oClient.DefaultRequestHeaders.Add("Password", Password); - oClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); - var response = oClient.GetStringAsync(sURL + "/rest/AuthenticateUser"); - response.Wait(7000); - if (response.IsCompleted) - { - Token = response.Result.Replace("\"", ""); + handler.AllowAutoRedirect = true; + handler.MaxAutomaticRedirections = 5; + handler.CheckCertificateRevocationList = false; + handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; }; //To prevent Issue with FW + oClient = new HttpClient(handler); + } - // Set cache options. - var cacheEntryOptions = new MemoryCacheEntryOptions() - // Keep in cache for this time, reset time if accessed. - .SetSlidingExpiration(TimeSpan.FromSeconds(300)); + if (!_cache.TryGetValue("PW" + (Username + Password).GetHashCode(StringComparison.InvariantCultureIgnoreCase), out Token)) + { + oClient.DefaultRequestHeaders.Clear(); + oClient.DefaultRequestHeaders.Accept.Clear(); - _cache.Set("PW" + (Username + Password).GetHashCode(StringComparison.InvariantCultureIgnoreCase), Token, cacheEntryOptions); + if (!string.IsNullOrEmpty(ProxyUserPW)) + { + oClient.DefaultRequestHeaders.ProxyAuthorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(bProxyUser)); + } + oClient.DefaultRequestHeaders.Add("Username", Username); + oClient.DefaultRequestHeaders.Add("Password", Password); + oClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType)); + var response = oClient.GetStringAsync(sURL + "/rest/AuthenticateUser"); + response.Wait(15000); + if (response.IsCompleted) + { + Token = response.Result.Replace("\"", ""); + if (!string.IsNullOrEmpty(Token)) + { + // Set cache options. + var cacheEntryOptions = new MemoryCacheEntryOptions() + // Keep in cache for this time, reset time if accessed. + .SetSlidingExpiration(TimeSpan.FromSeconds(300)); - return Token; - } + _cache.Set("PW" + (Username + Password).GetHashCode(StringComparison.InvariantCultureIgnoreCase), Token, cacheEntryOptions); + oClient.DefaultRequestHeaders.Add("AuthenticatedToken", Token); + oClient.DefaultRequestHeaders.Remove("Username"); + oClient.DefaultRequestHeaders.Remove("Password"); + return Token; } } } else + { + //oClient.DefaultRequestHeaders.Add("AuthenticatedToken", Token); return Token; + } } - catch(Exception ex) + catch (Exception ex) { Console.WriteLine("Auth. Error: " + ex.Message); } @@ -96,16 +117,15 @@ public static string GetAuthToken(string Username, string Password) public static string SWResults(string Searchstring) { - string sCatFile = @"wwwroot/rzcat.xml"; + string sCatFile = @"wwwroot/rzcat.json"; string sResult = ""; - if (contentType.ToLower() == "application/json") + + if (_cache.TryGetValue("SWResult-" + Searchstring, out sResult)) { - if (_cache.TryGetValue("SWResult-" + Searchstring, out sResult)) - { - return sResult; - } - sCatFile = @"wwwroot/rzcat.json"; + return sResult; } + sCatFile = @"wwwroot/rzcat.json"; + try { @@ -116,10 +136,13 @@ public static string SWResults(string Searchstring) if (CatalogTTL == 0 || DateTime.Now.ToUniversalTime() - File.GetCreationTime(sCatFile).ToUniversalTime() <= new TimeSpan(CatalogTTL, 0, 1)) { sResult = File.ReadAllText(sCatFile); - var cacheEntryOptions = new MemoryCacheEntryOptions() - .SetSlidingExpiration(TimeSpan.FromSeconds(60)); - _cache.Set("SWResult-" + Searchstring, sResult, cacheEntryOptions); - return sResult; + if (sResult.StartsWith("[") & sResult.Length > 64) //check if it's JSON + { + var cacheEntryOptions = new MemoryCacheEntryOptions() + .SetSlidingExpiration(TimeSpan.FromSeconds(60)); + _cache.Set("SWResult-" + Searchstring, sResult, cacheEntryOptions); + return sResult; + } } } } @@ -128,41 +151,31 @@ public static string SWResults(string Searchstring) } - using (var handler = new HttpClientHandler()) + //oClient.DefaultRequestHeaders.Remove("AuthenticatedToken"); //Remove existing Token + //oClient.DefaultRequestHeaders.Add("AuthenticatedToken", Token); + //oClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType)); + oClient.DefaultRequestHeaders.Accept.Clear(); + oClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + var response = oClient.GetStringAsync(sURL + "/rest/SWResults?search=" + Searchstring); + response.Wait(10000); //10s max. + if (response.IsCompleted) { - //handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; }; //To prevent Issue with FW - - if (!string.IsNullOrEmpty(Proxy)) + sResult = response.Result; + if (sResult.StartsWith('[') & sResult.Length > 64) { - handler.Proxy = new WebProxy(Proxy, true); - handler.UseProxy = true; - } + // Set cache options. + var cacheEntryOptions = new MemoryCacheEntryOptions() + .SetSlidingExpiration(TimeSpan.FromSeconds(60)); + _cache.Set("SWResult-" + Searchstring, sResult, cacheEntryOptions); - using (var oClient = new HttpClient(handler)) - { - if (!string.IsNullOrEmpty(ProxyUserPW)) - { - oClient.DefaultRequestHeaders.ProxyAuthorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(bProxyUser)); - } - oClient.DefaultRequestHeaders.Add("AuthenticatedToken", Token); - oClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType)); - var response = oClient.GetStringAsync(sURL + "/rest/SWResults?search=" + Searchstring); - response.Wait(10000); //10s max. - if (response.IsCompleted) - { - sResult = response.Result; - // Set cache options. - var cacheEntryOptions = new MemoryCacheEntryOptions() - .SetSlidingExpiration(TimeSpan.FromSeconds(60)); - _cache.Set("SWResult-" + Searchstring, sResult, cacheEntryOptions); + if (string.IsNullOrEmpty(Searchstring)) + File.WriteAllText(sCatFile, sResult); - if (string.IsNullOrEmpty(Searchstring)) - File.WriteAllText(sCatFile, sResult); - - return sResult; - } + return sResult; } } + + } catch (Exception ex) { @@ -183,38 +196,32 @@ public static string SWGet(string Shortname) string sResult = ""; if (!_cache.TryGetValue("SWGET1-" + Shortname.GetHashCode(StringComparison.InvariantCultureIgnoreCase), out sResult)) { - using (var handler = new HttpClientHandler()) + + try { - if (!string.IsNullOrEmpty(Proxy)) + //oClient.DefaultRequestHeaders.Add("AuthenticatedToken", Token); + //oClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType)); + oClient.DefaultRequestHeaders.Accept.Clear(); + oClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + var response = oClient.GetStringAsync(sURL + "/rest/SWGetShort?name=" + WebUtility.UrlEncode(Shortname)); + response.Wait(15000); + if (response.IsCompleted) { - handler.Proxy = new WebProxy(Proxy, true); - handler.UseProxy = true; - } - using (var oClient = new HttpClient(handler)) - { - try - { - oClient.DefaultRequestHeaders.Add("AuthenticatedToken", Token); - oClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType)); - var response = oClient.GetStringAsync(sURL + "/rest/SWGetShort?name=" + WebUtility.UrlEncode(Shortname)); - response.Wait(5000); - if (response.IsCompleted) - { - sResult = response.Result; + sResult = response.Result; - // Set cache options. - var cacheEntryOptions = new MemoryCacheEntryOptions() - .SetSlidingExpiration(TimeSpan.FromSeconds(330)); + if (sResult.StartsWith('[') & sResult.Length > 64) + { + // Set cache options. + var cacheEntryOptions = new MemoryCacheEntryOptions() + .SetSlidingExpiration(TimeSpan.FromSeconds(330)); - _cache.Set("SWGET1-" + Shortname.GetHashCode(StringComparison.InvariantCultureIgnoreCase), sResult, cacheEntryOptions); + _cache.Set("SWGET1-" + Shortname.GetHashCode(StringComparison.InvariantCultureIgnoreCase), sResult, cacheEntryOptions); - return sResult; - } + return sResult; } - catch { } } } - + catch { } } return sResult; @@ -227,30 +234,24 @@ public static string SWGet(string PackageName, string PackageVersion) { try { - using (var handler = new HttpClientHandler()) + //oClient.DefaultRequestHeaders.Add("AuthenticatedToken", Token); + //oClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType)); + oClient.DefaultRequestHeaders.Accept.Clear(); + oClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + var response = oClient.GetStringAsync(sURL + "/rest/SWGet?name=" + WebUtility.UrlEncode(PackageName) + "&ver=" + PackageVersion); + response.Wait(15000); + if (response.IsCompleted) { - if (!string.IsNullOrEmpty(Proxy)) - { - handler.Proxy = new WebProxy(Proxy, true); - handler.UseProxy = true; - } - using (var oClient = new HttpClient(handler)) - { - oClient.DefaultRequestHeaders.Add("AuthenticatedToken", Token); - oClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType)); - var response = oClient.GetStringAsync(sURL + "/rest/SWGet?name=" + WebUtility.UrlEncode(PackageName) + "&ver=" + PackageVersion); - response.Wait(5000); - if (response.IsCompleted) - { - sResult = response.Result; + sResult = response.Result; - // Set cache options. - var cacheEntryOptions = new MemoryCacheEntryOptions() - .SetSlidingExpiration(TimeSpan.FromSeconds(330)); - sResult = response.Result; - _cache.Set("SWGET2-" + (PackageName + PackageVersion).GetHashCode(StringComparison.InvariantCultureIgnoreCase), sResult, cacheEntryOptions); - return sResult; - } + if (sResult.StartsWith('[') & sResult.Length > 64) + { + // Set cache options. + var cacheEntryOptions = new MemoryCacheEntryOptions() + .SetSlidingExpiration(TimeSpan.FromSeconds(330)); + sResult = response.Result; + _cache.Set("SWGET2-" + (PackageName + PackageVersion).GetHashCode(StringComparison.InvariantCultureIgnoreCase), sResult, cacheEntryOptions); + return sResult; } } } @@ -267,30 +268,25 @@ public static string SWGet(string PackageName, string Manufacturer, string Packa { try { - using (var handler = new HttpClientHandler()) + + //oClient.DefaultRequestHeaders.Add("AuthenticatedToken", Token); + //oClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType)); + oClient.DefaultRequestHeaders.Accept.Clear(); + oClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + var response = oClient.GetStringAsync(sURL + "/rest/SWGetPkg?name=" + WebUtility.UrlEncode(PackageName) + "&manuf=" + WebUtility.UrlEncode(Manufacturer) + "&ver=" + PackageVersion); + response.Wait(5000); + if (response.IsCompleted) { - if (!string.IsNullOrEmpty(Proxy)) - { - handler.Proxy = new WebProxy(Proxy, true); - handler.UseProxy = true; - } - using (var oClient = new HttpClient(handler)) - { - oClient.DefaultRequestHeaders.Add("AuthenticatedToken", Token); - oClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType)); - var response = oClient.GetStringAsync(sURL + "/rest/SWGetPkg?name=" + WebUtility.UrlEncode(PackageName) + "&manuf=" + WebUtility.UrlEncode(Manufacturer) + "&ver=" + PackageVersion); - response.Wait(5000); - if (response.IsCompleted) - { - sResult = response.Result; + sResult = response.Result; - // Set cache options. - var cacheEntryOptions = new MemoryCacheEntryOptions() - .SetSlidingExpiration(TimeSpan.FromSeconds(330)); - sResult = response.Result; - _cache.Set("SWGET3-" + (PackageName + Manufacturer + PackageVersion).GetHashCode(StringComparison.InvariantCultureIgnoreCase), sResult, cacheEntryOptions); - return sResult; - } + if (sResult.StartsWith('[') & sResult.Length > 64) + { + // Set cache options. + var cacheEntryOptions = new MemoryCacheEntryOptions() + .SetSlidingExpiration(TimeSpan.FromSeconds(330)); + sResult = response.Result; + _cache.Set("SWGET3-" + (PackageName + Manufacturer + PackageVersion).GetHashCode(StringComparison.InvariantCultureIgnoreCase), sResult, cacheEntryOptions); + return sResult; } } } @@ -306,9 +302,8 @@ public static async Task Feedback(string productName, string productVers { try { - var oClient = new HttpClient(); - oClient.DefaultRequestHeaders.Add("AuthenticatedToken", Token); - oClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType)); + //oClient.DefaultRequestHeaders.Add("AuthenticatedToken", Token); + //oClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType)); var oRes = await oClient.GetStringAsync(sURL + "/rest/Feedback?name=" + WebUtility.UrlEncode(productName) + "&ver=" + WebUtility.UrlEncode(productVersion) + "&man=" + WebUtility.UrlEncode(manufacturer) + "&arch=" + architecture + "&ok=" + working + "&user=" + WebUtility.UrlEncode(userKey) + "&text=" + WebUtility.UrlEncode(feedback)); return oRes; } @@ -322,106 +317,134 @@ public static string GetSWDefinitions(string productName, string productVersion, { Console.WriteLine("GET SW:" + productName); string s1 = NormalizeString(productName + productVersion + manufacturer); - string sSWFile = @"wwwroot/rzsw/" + s1 + ".xml"; + string sSWFile = @"wwwroot/rzsw/" + s1 + ".json"; + contentType = "application/json"; try { - - if (!Directory.Exists("wwwroot/rzsw")) Directory.CreateDirectory("wwwroot/rzsw"); if (!Directory.Exists("wwwroot/files")) Directory.CreateDirectory("wwwroot/files"); - if (contentType.ToLower() == "application/json") - sSWFile = @"wwwroot/rzsw/" + s1 + ".json"; - if (File.Exists(sSWFile)) { if (CatalogTTL == 0 || DateTime.Now.ToUniversalTime() - File.GetCreationTime(sSWFile).ToUniversalTime() <= new TimeSpan(CatalogTTL, 0, 1)) { - return File.ReadAllText(sSWFile); + string sContent = File.ReadAllText(sSWFile); + if (sContent.Length > 64) + { + return sContent; + } + else + { + File.Delete(sSWFile); + } } } - using (var handler = new HttpClientHandler()) + //oClient.DefaultRequestHeaders.Add("AuthenticatedToken", Token); + //oClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType)); + oClient.DefaultRequestHeaders.Accept.Clear(); + oClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + var response = oClient.GetStringAsync(sURL + "/rest/GetSWDefinition?name=" + WebUtility.UrlEncode(productName) + "&ver=" + WebUtility.UrlEncode(productVersion) + "&man=" + WebUtility.UrlEncode(manufacturer)); + response.Wait(15000); + if (response.IsCompleted) { - handler.AllowAutoRedirect = true; - handler.MaxAutomaticRedirections = 5; - handler.CheckCertificateRevocationList = false; - - if (!string.IsNullOrEmpty(Proxy)) - { - handler.Proxy = new WebProxy(Proxy, true); - handler.UseProxy = true; - } + string sResult = response.Result; - using (var oClient = new HttpClient(handler)) + try { - oClient.DefaultRequestHeaders.Add("AuthenticatedToken", Token); - oClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType)); - var response = oClient.GetStringAsync(sURL + "/rest/GetSWDefinition?name=" + WebUtility.UrlEncode(productName) + "&ver=" + WebUtility.UrlEncode(productVersion) + "&man=" + WebUtility.UrlEncode(manufacturer)); - response.Wait(15000); - if (response.IsCompleted) + var oAddSW = Newtonsoft.Json.JsonConvert.DeserializeObject>(sResult); + bool isReady = true; + foreach (var oSW in oAddSW) { - string sResult = response.Result; - - - try + foreach (var oDL in oSW.Files) { - if (contentType.ToLower() == "application/json") + string sDir = @"wwwroot/files/" + oSW.ContentID; + if (!Directory.Exists(sDir)) + { + Directory.CreateDirectory(sDir); + } + + if (oDL.URL.StartsWith("http") || oDL.URL.StartsWith("ftp")) { - var oAddSW = Newtonsoft.Json.JsonConvert.DeserializeObject>(sResult); - bool isReady = true; - foreach (var oSW in oAddSW) + if (!File.Exists(sDir + "/" + oDL.FileName)) { - foreach (var oDL in oSW.Files) + isReady = false; + if (!RedirectToIPFS) { - string sDir = @"wwwroot/files/" + oSW.ContentID; - if (!Directory.Exists(sDir)) - { - Directory.CreateDirectory(sDir); - } - - if (oDL.URL.StartsWith("http") || oDL.URL.StartsWith("ftp")) - { - if (!File.Exists(sDir + "/" + oDL.FileName)) - { - isReady = false; - - var oRes = _DownloadFile(oDL.URL, sDir + "/" + oDL.FileName); - Thread.Sleep(500); + var oRes = _DownloadFile(oDL.URL, sDir + "/" + oDL.FileName); + Thread.Sleep(500); + } + else + isReady = true; - } + } - if (!IsFileLocked(new FileInfo(sDir + "/" + oDL.FileName))) - { - oDL.URL = localURL + "/rest/dl/" + oSW.ContentID + "/" + oDL.FileName; - } - else + if (!IsFileLocked(new FileInfo(sDir + "/" + oDL.FileName))) + { + string sHash = ""; + if (UseIPFS == 1) + { + sHash = IPFSAdd(sDir + "/" + oDL.FileName); + } + if(RedirectToIPFS) + { + sHash = GetIPFS(oSW.ContentID, oDL.FileName).Trim('"'); + } + if(!string.IsNullOrEmpty(sHash)) + { + if (!RedirectToIPFS) + { + long lSize = new FileInfo(sDir + "/" + oDL.FileName).Length; + if (lSize > 8100 & sHash.StartsWith("Qm")) { - isReady = false; + AddIPFS(oSW.ContentID, oDL.FileName, sHash, lSize, true); } - } + //https://gateway.ipfs.io/ipfs/QmNMKonBPBuE8NyGPcu1prkaE9MaJBHTGMLB9JX6sFMStQ/ + oDL.URL = ipfs_GW_URL + "/" + sHash + "/"; } + else + { + oDL.URL = localURL + "/rest/dl/" + oSW.ContentID + "/" + oDL.FileName; + } + } - if (isReady) + else { - sResult = Newtonsoft.Json.JsonConvert.SerializeObject(oAddSW); - File.WriteAllText(sSWFile, sResult); + isReady = false; } + } } - catch { } - - - return sResult; } + if (isReady) + { + sResult = Newtonsoft.Json.JsonConvert.SerializeObject(oAddSW); + if (sResult.StartsWith('[') & sResult.Length > 64) + { + File.WriteAllText(sSWFile, sResult); + } + } + } + catch(Exception ex) + { + Console.WriteLine("Error:" + ex.Message); + } + + + return sResult; } + + + } + catch(Exception ex) + { + Console.WriteLine("ERROR: " + ex.Message); } - catch { } if (File.Exists(sSWFile)) { @@ -521,38 +544,23 @@ public static Stream GetIcon(Int32 iconid) } else { - using (var handler = new HttpClientHandler()) + //oClient.DefaultRequestHeaders.Add("AuthenticatedToken", Token); + //oClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("image/jpeg")); + var response = oClient.GetStreamAsync(sURL + "/rest/GetIcon?id=" + iconid.ToString()); + response.Wait(5000); + if (response.IsCompleted) { - handler.AllowAutoRedirect = true; - handler.MaxAutomaticRedirections = 5; - handler.CheckCertificateRevocationList = false; - - if (!string.IsNullOrEmpty(Proxy)) + var oRet = response.Result; + using (var sIcon = new FileStream(@"wwwroot/icons/" + iconid.ToString() + ".jpg", FileMode.Create)) { - handler.Proxy = new WebProxy(Proxy, true); - handler.UseProxy = true; + response.Result.CopyTo(sIcon); + sIcon.Flush(); + sIcon.Dispose(); } + return File.Open(@"wwwroot/icons/" + iconid.ToString() + ".jpg", FileMode.Open, FileAccess.Read, FileShare.Read); + } - using (var oClient = new HttpClient(handler)) - { - //oClient.DefaultRequestHeaders.Add("AuthenticatedToken", Token); - oClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("image/jpeg")); - var response = oClient.GetStreamAsync(sURL + "/rest/GetIcon?id=" + iconid.ToString()); - response.Wait(5000); - if (response.IsCompleted) - { - var oRet = response.Result; - using (var sIcon = new FileStream(@"wwwroot/icons/" + iconid.ToString() + ".jpg", FileMode.Create)) - { - response.Result.CopyTo(sIcon); - sIcon.Flush(); - sIcon.Dispose(); - } - return File.Open(@"wwwroot/icons/" + iconid.ToString() + ".jpg", FileMode.Open, FileAccess.Read, FileShare.Read); - } - } - } } } catch { } @@ -576,27 +584,14 @@ public static Stream GetFile(string filename) return null; } - public static async void TrackDownloads(string contentID) - { - try - { - var oClient = new HttpClient(); - oClient.DefaultRequestHeaders.Add("AuthenticatedToken", Token); - oClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType)); - await oClient.GetStringAsync(sURL + "/rest/TrackDownloads/" + WebUtility.UrlEncode(contentID)); - } - catch { } - } - public static async void TrackDownloadsNew(string SWId, string Architecture) { try { if (!string.IsNullOrEmpty(SWId) & !string.IsNullOrEmpty(Architecture)) { - var oClient = new HttpClient(); - oClient.DefaultRequestHeaders.Add("AuthenticatedToken", Token); - oClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType)); + //oClient.DefaultRequestHeaders.Add("AuthenticatedToken", Token); + //oClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType)); await oClient.GetStringAsync(sURL + "/rest/TrackDownloadsNew?SWId=" + SWId.ToString() + "&arch=" + WebUtility.UrlEncode(Architecture)); } } @@ -614,83 +609,53 @@ public static string CheckForUpdate(string lSoftware) oJSet.NullValueHandling = NullValueHandling.Ignore; var oSWUpload = JsonConvert.DeserializeObject>(lSoftware, oJSet); List lSWToCheck = new List(); - foreach(AddSoftware oSW in oSWUpload ) + foreach (AddSoftware oSW in oSWUpload) { AddSoftware oCheck; - if(!_cache.TryGetValue("noUpd" + oSW.ProductName + oSW.ProductVersion + oSW.Manufacturer, out oCheck)) + if (!_cache.TryGetValue("noUpd" + oSW.ProductName + oSW.ProductVersion + oSW.Manufacturer, out oCheck)) { lSWToCheck.Add(oSW); } } - if (lSWToCheck.Count > 0) + if (lSWToCheck.Count > 0) { lSoftware = JsonConvert.SerializeObject(lSWToCheck); } else return ""; //no updates required - using (var handler = new HttpClientHandler()) + //oClient.DefaultRequestHeaders.Add("AuthenticatedToken", Token); + //oClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType)); + using (HttpContent oCont = new StringContent(lSoftware, Encoding.UTF8, contentType)) { - handler.AllowAutoRedirect = true; - handler.MaxAutomaticRedirections = 5; - handler.CheckCertificateRevocationList = false; - if (!string.IsNullOrEmpty(Proxy)) + var response = oClient.PostAsync(sURL + "/rest/CheckForUpdate", oCont); + response.Wait(10000); + if (response.IsCompleted) { - handler.Proxy = new WebProxy(Proxy, true); - handler.UseProxy = true; - } + string responseBody = response.Result.Content.ReadAsStringAsync().Result; - using (var oClient = new HttpClient(handler)) - { - oClient.DefaultRequestHeaders.Add("AuthenticatedToken", Token); - oClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType)); - HttpContent oCont = new StringContent(lSoftware, Encoding.UTF8, contentType); - if (contentType == "application/xml") - { - var response = oClient.PostAsync(sURL + "/rest/CheckForUpdateXml", oCont); - response.Wait(10000); - if (response.IsCompleted) - { - string responseBody = response.Result.Content.ReadAsStringAsync().Result; - sResult = responseBody; - return sResult; - } - } + sResult = responseBody; - if (contentType == "application/json") + try { - var response = oClient.PostAsync(sURL + "/rest/CheckForUpdate", oCont); - response.Wait(10000); - if (response.IsCompleted) + var lSWUpd = Newtonsoft.Json.JsonConvert.DeserializeObject>(sResult); + if (lSWUpd.Count == 0) //No Updates found -> cache all SW Items to prevent further check { - string responseBody = response.Result.Content.ReadAsStringAsync().Result; - - sResult = responseBody; + // Set cache options. + var cacheEntryOptions = new MemoryCacheEntryOptions() + .SetSlidingExpiration(new TimeSpan(4, 0, 0)); //Cache 4h - try + foreach (AddSoftware oSW in lSWToCheck) { - var lSWUpd = Newtonsoft.Json.JsonConvert.DeserializeObject>(sResult); - if (lSWUpd.Count == 0) //No Updates found -> cache all SW Items to prevent further check - { - // Set cache options. - var cacheEntryOptions = new MemoryCacheEntryOptions() - .SetSlidingExpiration(new TimeSpan(4, 0, 0)); //Cache 4h - - foreach (AddSoftware oSW in lSWToCheck) - { - _cache.Set("noUpd" + oSW.ProductName + oSW.ProductVersion + oSW.Manufacturer, oSW, cacheEntryOptions); - } - } + _cache.Set("noUpd" + oSW.ProductName + oSW.ProductVersion + oSW.Manufacturer, oSW, cacheEntryOptions); } - catch { } - - return sResult; } } + catch { } + + return sResult; } } - - } catch { } @@ -701,52 +666,73 @@ public static bool UploadSWEntry(string lSoftware) { try { - using (var handler = new HttpClientHandler()) - { - handler.AllowAutoRedirect = true; - handler.MaxAutomaticRedirections = 5; - handler.CheckCertificateRevocationList = false; - if (!string.IsNullOrEmpty(Proxy)) + //oClient.DefaultRequestHeaders.Add("AuthenticatedToken", Token); + //oClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType)); + HttpContent oCont = new StringContent(lSoftware, Encoding.UTF8, contentType); + + var response = oClient.PostAsync(sURL + "/rest/UploadSWEntry", oCont); + response.Wait(5000); + + if (response.IsCompleted) + { + if (response.Result.StatusCode == HttpStatusCode.OK) { - handler.Proxy = new WebProxy(Proxy, true); - handler.UseProxy = true; + return true; } - - using (var oClient = new HttpClient(handler)) + else { - if (!string.IsNullOrEmpty(ProxyUserPW)) - { - oClient.DefaultRequestHeaders.ProxyAuthorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(bProxyUser)); - } - oClient.DefaultRequestHeaders.Add("AuthenticatedToken", Token); - oClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType)); - HttpContent oCont = new StringContent(lSoftware, Encoding.UTF8, contentType); + return false; + } + } + } + catch { } - var response = oClient.PostAsync(sURL + "/rest/UploadSWEntry", oCont); - response.Wait(5000); + return false; + } - if (response.IsCompleted) - { - if (response.Result.StatusCode == HttpStatusCode.OK) - { - return true; - } - else - { - return false; - } - } + public static bool AddIPFS(string contentID, string fileName, string iPFS, long size, bool update) + { + try + { + var response = oClient.GetAsync(sURL + "/rest/AddIPFS?Id=" + contentID + "&file=" + fileName + "&hash=" + iPFS + "&size=" + size + "&upd=" + update); + response.Wait(5000); + if (response.IsCompleted) + { + if (response.Result.StatusCode == HttpStatusCode.OK) + { + //Console.WriteLine("AddIPFS:" + contentID + "/" + fileName + "/" + iPFS); + return true; + } + else + { + return false; } } - } catch { } return false; } + public static string GetIPFS(string contentID, string fileName) + { + try + { + var response = oClient.GetStringAsync(sURL + "/rest/GetIPFS?Id=" + contentID + "&file=" + fileName); + response.Wait(5000); + + if (response.IsCompleted) + { + return response.Result.Trim('"'); + } + } + catch { } + + return ""; + } + public static string NormalizeString(string Input) { char[] arr = (Input).ToCharArray(); @@ -760,51 +746,21 @@ private static async Task _DownloadFile(string URL, string FileName) { try { - using (HttpClientHandler handler = new HttpClientHandler()) - { - handler.AllowAutoRedirect = true; - handler.MaxAutomaticRedirections = 5; - handler.CheckCertificateRevocationList = false; - handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; }; //To prevent Issue with FW + oClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "chocolatey command line"); - if (!string.IsNullOrEmpty(Proxy)) + using (HttpResponseMessage response = await oClient.GetAsync(URL, HttpCompletionOption.ResponseHeadersRead)) + using (Stream streamToReadFrom = await response.Content.ReadAsStreamAsync()) + { + string fileToWriteTo = FileName; // Path.GetTempFileName(); + + using (Stream streamToWriteTo = File.Open(fileToWriteTo, FileMode.Create)) { - handler.Proxy = new WebProxy(Proxy, true); - handler.UseProxy = true; + await streamToReadFrom.CopyToAsync(streamToWriteTo); } + Console.WriteLine("Donwloaded: " + URL); + } - /*using (var httpClient = new HttpClient(handler)) - { - httpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "chocolatey command line"); - httpClient.Timeout = new TimeSpan(0, 15, 0); - using (var request = new HttpRequestMessage(HttpMethod.Get, new Uri(URL))) - { - using (Stream contentStream = await (await httpClient.SendAsync(request)).Content.ReadAsStreamAsync(), - stream = new FileStream(FileName, FileMode.Create, FileAccess.Write, FileShare.None, 32768, true)) - { - await contentStream.CopyToAsync(stream); - } - Console.WriteLine("Donwloaded: " + URL); - } - - }*/ - using (HttpClient httpClient = new HttpClient(handler)) - { - httpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "chocolatey command line"); - - using (HttpResponseMessage response = await httpClient.GetAsync(URL, HttpCompletionOption.ResponseHeadersRead)) - using (Stream streamToReadFrom = await response.Content.ReadAsStreamAsync()) - { - string fileToWriteTo = FileName; // Path.GetTempFileName(); - using (Stream streamToWriteTo = File.Open(fileToWriteTo, FileMode.Create)) - { - await streamToReadFrom.CopyToAsync(streamToWriteTo); - } - Console.WriteLine("Donwloaded: " + URL); - } - } - } } catch (Exception ex) { @@ -817,6 +773,9 @@ private static async Task _DownloadFile(string URL, string FileName) private static bool IsFileLocked(FileInfo file) { + if (!file.Exists) + return false; + FileStream stream = null; try @@ -836,6 +795,40 @@ private static bool IsFileLocked(FileInfo file) //file is not locked return false; } + + /// + /// Add file to IPFS + /// + /// File + /// IPFS Hash + public static string IPFSAdd(string path) + { + string sResult = ""; + try + { + if (UseIPFS > 0) + { + var oProc = System.Diagnostics.Process.Start("ipfs", "add --nocopy \"" + path + "\""); + oProc.StartInfo.RedirectStandardOutput = true; + oProc.StartInfo.UseShellExecute = false; + List output = new List(); + oProc.OutputDataReceived += new DataReceivedEventHandler((sender, e) => + { + output.Add(e.Data); + }); + oProc.Start(); + oProc.BeginOutputReadLine(); + oProc.WaitForExit(30000); + sResult = output.First().Split(' ')[1]; + } + } + catch(Exception ex) + { + Console.WriteLine("ERROR: " + ex.Message); + } + + return sResult; + } } public class GetSoftware diff --git a/RZ.Cache/RZCache/RZWCF.csproj b/RZ.Cache/RZCache/RZWCF.csproj deleted file mode 100644 index c665d08..0000000 --- a/RZ.Cache/RZCache/RZWCF.csproj +++ /dev/null @@ -1,38 +0,0 @@ - - - - netcoreapp2.0 - ..\docker-compose.dcproj - 1.0.1.0 - 1.0.1.0 - Roger Zander - Zander Tools - RZWCF Proxy Service - RuckZuck.tool Proxy and Caching Service - Copyright (c) 2017 by Roger Zander - https://github.com/rzander/ruckzuck - https://github.com/rzander/ruckzuck/blob/master/LICENSE.md - software package manager windows cache proxy - 3d943e5a-c0e9-40f0-b91e-9ab682223099 - 1.0.1 - - - - - - - - - - - - - - Always - - - - - - - diff --git a/RZ.Cache/RZCache/Startup.cs b/RZ.Cache/RZCache/Startup.cs index 2a1b268..fa2d7af 100644 --- a/RZ.Cache/RZCache/Startup.cs +++ b/RZ.Cache/RZCache/Startup.cs @@ -18,7 +18,7 @@ public Startup(IHostingEnvironment env) var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) - .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) + .AddJsonFile("appsettings.{env.EnvironmentName}.json", optional: true) .AddEnvironmentVariables(); Configuration = builder.Build(); diff --git a/RZ.Cache/RZCache/appsettings.json b/RZ.Cache/RZCache/appsettings.json index 58e88b5..db8a374 100644 --- a/RZ.Cache/RZCache/appsettings.json +++ b/RZ.Cache/RZCache/appsettings.json @@ -10,7 +10,9 @@ "localURL": "https://rzproxy.azurewebsites.net", "CatalogTTL": 4, "Proxy": "", - "ProxyUSerPW": "" + "ProxyUSerPW": "", + "UseIPFS": 0, + "IPFS_GW_URL": "https://gateway.ipfs.io/ipfs" } }