-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
237 lines (214 loc) · 13.8 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
using System.IO.Compression;
using System.Net;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.DependencyInjection;
using System.Runtime.InteropServices;
using McMaster.Extensions.CommandLineUtils;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using Microsoft.AspNetCore.ResponseCompression;
using System;
namespace Onllama.MyRegistry
{
internal class Program
{
static void Main(string[] args)
{
var hostname = string.Empty;
var isZh = Thread.CurrentThread.CurrentCulture.Name.Contains("zh");
var allowUA = new List<string> {"ollama", "go-http-client"};
var cmd = new CommandLineApplication
{
Name = "Onllama.MyRegistry",
Description = "Onllama.MyRegistry - Running your own Ollama Registry locally." +
Environment.NewLine +
$"Copyright (c) {DateTime.Now.Year} Milkey Tan. Code released under the MIT License"
};
cmd.HelpOption("-?|-h|--help|-he");
var ipOption = cmd.Option<string>("-l|--listen <IPEndPoint>",
isZh ? "监听的地址与端口。" : "Set server listening address and port",
CommandOptionType.SingleValue);
var modelPathOption = cmd.Option<string>("-m|--model <path>",
isZh ? "模型文件路径。" : "Set model path",
CommandOptionType.SingleValue);
var httpsOption = cmd.Option("-s|--https", isZh ? "启用 HTTPS。(默认自签名,不推荐)" : "Set enable HTTPS (Self-signed by default, not recommended)",
CommandOptionType.NoValue);
var compressionOption = cmd.Option("-c|--compression", isZh ? "启用压缩(gzip)" : "Set enable compression (gzip)",
CommandOptionType.NoValue);
var uaOption = cmd.Option("-u", isZh ? "限制客户端 UA" : "Limit client User-Agent",
CommandOptionType.NoValue);
var pemOption = cmd.Option<string>("-pem|--pemfile <FilePath>",
isZh ? "PEM 证书路径。 <./cert.pem>" : "Set your pem certificate file path <./cert.pem>",
CommandOptionType.SingleOrNoValue);
var keyOption = cmd.Option<string>("-key|--keyfile <FilePath>",
isZh ? "PEM 证书密钥路径。 <./cert.key>" : "Set your pem certificate key file path <./cert.key>",
CommandOptionType.SingleOrNoValue);
var hostOption = cmd.Option<string>("-h|--host <Hostname>",
isZh ? "设置允许的主机名。(默认全部允许)" : "Set the allowed host names. (Allow all by default)",
CommandOptionType.SingleOrNoValue); var listen = new IPEndPoint(IPAddress.Any, 80);
var modelPath = Environment.GetEnvironmentVariable("OLLAMA_MODELS") ??
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".ollama",
"models");
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) &&
Directory.Exists("/usr/share/ollama/.ollama/models") &&
string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("OLLAMA_MODELS")))
modelPath = "/usr/share/ollama/.ollama/models";
cmd.OnExecute(() =>
{
if (httpsOption.HasValue()) listen.Port = 443;
if (ipOption.HasValue()) listen = IPEndPoint.Parse(ipOption.Value());
if (modelPathOption.HasValue()) modelPath = modelPathOption.Value();
if (hostOption.HasValue()) hostname = hostOption.Value();
Console.WriteLine("ModelPath:" + modelPath);
try
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(AppDomain.CurrentDomain.SetupInformation.ApplicationBase)
.ConfigureServices(services =>
{
services.AddRouting();
if (compressionOption.HasValue())
{
services.Configure<BrotliCompressionProviderOptions>(options => options.Level = CompressionLevel.Fastest);
services.Configure<GzipCompressionProviderOptions>(options => options.Level = CompressionLevel.Fastest);
services.AddResponseCompression(options =>
{
options.Providers.Add<BrotliCompressionProvider>();
options.Providers.Add<GzipCompressionProvider>();
options.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(new[]
{
"application/octet-stream"
});
});
}
})
.ConfigureKestrel(options =>
{
options.Listen(listen,
listenOptions =>
{
listenOptions.Protocols = HttpProtocols.Http1AndHttp2;
if (httpsOption.HasValue()) listenOptions.UseHttps();
if (pemOption.HasValue() && keyOption.HasValue())
listenOptions.UseHttps(X509Certificate2.CreateFromPem(
File.ReadAllText(pemOption.Value()), File.ReadAllText(keyOption.Value())));
});
})
.Configure(app =>
{
app.Map("/v2", svr =>
{
app.UseRouting().UseEndpoints(endpoint =>
{
app.Use(async (context, next) =>
{
Console.WriteLine(context.Request.Path);
await next.Invoke();
});
app.Use(async (context, next) =>
{
if (!string.IsNullOrWhiteSpace(hostname) &&
!string.Equals(context.Request.Host.Host, hostname,
StringComparison.CurrentCultureIgnoreCase))
{
context.Response.StatusCode = 403;
context.Response.ContentType = "text/plain";
await context.Response.WriteAsync("Host Not Found");
return;
}
if (uaOption.HasValue() && !allowUA.Any(x =>
context.Request.Headers.UserAgent.ToString().ToLower().Contains(x)))
{
context.Response.StatusCode = 403;
context.Response.ContentType = "text/plain";
await context.Response.WriteAsync("Unsupported Client");
return;
}
await next.Invoke();
});
endpoint.Map(
"/v2/{repo}/{model}/blobs/{hash}",
async context =>
{
Console.WriteLine("Blobs:" + context.Connection.RemoteIpAddress + "|" + string.Join(
' ', context.Request.Headers.Select(x => x.Key + ":" + x.Value)));
var hash = context.Request.RouteValues["hash"].ToString().Replace(':', '-');
var path = Path.Combine(modelPath, "blobs", hash);
if (File.Exists(path))
{
var fileLength = new FileInfo(path).Length;
context.Response.Headers.Location = context.Request.Path.ToString();
context.Response.ContentType = "application/octet-stream";
context.Response.Headers.AcceptRanges = "bytes";
if (context.Request.Headers.ContainsKey("Range"))
{
var rangeHeader = context.Request.Headers["Range"].ToString();
var range = rangeHeader.Replace("bytes=", "").Split('-');
var start = long.Parse(range[0]);
var end = range.Length > 1 ? long.Parse(range[1]) : fileLength - 1;
var length = end - start + 1;
if (start >= fileLength || end >= fileLength)
{
context.Response.StatusCode =
StatusCodes.Status416RequestedRangeNotSatisfiable;
return;
}
context.Response.ContentLength = length;
context.Response.StatusCode = StatusCodes.Status206PartialContent;
context.Response.Headers.Add("Content-Range",
$"bytes {start}-{end}/{fileLength}");
if (context.Request.Method.ToUpper() != "HEAD")
await context.Response.SendFileAsync(path, start, length);
}
else
{
context.Response.ContentLength = fileLength;
context.Response.StatusCode = StatusCodes.Status200OK;
context.Response.Headers.Add("Content-Disposition",
$"attachment; filename={hash}");
if (context.Request.Method.ToUpper() != "HEAD")
await context.Response.SendFileAsync(path);
}
}
else
context.Response.StatusCode = 404;
});
endpoint.Map(
"/v2/{repo}/{model}/manifests/{tag}",
async context =>
{
Console.WriteLine("Manifests:" + context.Connection.RemoteIpAddress + "|" + string.Join(
' ', context.Request.Headers.Select(x => x.Key + ":" + x.Value)));
var repo = context.Request.RouteValues["repo"].ToString();
var model = context.Request.RouteValues["model"].ToString();
var tag = context.Request.RouteValues["tag"].ToString();
foreach (var path in Directory
.GetDirectories(Path.Combine(modelPath, "manifests")).ToList()
.Select(subs => Path.Combine(subs, repo, model, tag)))
{
if (!File.Exists(path)) continue;
Console.WriteLine("Manifests:" + path);
if (context.Request.Method.ToUpper() != "HEAD")
await context.Response.SendFileAsync(path);
return;
}
context.Response.StatusCode = 404;
});
});
});
}).Build();
host.Run();
}
catch (Exception e)
{
Console.WriteLine(e);
}
});
cmd.Execute(args);
}
}
}