-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for registering hidden services
Usage: ``` add Adds a service to register on the Tor network Usage: tor [options] add <name> <service> Arguments: <name> Name of the service to register <service> Address and port of the local service being registered, such as 127.0.0.1:8080 Options: -p, --port <port> Optional port on the Tor network to listen on, if different than the service port -?, -h, --help Show help and usage information ``` Service directory is set to `~/tor/[name]` since the service keys and address should be reused across tool reinstalls/updates (especially updates of the tor binaries themselves). The configuration is saved to `~/tor/.netconfig` so it's also preserved across reinstalls. Example configuration: ``` [tor] proxy = 1337 socks = 1338 control = 1339 [tor "echo"] port = 8080 service = 127.0.0.1:8080 ``` The `torrc` file is updated accordingly after configuration and before starting, so that it picks up our changes. > NOTE: this is possible because we depend on our fork of TorSharp which implements the fix proposed in joelverhagen/TorSharp#70. Fixes #1
- Loading branch information
Showing
11 changed files
with
164 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ artifacts | |
pack | ||
.vs | ||
.vscode | ||
/tor | ||
|
||
*.suo | ||
*.sdf | ||
|
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
using System.CommandLine; | ||
using System.CommandLine.Invocation; | ||
using System.Threading; | ||
|
||
class AddCommand : Command | ||
{ | ||
public AddCommand() : base("add", "Adds a service to register on the Tor network") | ||
{ | ||
Add(new Argument<string>("name", "Name of the service to register")); | ||
Add(new Argument<string>("service", "Address and port of the local service being registered, such as 127.0.0.1:8080")); | ||
Add(new Option<int?>(new[] { "--port", "-p" }, "Optional port on the Tor network to listen on, if different than the service port")); | ||
|
||
Handler = CommandHandler.Create<string, int?, string, CancellationToken>(Run); | ||
} | ||
|
||
static void Run(string name, int? port, string service, CancellationToken cancellation) | ||
{ | ||
if (!Uri.TryCreate("http://" + service, UriKind.Absolute, out var uri)) | ||
throw new ArgumentException("Service specified is not valid. It should be an IP:PORT combination: " + service); | ||
|
||
Tor.Config.GetSection("tor", name) | ||
.SetNumber("port", port ?? uri.Port) | ||
.SetString("service", service); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
"profiles": { | ||
"dotnet-tor": { | ||
"commandName": "Project", | ||
"commandLineArgs": "-?" | ||
"commandLineArgs": "add -?" | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System; | ||
using System.IO; | ||
using System.Reflection; | ||
using DotNetConfig; | ||
|
||
static class Tor | ||
{ | ||
public static string AppPath { get; } = GetApplicationPath(); | ||
|
||
public static string DataDir { get; } = Path.Combine( | ||
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), | ||
"tor"); | ||
|
||
// We rebuild on every request since other code can mutate the | ||
// immutable structure and cause this property to become out of | ||
// sync with the persisted state. | ||
public static Config Config => Config.Build(Path.Combine(DataDir, Config.FileName)); | ||
|
||
// See GCM's Program.cs | ||
static string GetApplicationPath() | ||
{ | ||
// Assembly::Location always returns an empty string if the application was published as a single file | ||
#pragma warning disable IL3000 | ||
bool isSingleFile = string.IsNullOrEmpty(Assembly.GetEntryAssembly()?.Location); | ||
#pragma warning restore IL3000 | ||
|
||
// Use "argv[0]" to get the full path to the entry executable - this is consistent across | ||
// .NET Framework and .NET >= 5 when published as a single file. | ||
string[] args = Environment.GetCommandLineArgs(); | ||
string candidatePath = args[0]; | ||
|
||
// If we have not been published as a single file on .NET 5 then we must strip the ".dll" file extension | ||
// to get the default AppHost/SuperHost name. | ||
if (!isSingleFile && Path.HasExtension(candidatePath)) | ||
{ | ||
return Path.ChangeExtension(candidatePath, null); | ||
} | ||
|
||
return candidatePath; | ||
} | ||
} | ||
|
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
Binary file not shown.
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
@echo off | ||
pushd src\dotnet-tor\bin\Debug | ||
tor %* | ||
popd |