This repository was archived by the owner on Nov 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update /api/info to be more extensible * Parse configs at startup * Make the error page respect user's theme * Delete obsolete code * Bump API version * Formatting, and change how LIGHTTUBE_MOTD works * Implement the alert in the page layout
- Loading branch information
Showing
25 changed files
with
194 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,90 @@ | ||
using System.Text.RegularExpressions; | ||
using InnerTube; | ||
using Newtonsoft.Json; | ||
|
||
namespace LightTube; | ||
|
||
public static class Configuration | ||
{ | ||
private static Dictionary<string, string> _variables = new(); | ||
private static Dictionary<string, string> _customThemeDefs = null; | ||
public static Dictionary<string, string> CustomThemeDefs { get; } = new(); | ||
public static InnerTubeAuthorization? InnerTubeAuthorization { get; private set; } | ||
public static bool ApiEnabled { get; private set; } | ||
public static bool OauthEnabled { get; private set; } | ||
public static bool RegistrationEnabled { get; private set; } | ||
public static bool ProxyEnabled { get; private set; } | ||
public static bool ThirdPartyProxyEnabled { get; private set; } | ||
public static int CacheSize { get; private set; } | ||
public static string ConnectionString { get; private set; } | ||
public static string Database { get; private set; } | ||
public static string DefaultContentLanguage { get; private set; } = "en"; | ||
public static string DefaultContentRegion { get; private set; } = "US"; | ||
public static string DefaultTheme { get; private set; } = "auto"; | ||
public static string? CustomCssPath { get; private set; } | ||
public static string[] Messages { get; private set; } | ||
public static string? Alert { get; private set; } | ||
public static string? AlertHash { get; private set; } | ||
private static Random random = new(); | ||
|
||
public static string? GetVariable(string var, string? def = null) | ||
private static string? GetVariable(string var, string? def = null) => | ||
Environment.GetEnvironmentVariable(var) ?? def; | ||
|
||
public static void InitConfig() | ||
{ | ||
if (_variables.TryGetValue(var, out string? res)) return res; | ||
string? v = Environment.GetEnvironmentVariable(var) ?? def; | ||
if (v == null) return null; | ||
_variables.Add(var, v); | ||
return v; | ||
} | ||
|
||
public static InnerTubeAuthorization? GetInnerTubeAuthorization() => | ||
GetVariable("LIGHTTUBE_AUTH_TYPE")?.ToLower() switch | ||
InnerTubeAuthorization = Environment.GetEnvironmentVariable("LIGHTTUBE_AUTH_TYPE")?.ToLower() switch | ||
{ | ||
"cookie" => InnerTubeAuthorization.SapisidAuthorization( | ||
GetVariable("LIGHTTUBE_AUTH_SAPISID") ?? | ||
Environment.GetEnvironmentVariable("LIGHTTUBE_AUTH_SAPISID") ?? | ||
throw new ArgumentNullException("LIGHTTUBE_AUTH_SAPISID", | ||
"Authentication type set to 'cookie' but the 'LIGHTTUBE_AUTH_SAPISID' environment variable is not set."), | ||
GetVariable("LIGHTTUBE_AUTH_PSID") ?? | ||
Environment.GetEnvironmentVariable("LIGHTTUBE_AUTH_PSID") ?? | ||
throw new ArgumentNullException("LIGHTTUBE_AUTH_PSID", | ||
"Authentication type set to 'cookie' but the 'LIGHTTUBE_AUTH_PSID' environment variable is not set.")), | ||
"oauth2" => InnerTubeAuthorization.RefreshTokenAuthorization( | ||
GetVariable("LIGHTTUBE_AUTH_REFRESH_TOKEN") ?? | ||
Environment.GetEnvironmentVariable("LIGHTTUBE_AUTH_REFRESH_TOKEN") ?? | ||
throw new ArgumentNullException("LIGHTTUBE_AUTH_REFRESH_TOKEN", | ||
"Authentication type set to 'oauth2' but the 'LIGHTTUBE_AUTH_REFRESH_TOKEN' environment variable is not set.")), | ||
var _ => null | ||
_ => null | ||
}; | ||
|
||
public static Dictionary<string, string> GetCustomThemeDefs() | ||
{ | ||
if (_customThemeDefs == null) | ||
CustomCssPath = Environment.GetEnvironmentVariable("LIGHTTUBE_CUSTOM_CSS_PATH"); | ||
if (CustomCssPath != null) | ||
{ | ||
Dictionary<string, string> dict = new(); | ||
string? fileName = GetVariable("LIGHTTUBE_CUSTOM_CSS_PATH"); | ||
|
||
if (fileName != null) | ||
string contents = File.ReadAllText(CustomCssPath); | ||
MatchCollection matches = Regex.Matches(contents, "@themedef \"(.+?)\" (\\S+)"); | ||
foreach (Match match in matches) | ||
{ | ||
using FileStream fs = File.OpenRead(fileName); | ||
using StreamReader sr = new(fs); | ||
string contents = sr.ReadToEnd(); | ||
fs.Close(); | ||
MatchCollection matches = Regex.Matches(contents, "@themedef \"(.+?)\" (\\S+)"); | ||
foreach (Match match in matches) | ||
{ | ||
dict.Add(match.Groups[2].Value, match.Groups[1].Value); | ||
} | ||
CustomThemeDefs.Add(match.Groups[2].Value, match.Groups[1].Value); | ||
} | ||
} | ||
|
||
ApiEnabled = GetVariable("LIGHTTUBE_DISABLE_API", "false")?.ToLower() != "true"; | ||
OauthEnabled = GetVariable("LIGHTTUBE_DISABLE_OAUTH", "false")?.ToLower() != "true"; | ||
RegistrationEnabled = GetVariable("LIGHTTUBE_DISABLE_REGISTRATION", "false")?.ToLower() != "true"; | ||
ProxyEnabled = GetVariable("LIGHTTUBE_DISABLE_PROXY", "false")?.ToLower() != "true"; | ||
ThirdPartyProxyEnabled = GetVariable("LIGHTTUBE_ENABLE_THIRD_PARTY_PROXY", "false")?.ToLower() != "true" && ProxyEnabled; | ||
|
||
_customThemeDefs = dict; | ||
CacheSize = int.Parse(GetVariable("LIGHTTUBE_CACHE_SIZE", "50")!); | ||
ConnectionString = GetVariable("LIGHTTUBE_MONGODB_CONNSTR") ?? throw new ArgumentNullException( | ||
"LIGHTTUBE_MONGODB_CONNSTR", | ||
"Database connection string is not set. Please set the 'LIGHTTUBE_MONGODB_CONNSTR' to a valid MongoDB connection string."); | ||
Database = GetVariable("LIGHTTUBE_MONGODB_DATABASE", "lighttube")!; | ||
DefaultContentLanguage = GetVariable("LIGHTTUBE_DEFAULT_CONTENT_LANGUAGE", "en")!; | ||
DefaultContentRegion = GetVariable("LIGHTTUBE_DEFAULT_CONTENT_REGION", "US")!; | ||
DefaultTheme = GetVariable("LIGHTTUBE_DEFAULT_THEME", "auto")!; | ||
|
||
try | ||
{ | ||
Messages = JsonConvert.DeserializeObject<string[]>(GetVariable("LIGHTTUBE_MOTD", | ||
"[\"Search something to get started!\"]")!)!; | ||
} | ||
catch (Exception) | ||
{ | ||
Messages = new[] { "Search something to get started!" }; | ||
} | ||
|
||
return _customThemeDefs; | ||
Alert = GetVariable("LIGHTTUBE_ALERT") ?? ":3 check! press the X to :3"; | ||
AlertHash = Alert != null ? Utils.Md5Sum(Alert) : null; | ||
} | ||
|
||
public static string RandomMessage() => Messages[random.Next(0, Messages.Length)]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.