ASP.NET Core 开源GitServer 实现自己的GitHub
2021-05-16 22:29
标签:pack 实现 mon 中间 res cookie exe syn window ASP.NET Core 2.0 开源Git HTTP Server,实现类似 GitHub、GitLab。 GitHub:https://github.com/linezero/GitServer 设置 需要先安装Git,并确保git 命令可以执行,GitPath 可以是 git 的绝对路径。 目前实现的功能 更多功能可以查看readme,也欢迎大家贡献支持。 LibGit2Sharp 用于操作Git库,实现创建读取仓库信息及删除仓库。 以下是主要代码: 执行Git命令 git-upload-pack git-receive-pack 主要代码 GitCommandResult 实现IActionResult git http 默认的认证为Basic 基本认证,所以这里实现Basic 基本认证。 在ASP.NET Core 2.0 中 Authentication 变化很大之前1.0的一些代码是无法使用。 首先实现 AuthenticationHandler,然后实现 AuthenticationSchemeOptions,创建 BasicAuthenticationOptions。 最主要就是这两个类,下面两个类为辅助类,用于配置和中间件注册。 更多可以查看官方文档 身份验证 https://docs.microsoft.com/zh-cn/aspnet/core/security/authentication/ https://docs.microsoft.com/zh-cn/aspnet/core/migration/1x-to-2x/identity-2x 实现自定义登录,无需identity ,实现注册登录。 主要代码: 启用Cookie https://github.com/linezero/GitServer/blob/master/GitServer/Startup.cs#L60 登录 https://github.com/linezero/GitServer/blob/master/GitServer/Controllers/UserController.cs#L34 官方文档介绍:https://docs.microsoft.com/zh-cn/aspnet/core/security/authentication/cookie?tabs=aspnetcore2x 发布后配置数据库及git目录(可以为绝对地址和命令)、git 仓库目录。 运行后注册账户,登录账户创建仓库,然后根据提示操作,随后git push、git pull 都可以。 ASP.NET Core 开源GitServer 实现自己的GitHub 标签:pack 实现 mon 中间 res cookie exe syn window 原文地址:http://www.cnblogs.com/linezero/p/gitserver.html "GitSettings": {
"BasePath": "D:\\Git",
"GitPath": "git"
}
Git交互
public Repository CreateRepository(string name)
{
string path = Path.Combine(Settings.BasePath, name);
Repository repo = new Repository(Repository.Init(path, true));
return repo;
}
public Repository CreateRepository(string name, string remoteUrl)
{
var path = Path.Combine(Settings.BasePath, name);
try
{
using (var repo = new Repository(Repository.Init(path, true)))
{
repo.Config.Set("core.logallrefupdates", true);
repo.Network.Remotes.Add("origin", remoteUrl, "+refs/*:refs/*");
var logMessage = "";
foreach (var remote in repo.Network.Remotes)
{
IEnumerablestring> refSpecs = remote.FetchRefSpecs.Select(x => x.Specification);
Commands.Fetch(repo, remote.Name, refSpecs, null, logMessage);
}
return repo;
}
}
catch
{
try
{
Directory.Delete(path, true);
}
catch { }
return null;
}
}
public void DeleteRepository(string name)
{
Exception e = null;
for(int i = 0; i 3; i++)
{
try
{
string path = Path.Combine(Settings.BasePath, name);
Directory.Delete(path, true);
}
catch(Exception ex) { e = ex; }
}
if (e != null)
throw new GitException("Failed to delete repository", e);
}
public async Task ExecuteResultAsync(ActionContext context)
{
HttpResponse response = context.HttpContext.Response;
Stream responseStream = GetOutputStream(context.HttpContext);
string contentType = $"application/x-{Options.Service}";
if (Options.AdvertiseRefs)
contentType += "-advertisement";
response.ContentType = contentType;
response.Headers.Add("Expires", "Fri, 01 Jan 1980 00:00:00 GMT");
response.Headers.Add("Pragma", "no-cache");
response.Headers.Add("Cache-Control", "no-cache, max-age=0, must-revalidate");
ProcessStartInfo info = new ProcessStartInfo(_gitPath, Options.ToString())
{
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true
};
using (Process process = Process.Start(info))
{
GetInputStream(context.HttpContext).CopyTo(process.StandardInput.BaseStream);
if (Options.EndStreamWithNull)
process.StandardInput.Write(‘\0‘);
process.StandardInput.Dispose();
using (StreamWriter writer = new StreamWriter(responseStream))
{
if (Options.AdvertiseRefs)
{
string service = $"# service={Options.Service}\n";
writer.Write($"{service.Length + 4:x4}{service}0000");
writer.Flush();
}
process.StandardOutput.BaseStream.CopyTo(responseStream);
}
process.WaitForExit();
}
}
BasicAuthentication 基本认证实现
1 public class BasicAuthenticationHandler : AuthenticationHandler
CookieAuthentication Cookie认证
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
}).AddCookie(options=> {
options.AccessDeniedPath = "/User/Login";
options.LoginPath = "/User/Login";
})
var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme, ClaimTypes.Name, ClaimTypes.Role);
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Name));
identity.AddClaim(new Claim(ClaimTypes.Name, user.Name));
identity.AddClaim(new Claim(ClaimTypes.Email, user.Email));
var principal = new ClaimsPrincipal(identity);
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);
部署说明
{
"ConnectionStrings": {
"ConnectionType": "Sqlite", //Sqlite,MSSQL,MySQL
"DefaultConnection": "Filename=gitserver.db"
},
"GitSettings": {
"BasePath": "D:\\Git",
"GitPath": "git"
}
}
上一篇:web前端面试题(一)
下一篇:web自动化测试记录
文章标题:ASP.NET Core 开源GitServer 实现自己的GitHub
文章链接:http://soscw.com/essay/86438.html