Skip to content

Commit

Permalink
Small fixes (Merge branch 'dev')
Browse files Browse the repository at this point in the history
  • Loading branch information
Myster-Tee committed Jan 13, 2024
2 parents 27f7083 + c472634 commit ed41c38
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 22 deletions.
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,23 @@ dotnet TinfoilWebServer.dll
1. Go to **File Browser**
1. Press **[-]** button to add a new server
1. Set **Protocol** to HTTP or HTTPS according to the server configuration
1. Set **Host** to any host pointing to your server (or the server IP address)
_The server IP address is logged at server startup_
1. Set **Host** to any host pointing to your server (or the server IP address)
_The server IP address is logged at server startup._
1. If authentication is enabled, set **Username** and **Password** to one of the allowed users
1. Set **Title** to a name of your choice

***Download speed note**: having a download/installation speed of ~10MB from Tinfoil is normal.
This low speed is only due to hardware limitation of Nintendo Switch, and is not at all related to some server limitation.*


## TinfoilWebServer.config.json format

```js
{
"ServedDirectories": string[], // ex: ["C:\\SomeDir\\WindowsDirWithPackages", "/dev/sda1/LinuxDirWithPackages", ".", "/Users/yourname/Documents/macOSDirWithPackages"] !!! Don't forget to escape backslashes with another one !!! No need to escape spaces
"StripDirectoryNames": boolean, // «true» to remove directories names in URLs of served files, «false» otherwise
"ServeEmptyDirectories": boolean, // «true» to serve empty directories, «false» otherwise (has no effect when "StripDirectoryNames" is «true»)
"AllowedExt": string[], // List of file extensions to serve (default is ["xci", "nsz", "nsp"])
"AllowedExt": string[], // List of file extensions to serve (default is ["xci", "nsz", "nsp", "xcz", "zip"])
"MessageOfTheDay": string, // The welcome message displayed when Tinfoil successfully contacts the server
"CustomIndexPath": string, // The path to a custom JSON file to be merged with the served index
"Cache": {
Expand Down Expand Up @@ -168,7 +172,6 @@ dotnet TinfoilWebServer.dll

When *"Kestrel"* configuration is omitted, server default listens to _http://localhost:5000/_ and _https://localhost:5001_.


### Custom Index

Specifying a custom index in the configuration file allows to set (or combine) any extra property described in [Tinfoil Custom Index documentation](https://blawar.github.io/tinfoil/custom_index/).
Expand Down Expand Up @@ -227,7 +230,7 @@ Example:
### Running as a Windows service

Server can be run as a Windows service. To to this, service must first be registered using [sc.exe](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/sc-config), PowerShell [New-Service](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/new-service) cmdlet or any other method.
Don't forget to specify the **--winService** command line parameter when registering the service.
Don't forget to specify the `--winService` command line parameter when registering the service.

*Note: by default, Windows services are run in **%WinDir%\System32**, you can change the current directory using **--currentDir** command line parameter.*

Expand All @@ -236,6 +239,11 @@ Don't forget to specify the **--winService** command line parameter when registe
New-Service TinfoilWebServer -BinaryPathName "<FullPathToExe> --winService --config ""<SomePathToConfigJson>"" --currentDir ""<SomePathToCurrentDir>"""
```

## Sharing game saves

TinfoilWebServer can serve game saves by allowing "zip" file extension (by default).
To share your own saves, copy Tinfoil saves from `SDCard/switch/tinfoil/saves/**/*.zip` to any of the served directories, preserving original file name.

## Security considerations and recommendations

If you plan to open your server to the Internet network (WAN) instead of local network (LAN) only, I would highly recommend you to:
Expand All @@ -250,7 +258,7 @@ If you plan to open your server to the Internet network (WAN) instead of local n
1. Set *Authentication.PwdType* to "Sha256" to avoid storing plaintext user passwords in your config file


