Skip to content

Commit

Permalink
Adding flexibility on network, and including docker service name, if …
Browse files Browse the repository at this point in the history
…there
  • Loading branch information
RAhnemann committed Dec 17, 2019
1 parent a564b2d commit e6b1112
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
30 changes: 24 additions & 6 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,28 @@ namespace windows_hosts_writer
class Program
{
private const string ENV_ENDPOINT = "endpoint";
private const string ENV_NETWORK = "network";
private const string ENV_HOSTPATH = "hosts_path";
private const string ERROR_HOSTPATH = "could not change hosts file at {0} inside of the container. You can change that path through environment variable " + ENV_HOSTPATH;
private const string EVENT_MSG = "got a {0} event from {1}";
private static string LISTEN_NETWORK = "nat";
private static DockerClient _client;
private static bool _debug = false;

static void Main(string[] args)
{
if (Environment.GetEnvironmentVariable("debug") != null) {
if (Environment.GetEnvironmentVariable("debug") != null)
{
_debug = true;
Console.WriteLine("Starting Windows hosts writer");
}

if (Environment.GetEnvironmentVariable(ENV_NETWORK) != null)
{
LISTEN_NETWORK = Environment.GetEnvironmentVariable(ENV_NETWORK);
}


var progress = new Progress<Message>(message =>
{
if (message.Action == "connect")
Expand Down Expand Up @@ -116,7 +125,7 @@ private static void HandleHosts(bool add, Message message)
Console.WriteLine(ERROR_HOSTPATH, hostsPath);
return;
}
catch (IOException)
catch (IOException ex)
{
if (tryCount == 5)
{
Expand All @@ -130,11 +139,12 @@ private static void HandleHosts(bool add, Message message)
if (message.Actor.Attributes["type"] == "nat")
{
var containerId = message.Actor.Attributes["container"];
try {
try
{
var response = GetClient().Containers.InspectContainerAsync(containerId).Result;
var networks = response.NetworkSettings.Networks;
EndpointSettings network = null;
if (networks.TryGetValue("nat", out network))
if (networks.TryGetValue(LISTEN_NETWORK, out network))
{
var hostsLines = new List<string>();
using (StreamReader reader = new StreamReader(hostsFileStream))
Expand All @@ -145,16 +155,24 @@ private static void HandleHosts(bool add, Message message)

hostsFileStream.Position = 0;
var removed = hostsLines.RemoveAll(l => l.EndsWith($"#{containerId} by whw"));
var hostnames = response.Config.Hostname;

if (response.Config.Labels.ContainsKey("com.docker.compose.service"))
{
hostnames += $" {response.Config.Labels["com.docker.compose.service"]}";
}

if (add)
hostsLines.Add($"{network.IPAddress}\t{response.Config.Hostname}\t\t#{containerId} by whw");
hostsLines.Add($"{network.IPAddress}\t{hostnames}\t\t#{containerId} by whw");

foreach (var line in hostsLines)
writer.WriteLine(line);
hostsFileStream.SetLength(hostsFileStream.Position);
}
}
} catch (Exception ex) {
}
catch (Exception ex)
{
if (_debug)
{
Console.WriteLine("Something went wrong. Maybe looking for a container that is already gone? Exception is " + ex.Message);
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# windows-hosts-writer

###Updated: 12-17-19
- Allows for configuration of the network to monitor. Set the "network" environment variable
- Adds the docker compose service name (for better user experience)


Small tool that monitors the Docker engine and modifies the hosts file on Windows to allow easier networking

You can run this natively as well but as you need to have Docker running anyways to use it, the easiest way is:
Expand Down
2 changes: 1 addition & 1 deletion windows-hosts-writer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>windows_hosts_writer</RootNamespace>
</PropertyGroup>

Expand Down

0 comments on commit e6b1112

Please sign in to comment.