2021-04-08 02:03:17 +01:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.IO.Compression;
|
|
|
|
|
using System.Linq;
|
2021-05-23 20:21:46 +01:00
|
|
|
using System.Text;
|
2021-05-16 14:07:14 +01:00
|
|
|
using System.Threading.Tasks;
|
2021-04-08 03:08:20 +01:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.DataProtection;
|
2021-04-08 02:03:17 +01:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2021-05-02 15:22:46 +01:00
|
|
|
using Microsoft.Extensions.Options;
|
2021-05-08 20:44:40 +01:00
|
|
|
using NuGet.Packaging.Core;
|
2021-05-23 20:21:46 +01:00
|
|
|
using NuGet.Versioning;
|
2021-05-09 03:06:23 +01:00
|
|
|
using nuget_host.Data;
|
2021-05-02 15:22:46 +01:00
|
|
|
using nuget_host.Entities;
|
2021-05-08 20:44:40 +01:00
|
|
|
using nuget_host.Helpers;
|
2021-04-08 02:03:17 +01:00
|
|
|
|
|
|
|
|
namespace nuget_host.Controllers
|
|
|
|
|
{
|
2021-05-09 03:06:23 +01:00
|
|
|
|
|
|
|
|
[AllowAnonymous]
|
2021-04-08 02:03:17 +01:00
|
|
|
public class PackagesController : Controller
|
|
|
|
|
{
|
2021-04-25 12:12:50 +01:00
|
|
|
private readonly ILogger<PackagesController> logger;
|
|
|
|
|
private readonly IDataProtector protector;
|
2021-04-08 02:03:17 +01:00
|
|
|
|
2021-05-02 15:22:46 +01:00
|
|
|
private readonly NugetSettings nugetSettings;
|
2021-05-09 03:06:23 +01:00
|
|
|
ApplicationDbContext dbContext;
|
2021-05-02 15:22:46 +01:00
|
|
|
|
|
|
|
|
public PackagesController(
|
|
|
|
|
ILoggerFactory loggerFactory,
|
|
|
|
|
IDataProtectionProvider provider,
|
2021-05-09 03:06:23 +01:00
|
|
|
IOptions<NugetSettings> nugetOptions,
|
|
|
|
|
ApplicationDbContext dbContext)
|
2021-04-08 02:03:17 +01:00
|
|
|
{
|
|
|
|
|
logger = loggerFactory.CreateLogger<PackagesController>();
|
2021-05-02 15:22:46 +01:00
|
|
|
nugetSettings = nugetOptions.Value;
|
|
|
|
|
protector = provider.CreateProtector(nugetSettings.ProtectionTitle);
|
2021-05-09 03:06:23 +01:00
|
|
|
this.dbContext = dbContext;
|
2021-04-08 02:03:17 +01:00
|
|
|
}
|
|
|
|
|
|
2021-05-11 23:01:11 +01:00
|
|
|
[HttpPut("packages")]
|
2021-05-16 14:07:14 +01:00
|
|
|
public async Task<IActionResult> Put()
|
2021-04-08 02:03:17 +01:00
|
|
|
{
|
[{"PkgName":"nuget-cli.1.0.0.nupkg","Executed":true,"OK":true,"AlreadyPresent":false,"Message":"{\"versionId\":\"nuget-host, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null\",\"files\":[\"nuget-cli-1.0.0.nupkg\"]}","StatusCode":"OK","StackTrace":null}]
2021-05-18 23:17:48 +01:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var clientVersionId = Request.Headers["X-NuGet-Client-Version"];
|
|
|
|
|
var apiKey = Request.Headers["X-NuGet-ApiKey"];
|
|
|
|
|
ViewData["versionId"] = typeof(PackagesController).Assembly.FullName;
|
|
|
|
|
var files = new List<string>();
|
|
|
|
|
ViewData["files"] = files;
|
2021-05-11 23:01:11 +01:00
|
|
|
|
[{"PkgName":"nuget-cli.1.0.0.nupkg","Executed":true,"OK":true,"AlreadyPresent":false,"Message":"{\"versionId\":\"nuget-host, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null\",\"files\":[\"nuget-cli-1.0.0.nupkg\"]}","StatusCode":"OK","StackTrace":null}]
2021-05-18 23:17:48 +01:00
|
|
|
var clearkey = protector.Unprotect(apiKey);
|
|
|
|
|
var apikey = dbContext.ApiKeys.SingleOrDefault(k => k.Id == clearkey);
|
|
|
|
|
if (apikey == null)
|
2021-04-08 02:03:17 +01:00
|
|
|
{
|
2021-05-20 20:35:11 +01:00
|
|
|
logger.LogError("403 : no api-key");
|
[{"PkgName":"nuget-cli.1.0.0.nupkg","Executed":true,"OK":true,"AlreadyPresent":false,"Message":"{\"versionId\":\"nuget-host, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null\",\"files\":[\"nuget-cli-1.0.0.nupkg\"]}","StatusCode":"OK","StackTrace":null}]
2021-05-18 23:17:48 +01:00
|
|
|
return Unauthorized();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var file in Request.Form.Files)
|
|
|
|
|
{
|
|
|
|
|
string initpath = Path.Combine(Environment.GetEnvironmentVariable("TEMP") ??
|
|
|
|
|
Environment.GetEnvironmentVariable("TMP") ?? "/tmp",
|
2021-05-16 14:07:14 +01:00
|
|
|
$"nuget_host-{Guid.NewGuid()}.nupkg");
|
|
|
|
|
|
2021-05-13 23:05:19 +01:00
|
|
|
using (FileStream fw = new FileStream(initpath, FileMode.Create))
|
|
|
|
|
{
|
|
|
|
|
file.CopyTo(fw);
|
|
|
|
|
}
|
2021-05-11 23:01:11 +01:00
|
|
|
|
2021-05-13 23:05:19 +01:00
|
|
|
using (FileStream fw = new FileStream(initpath, FileMode.Open))
|
2021-04-08 02:03:17 +01:00
|
|
|
{
|
[{"PkgName":"nuget-cli.1.0.0.nupkg","Executed":true,"OK":true,"AlreadyPresent":false,"Message":"{\"versionId\":\"nuget-host, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null\",\"files\":[\"nuget-cli-1.0.0.nupkg\"]}","StatusCode":"OK","StackTrace":null}]
2021-05-18 23:17:48 +01:00
|
|
|
var archive = new ZipArchive(fw);
|
2021-05-13 23:05:19 +01:00
|
|
|
|
2021-05-23 20:21:46 +01:00
|
|
|
var nuspec = archive.Entries.FirstOrDefault(e => e.FullName.EndsWith(".nuspec"));
|
|
|
|
|
if (nuspec==null) return BadRequest("no nuspec from archive");
|
|
|
|
|
string pkgpath;
|
|
|
|
|
NuGetVersion version;
|
|
|
|
|
string pkgid;
|
|
|
|
|
string fullpath;
|
|
|
|
|
|
|
|
|
|
using (var specstr = nuspec.Open())
|
2021-04-08 02:03:17 +01:00
|
|
|
{
|
2021-05-23 20:21:46 +01:00
|
|
|
NuspecCoreReader reader = new NuspecCoreReader(specstr);
|
|
|
|
|
|
|
|
|
|
string pkgdesc = reader.GetDescription();
|
|
|
|
|
pkgid = reader.GetId();
|
|
|
|
|
version = reader.GetVersion();
|
|
|
|
|
string pkgidpath = Path.Combine(nugetSettings.PackagesRootDir,
|
|
|
|
|
pkgid);
|
|
|
|
|
pkgpath = Path.Combine(pkgidpath, version.ToFullString());
|
|
|
|
|
string name = $"{pkgid}-{version}.nupkg";
|
|
|
|
|
fullpath = Path.Combine(pkgpath, name);
|
|
|
|
|
|
|
|
|
|
var destpkgiddir = new DirectoryInfo(pkgidpath);
|
|
|
|
|
Package package = dbContext.Packages.SingleOrDefault(p => p.Id == pkgid);
|
|
|
|
|
if (package != null)
|
2021-05-13 23:05:19 +01:00
|
|
|
{
|
2021-05-23 20:21:46 +01:00
|
|
|
if (package.OwnerId != apikey.UserId)
|
2021-05-20 20:24:03 +01:00
|
|
|
{
|
2021-05-23 20:21:46 +01:00
|
|
|
return new ForbidResult();
|
2021-05-20 20:24:03 +01:00
|
|
|
}
|
2021-05-23 20:21:46 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
package = new Package
|
2021-05-20 20:24:03 +01:00
|
|
|
{
|
2021-05-23 20:21:46 +01:00
|
|
|
Id = pkgid,
|
|
|
|
|
Description = pkgdesc,
|
|
|
|
|
OwnerId = apikey.UserId
|
|
|
|
|
};
|
|
|
|
|
dbContext.Packages.Add(package);
|
|
|
|
|
}
|
|
|
|
|
if (!destpkgiddir.Exists) destpkgiddir.Create();
|
|
|
|
|
|
|
|
|
|
var source = new FileInfo(initpath);
|
|
|
|
|
var dest = new FileInfo(fullpath);
|
|
|
|
|
var destdir = new DirectoryInfo(dest.DirectoryName);
|
|
|
|
|
if (dest.Exists)
|
|
|
|
|
{
|
2021-06-18 21:16:38 +01:00
|
|
|
ViewData["msg"] = "existant";
|
|
|
|
|
ViewData["ecode"] = 1;
|
2021-05-23 20:21:46 +01:00
|
|
|
logger.LogWarning("400 : existant");
|
|
|
|
|
return BadRequest(ViewData);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
destdir.Create();
|
2021-05-16 14:07:14 +01:00
|
|
|
|
2021-05-23 20:21:46 +01:00
|
|
|
source.MoveTo(fullpath);
|
|
|
|
|
files.Add(name);
|
2021-06-21 01:49:19 +01:00
|
|
|
string fullstringversion = version.ToFullString();
|
|
|
|
|
PackageVersion pkgver = dbContext.PackageVersions.FirstOrDefault
|
|
|
|
|
(v => v.PackageId == package.Id && v.FullString == fullstringversion);
|
|
|
|
|
if (pkgver == null)
|
2021-05-18 00:39:03 +01:00
|
|
|
{
|
2021-06-21 01:49:19 +01:00
|
|
|
pkgver = new PackageVersion
|
|
|
|
|
{
|
|
|
|
|
Package = package,
|
|
|
|
|
Major = version.Major,
|
|
|
|
|
Minor = version.Minor,
|
|
|
|
|
Patch = version.Patch,
|
|
|
|
|
IsPrerelease = version.IsPrerelease,
|
|
|
|
|
FullString = version.ToFullString()
|
|
|
|
|
};
|
|
|
|
|
dbContext.PackageVersions.Add(pkgver);
|
|
|
|
|
await dbContext.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// existant en db mais pas sur le disque
|
|
|
|
|
// TODO prise en charge de ce cas anormal
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-23 20:21:46 +01:00
|
|
|
logger.LogInformation($"new package : {nuspec.Name}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
using (var shacrypto = System.Security.Cryptography.SHA512.Create())
|
|
|
|
|
{
|
|
|
|
|
using (var stream = System.IO.File.OpenRead(fullpath))
|
|
|
|
|
{
|
|
|
|
|
var hash = shacrypto.ComputeHash(stream);
|
|
|
|
|
var shafullname = fullpath + ".sha512";
|
|
|
|
|
var hashtext = Convert.ToBase64String(hash);
|
|
|
|
|
var hashtextbytes = Encoding.ASCII.GetBytes(hashtext);
|
|
|
|
|
|
|
|
|
|
using (var shafile = System.IO.File.OpenWrite(shafullname))
|
2021-05-16 14:07:14 +01:00
|
|
|
{
|
2021-05-23 20:21:46 +01:00
|
|
|
shafile.Write(hashtextbytes, 0, hashtextbytes.Length);
|
2021-05-20 20:24:03 +01:00
|
|
|
}
|
2021-05-13 23:05:19 +01:00
|
|
|
}
|
2021-04-08 02:03:17 +01:00
|
|
|
}
|
2021-05-23 20:21:46 +01:00
|
|
|
nuspec.ExtractToFile(Path.Combine(pkgpath, pkgid + ".nuspec"));
|
2021-04-08 02:03:17 +01:00
|
|
|
}
|
[{"PkgName":"nuget-cli.1.0.0.nupkg","Executed":true,"OK":true,"AlreadyPresent":false,"Message":"{\"versionId\":\"nuget-host, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null\",\"files\":[\"nuget-cli-1.0.0.nupkg\"]}","StatusCode":"OK","StackTrace":null}]
2021-05-18 23:17:48 +01:00
|
|
|
|
2021-05-13 23:05:19 +01:00
|
|
|
}
|
[{"PkgName":"nuget-cli.1.0.0.nupkg","Executed":true,"OK":true,"AlreadyPresent":false,"Message":"{\"versionId\":\"nuget-host, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null\",\"files\":[\"nuget-cli-1.0.0.nupkg\"]}","StatusCode":"OK","StackTrace":null}]
2021-05-18 23:17:48 +01:00
|
|
|
return Ok(ViewData);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2021-05-23 01:00:57 +01:00
|
|
|
logger.LogError(ex.Message);
|
|
|
|
|
logger.LogError("Stack Trace: "+ ex.StackTrace);
|
|
|
|
|
return new ObjectResult(new { ViewData, ex.Message})
|
[{"PkgName":"nuget-cli.1.0.0.nupkg","Executed":true,"OK":true,"AlreadyPresent":false,"Message":"{\"versionId\":\"nuget-host, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null\",\"files\":[\"nuget-cli-1.0.0.nupkg\"]}","StatusCode":"OK","StackTrace":null}]
2021-05-18 23:17:48 +01:00
|
|
|
{ StatusCode = 500 };
|
2021-04-08 02:03:17 +01:00
|
|
|
}
|
|
|
|
|
}
|
2021-05-23 20:21:46 +01:00
|
|
|
// dotnet add . package -s http://localhost:5000/packages nuget-cli
|
|
|
|
|
// packages/FindPackagesById()?id='nuget-cli'&semVerLevel=2.0.0
|
|
|
|
|
[HttpGet("packages/FindPackagesById()")]
|
|
|
|
|
public IActionResult Index(string id, string semVerLevel)
|
2021-04-08 02:03:17 +01:00
|
|
|
{
|
2021-05-23 20:21:46 +01:00
|
|
|
if (string.IsNullOrEmpty(id))
|
2021-04-08 02:03:17 +01:00
|
|
|
{
|
2021-06-18 21:16:38 +01:00
|
|
|
ViewData["msg"] = "no id";
|
2021-04-08 02:03:17 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-05-23 20:21:46 +01:00
|
|
|
ViewData["id"] = id;
|
2021-04-08 02:03:17 +01:00
|
|
|
// TODO Assert valid sem ver spec
|
2021-05-02 15:22:46 +01:00
|
|
|
var filelst = new DirectoryInfo(nugetSettings.PackagesRootDir);
|
2021-05-23 20:21:46 +01:00
|
|
|
var lst = filelst.GetDirectories(id);
|
2021-04-08 02:03:17 +01:00
|
|
|
ViewData["lst"] = lst.Select(entry => entry.Name);
|
|
|
|
|
}
|
|
|
|
|
return Ok(ViewData);
|
|
|
|
|
}
|
2021-04-08 03:08:20 +01:00
|
|
|
|
2021-05-02 15:22:46 +01:00
|
|
|
|
|
|
|
|
|
2021-04-08 03:08:20 +01:00
|
|
|
[Authorize]
|
|
|
|
|
[HttpGet("api/get-key/{*apikey}")]
|
|
|
|
|
public IActionResult GetApiKey(string apiKey)
|
|
|
|
|
{
|
|
|
|
|
return Ok(protector.Protect(apiKey));
|
|
|
|
|
}
|
2021-04-08 02:03:17 +01:00
|
|
|
}
|
|
|
|
|
}
|