240 lines
9.4 KiB
C#
240 lines
9.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using AspNetCoreRateLimit;
|
|
using Blog3000.Client;
|
|
using Blog3000.Server.Utils;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
using Syslog.Framework.Logging;
|
|
|
|
namespace Blog3000.Server
|
|
{
|
|
public class Program
|
|
{
|
|
|
|
public static string blogDataPath = null;
|
|
|
|
|
|
public static bool xCleanTweets = false;
|
|
|
|
private static string syslogAppName;
|
|
|
|
public static async Task Main(string[] args)
|
|
{
|
|
Console.WriteLine($"Blog3000 0.1 Build {Blog3000.Server.BuildVersion.BUILD_DATE}");
|
|
|
|
//if (args.Length == 0)
|
|
//{
|
|
// ShowHelp();
|
|
// Environment.Exit(1);
|
|
//}
|
|
|
|
foreach (var a in args)
|
|
{
|
|
Console.WriteLine($"Processing parameter {a}");
|
|
|
|
if (a.StartsWith("--help") || a.StartsWith("-h") || a.StartsWith("-?") || a.StartsWith("(/h") || a.StartsWith("/?"))
|
|
{
|
|
ShowHelp();
|
|
Environment.Exit(0);
|
|
}
|
|
/*if (a.StartsWith("--port="))
|
|
{
|
|
revProxyMode = a.Substring("--port=".Length);
|
|
}
|
|
else if (a.StartsWith("--visible-url-prefix="))
|
|
{
|
|
visibleUrlPrefix= a.Substring("--visible-url-prefix=".Length);
|
|
}
|
|
|
|
else*/ if (a.StartsWith("--blog-data-path="))
|
|
{
|
|
blogDataPath = a.Substring("--blog-data-path=".Length);
|
|
}
|
|
/*else if (a.StartsWith("--public-dl-uri="))
|
|
{
|
|
publicDlUri = a.Substring("--public-dl-uri=".Length);
|
|
}
|
|
else if (a.StartsWith("--allow-search-bots"))
|
|
{
|
|
allowSearchBots = true;
|
|
}
|
|
else if (a.StartsWith("--allow-search-devbot"))
|
|
{
|
|
allowSearchDevBot = true;
|
|
}
|
|
else if (a.StartsWith("--x-clean-tweets"))
|
|
{
|
|
xCleanTweets = true;
|
|
}*/
|
|
}
|
|
|
|
Console.WriteLine($"Active parameters");
|
|
/*Console.WriteLine($"- Blog data path: {blogDataPath}");
|
|
Console.WriteLine($"- PublicDlUri: {publicDlUri}");
|
|
Console.WriteLine($"- Vislble url-prefix: {visibleUrlPrefix}");
|
|
Console.WriteLine($"- Using port: {revProxyMode}");
|
|
Console.WriteLine($"- Allow search bots: {allowSearchBots}");
|
|
Console.WriteLine($"- Allow search-devbot: {allowSearchDevBot} ('xx-testbot-xx')");
|
|
Console.WriteLine("");
|
|
Console.WriteLine($"- Clean tweets/disable: {xCleanTweets} ");*/
|
|
|
|
|
|
foreach (var a in args)
|
|
{
|
|
if (a.StartsWith("--set-protected="))
|
|
{
|
|
SetConfig(a.Substring("--set-protected=".Length), true);
|
|
}
|
|
}
|
|
|
|
|
|
//if (String.IsNullOrWhiteSpace(visibleUrlPrefix))
|
|
//{
|
|
// Console.Error.WriteLine("Paramter --visible-url-prefix missing. Abort");
|
|
// Environment.Exit(1);
|
|
//}
|
|
|
|
var env = BlogEnv.LoadConfig();
|
|
if (String.IsNullOrWhiteSpace(env.VisibleUrlPrefix))
|
|
{
|
|
Console.Error.WriteLine("Parameter VisibleUrlPrefix missing in blogconfig.json. Abort");
|
|
Environment.Exit(1);
|
|
}
|
|
|
|
|
|
syslogAppName = env.SyslogAppName;
|
|
|
|
//Console.WriteLine(env.ToString());
|
|
Console.WriteLine($"- Blog data path: {blogDataPath ?? "<AppDataPath>"}");
|
|
Console.WriteLine($"- PublicDlUri: {env.PublicDlUri}");
|
|
Console.WriteLine($"- Vislble url-prefix: {env.VisibleUrlPrefix}");
|
|
Console.WriteLine($"- Using port: {env.ListenPort ?? 5001}{(env.ListenPort != null ? "NonSSL" : "SSL")}");
|
|
Console.WriteLine($"- Allow search bots: {env.AllowSearchBots}");
|
|
Console.WriteLine($"- Allow search-devbot: {env.AllowSearchDevBot} ('xx-testbot-xx')");
|
|
Console.WriteLine($"- Log to syslog: {env.SyslogAppName ?? "<disabled>"}");
|
|
|
|
|
|
Console.WriteLine("Starting ASP.Net webhost...");
|
|
|
|
var webHost = CreateHostBuilder(args).Build();
|
|
|
|
// For IP RateLimiting
|
|
using (var scope = webHost.Services.CreateScope())
|
|
{
|
|
// get the IpPolicyStore instance
|
|
var ipPolicyStore = scope.ServiceProvider.GetRequiredService<IIpPolicyStore>();
|
|
|
|
// seed IP data from appsettings
|
|
await ipPolicyStore.SeedAsync();
|
|
}
|
|
|
|
webHost.Run();
|
|
}
|
|
|
|
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
|
Host.CreateDefaultBuilder(args)
|
|
.ConfigureLogging((hostingContext, logging) =>
|
|
{
|
|
logging.ClearProviders();
|
|
if (syslogAppName == null || System.Diagnostics.Debugger.IsAttached)
|
|
{
|
|
logging.AddConsole(options => options.IncludeScopes = false);
|
|
}
|
|
|
|
//logging.AddDebug();
|
|
if (syslogAppName != null)
|
|
{
|
|
var settings = new SyslogLoggerSettings();
|
|
settings.FacilityType = FacilityType.Local0;
|
|
settings.HeaderType = SyslogHeaderType.Rfc5424v1;
|
|
settings.MessageTransportProtocol = Syslog.Framework.Logging.TransportProtocols.TransportProtocol.UnixSocket;
|
|
//logging.AddSyslog(new ILoggingBuilder( settings);
|
|
logging.AddProvider(new MySyslogLoggerProvider(settings, syslogAppName, System.Environment.MachineName, LogLevel.Debug));
|
|
}
|
|
})
|
|
.ConfigureWebHostDefaults(webBuilder =>
|
|
{
|
|
var cfg = BlogEnv.LoadConfig();
|
|
|
|
webBuilder
|
|
//.UseUrls(revProxyMode != null ? $"http://127.0.0.1:{revProxyMode}" : "https://0.0.0.0:5001")
|
|
.UseUrls(cfg?.ListenPort != null ? $"http://127.0.0.1:{cfg.ListenPort}" : "https://0.0.0.0:5001")
|
|
.UseStartup<Startup>();
|
|
});
|
|
|
|
|
|
|
|
private static void SetConfig(string parm, bool prot)
|
|
{
|
|
Console.WriteLine("Set Config");
|
|
var bg = BlogEnv.LoadConfig();
|
|
bg.Protected ??= new BlogEnv.ProtectedConfig();
|
|
if (prot)
|
|
{
|
|
if ("twitter-api-key".Equals(parm))
|
|
{
|
|
Console.Write("Twitter API-Key> ");
|
|
var s = Console.ReadLine();
|
|
bg.Protected.TwitterAPIKey = Utils.DataProtect.Static.Protect(s);
|
|
}
|
|
else if ("twitter-api-secret".Equals(parm))
|
|
{
|
|
Console.Write("Twitter API-Secret> ");
|
|
var s = Console.ReadLine();
|
|
bg.Protected.TwitterAPISecret = Utils.DataProtect.Static.Protect(s);
|
|
}
|
|
else if ("twitter-access-token".Equals(parm))
|
|
{
|
|
Console.Write("Twitter Access-Token> ");
|
|
var s = Console.ReadLine();
|
|
bg.Protected.TwitterAccessToken = Utils.DataProtect.Static.Protect(s);
|
|
}
|
|
else if ("twitter-access-secret".Equals(parm))
|
|
{
|
|
Console.Write("Twitter Access-Secret> ");
|
|
var s = Console.ReadLine();
|
|
bg.Protected.TwitterAccessSecret = Utils.DataProtect.Static.Protect(s);
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("Bad params");
|
|
Environment.Exit(1);
|
|
}
|
|
BlogEnv.SaveConfig(bg);
|
|
Environment.Exit(0);
|
|
}
|
|
|
|
Console.WriteLine("Bad params");
|
|
Environment.Exit(1);
|
|
}
|
|
|
|
|
|
private static void ShowHelp()
|
|
{
|
|
Console.WriteLine($"Available Parameters: ");
|
|
Console.WriteLine($" --blog-data-path= [default app-data-path]");
|
|
//Console.WriteLine($" --visible-url-prefix=https://mydemoblog.com (prefix for blogpost-links e.g in sitemap");
|
|
//Console.WriteLine($" --port= [default 5001]");
|
|
|
|
//Console.WriteLine($" --public-dl-uri= [default null]");
|
|
//Console.WriteLine($" --allow-search-bots [default false]");
|
|
//Console.WriteLine($" --allow-search-devbot [default false]");
|
|
//Console.WriteLine();
|
|
//Console.WriteLine($" --x-clean-tweets [default false], ");
|
|
//Console.WriteLine(" tweets reappear on false if was true (user-annoyance)!");
|
|
Console.WriteLine("");
|
|
Console.WriteLine("Special interactive ops: ");
|
|
Console.WriteLine(" --set-protected=");
|
|
Console.WriteLine(" twitter-api-key,twitter-api-secret,");
|
|
Console.WriteLine(" twitter-access-token,twitter-access-key");
|
|
|
|
}
|
|
}
|
|
}
|