# Similar Projects
## Similar Projects
If you want to create your personal NSP Shop then check out these other similar projects:
- [a1ex4/Ownfoil](https://github.com/a1ex4/ownfoil)
- [eXhumer/pyTinGen](https://github.com/eXhumer/pyTinGen)
Expand Down
9 changes: 7 additions & 2 deletions TinfoilWebServer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.AspNetCore.Hosting.WindowsServices;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using TinfoilWebServer.Booting;
Expand Down Expand Up @@ -133,6 +134,12 @@ public static int Main(string[] args)
logger.LogError(ex, $"An unhandled exception occurred: {ex?.Message ?? eventArgs.ExceptionObject}");
};

webHost.Services.GetRequiredService<IHostApplicationLifetime>().ApplicationStopped.Register(() =>
{
// On Linux, logs are not written to file if called after webhost.Run(), but works here
logger.LogInformation("Server stopped.");
});

//===========================//
//===> Starts the server <===//
if (bootInfo.CmdOptions.RunAsWindowsService)
Expand All @@ -142,8 +149,6 @@ public static int Main(string[] args)
//===> Starts the server <===//
//===========================//

logger.LogInformation("Server closing.");

return 0;
}
catch (Exception ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private void OnFingerprintsFilterSettingsChanged(object? sender, PropertyChanged
{
_logger.LogInformation("Fingerprints file path changed, reloading fingerprints.");
Initialize();
}
}
else if (e.PropertyName == nameof(IFingerprintsFilterSettings.MaxFingerprints))
{
_logger.LogInformation($"Max global allowed fingerprints updated to {_fingerprintsFilterSettings.MaxFingerprints}.");
Expand All @@ -73,18 +73,29 @@ private void OnAuthenticationSettingsChanged(object? sender, PropertyChangedEven

private void CheckSettingsConsistency()
{
if (_fingerprintsFilterSettings.Enabled && _authenticationSettings is { Enabled: true, WebBrowserAuthEnabled: true })
if (!_fingerprintsFilterSettings.Enabled)
return;

// Here fingerprint filtering is enabled

if (_authenticationSettings.Enabled)
{
_logger.LogWarning($"Inconsistent configuration: Web Browser authentication is enabled ({nameof(IAuthenticationSettings.WebBrowserAuthEnabled)}) " +
$"as well as fingerprints filtering ({nameof(IFingerprintsFilterSettings.MaxFingerprints)}), " +
$"but Web Browsers never send fingerprints, only Tinfoil do.");
if (_authenticationSettings.WebBrowserAuthEnabled)
_logger.LogWarning(
$"Inconsistent configuration: Web Browser authentication is enabled ({nameof(IAuthenticationSettings.WebBrowserAuthEnabled)}) " +
$"as well as fingerprints filtering ({nameof(IFingerprintsFilterSettings.MaxFingerprints)}), " +
$"but Web Browsers never send fingerprints, only Tinfoil do.");

var allowedUsers = _authenticationSettings.Users;
if (allowedUsers.Count > 0 && allowedUsers.All(user => user.MaxFingerprints <= 0))
_logger.LogWarning("Inconsistent configuration: authentication is enabled as well as fingerprints filtering but the number of allowed fingerprints is 0 for all allowed users.");
}

if (_fingerprintsFilterSettings is { Enabled: true, MaxFingerprints: <= 0 } &&
_authenticationSettings.Users.All(user => user.MaxFingerprints <= 0))
else
{
_logger.LogWarning("Inconsistent configuration: fingerprints filtering is enabled but the number of allowed fingerprints is 0.");
if (_fingerprintsFilterSettings.MaxFingerprints <= 0)
_logger.LogWarning("Inconsistent configuration: fingerprints filtering is enabled but the number of allowed fingerprints is 0.");
}

}

public bool AcceptFingerprint(string? fingerprint, IUserInfo? userInfo, string traceId)
Expand Down
4 changes: 4 additions & 0 deletions TinfoilWebServer/Services/VirtualFileSystemRootProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ private void OnAppSettingsChanged(object? sender, PropertyChangedEventArgs e)
{
_ = SafeRefresh();
}
else if (e.PropertyName == nameof(IAppSettings.AllowedExt))
{
_ = SafeRefresh();
}
}

public async Task SafeRefresh()
Expand Down
8 changes: 6 additions & 2 deletions TinfoilWebServer/Settings/AppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ public AppSettings(IOptionsMonitor<AppSettingsModel> appSettingsModel, ILogger<A
appSettingsModel = appSettingsModel ?? throw new ArgumentNullException(nameof(appSettingsModel));
InitializeFromModel(appSettingsModel.CurrentValue);

appSettingsModel.OnChange(InitializeFromModel);
appSettingsModel.OnChange(model =>
{
_logger.LogDebug("Settings change detected.");
InitializeFromModel(model);
});
}

/// <summary>
Expand All @@ -49,7 +53,7 @@ private void InitializeFromModel(AppSettingsModel appSettingsModel)
ServeEmptyDirectories = appSettingsModel.ServeEmptyDirectories ?? true;

var allowedExt = appSettingsModel.AllowedExt;
AllowedExt = allowedExt == null || allowedExt.Length == 0 ? new[] { "xci", "nsz", "nsp" } : allowedExt;
AllowedExt = allowedExt == null || allowedExt.Length == 0 ? new[] { "xci", "nsz", "nsp", "xcz", "zip" } : allowedExt;

MessageOfTheDay = string.IsNullOrWhiteSpace(appSettingsModel.MessageOfTheDay) ? null : appSettingsModel.MessageOfTheDay;

Expand Down
2 changes: 1 addition & 1 deletion TinfoilWebServer/TinfoilWebServer.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"ServedDirectories": [ "D:\\Switch\\Packages", "/dev/sda1/Switch/Packages" ],
"StripDirectoryNames": true,
"ServeEmptyDirectories": false,
"AllowedExt": [ "nsp", "nsz", "xci" ],
"AllowedExt": [ "nsp", "nsz", "xci", "xcz" ],
"MessageOfTheDay": "Hello World!",
"CustomIndexPath": null,
"Cache": {
Expand Down
2 changes: 1 addition & 1 deletion TinfoilWebServer/TinfoilWebServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<UserSecretsId>437ef1a7-7fbd-490e-9580-9a3cf8c175d5</UserSecretsId>
<ApplicationIcon>Resources\Icon.ico</ApplicationIcon>
<StartupObject>TinfoilWebServer.Program</StartupObject>
<Version>2.0.0</Version>
<Version>2.0.1</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down
2 changes: 1 addition & 1 deletion TinfoilWebServer/Utils/LoggerHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static class LoggerHelper
public static void LogWelcomeMessage(this ILogger logger)
{
var version = Assembly.GetExecutingAssembly().GetName().Version!;
logger.LogInformation($"Welcome to Tinfoil Web Server v{version.Major}.{version.Minor}.{version.Build} (press CTRL+C to exit)");
logger.LogInformation($"Welcome to TinfoilWebServer v{version.Major}.{version.Minor}.{version.Build} (press CTRL+C to exit)");
}

public static void LogBootInfo(this ILogger logger, IBootInfo bootInfo)
Expand Down

0 comments on commit ed41c38

Please sign in to comment.