Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Bring in windows DNS modifier from neckonet

  • Loading branch information...
commit dd9080c112f91479ee716a7341908cfcbc521acd 1 parent fe687b5
Nicholas Hurley todesschaf authored
15 windows/README
View
@@ -0,0 +1,15 @@
+This contains the source code and executable for the Stone Ridge DNS modifier
+that runs on windows.
+
+It's C#. To build it (if you really feel like building your own copy, or if you
+want to change things), create a new C# console project in Visual Studio.
+Overwrite the file it generates with srdns.cs from this directory. You need to
+add a reference to the System.ServiceProcess assembly to your project before
+you build this (in the Solution Explorer, right-click on "References", select
+"Add Reference", and then on the .NET tab, select "System.ServiceProcess" and
+hit "OK").
+
+Your resulting exe can be added as a service using sc.exe (see
+install.py for an example of the command line used).
+
+Enjoy
12 windows/install.py
View
@@ -0,0 +1,12 @@
+import os
+import subprocess
+
+mydir = os.path.split(os.path.abspath(__file__))[0]
+
+# Install the dns modifier service
+windnssvc = os.path.join(mydir, 'srdns.exe')
+subprocess.call(['sc.exe', 'create', 'srdns', 'binPath=', windnssvc,
+ 'DisplayName=', 'Stone Ridge DNS Modifier', 'start=', 'auto'])
+
+# Start the dns modifier service
+subprocess.call(['sc.exe', 'start', 'srdns'])
124 windows/srdns.cs
View
@@ -0,0 +1,124 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Diagnostics;
+using System.ServiceProcess;
+using System.Threading;
+using System.Net.Sockets;
+using System.Net;
+
+namespace neckodnssvc
+{
+ class NeckoDnsSvc : System.ServiceProcess.ServiceBase
+ {
+ protected TimeSpan stopdelay;
+ protected ManualResetEvent shutdownevt;
+ protected Thread thread;
+
+ public NeckoDnsSvc()
+ {
+ stopdelay = new TimeSpan(0, 0, 0, 1);
+ }
+
+ public void setDns(string dns)
+ {
+ Process proc = new Process();
+ proc.StartInfo.FileName = "netsh.exe";
+ proc.StartInfo.Arguments = "interface ipv4 set dnsservers \"Local Area Connection\" static " + dns + " validate=no";
+ proc.StartInfo.UseShellExecute = false;
+ proc.StartInfo.RedirectStandardOutput = true;
+ proc.Start();
+ proc.WaitForExit();
+ }
+
+ public void resetDns()
+ {
+ Process proc = new Process();
+ proc.StartInfo.FileName = "netsh.exe";
+ proc.StartInfo.Arguments = "interface ipv4 set dnsservers \"Local Area Connection\" dhcp";
+ proc.StartInfo.UseShellExecute = false;
+ proc.StartInfo.RedirectStandardOutput = true;
+ proc.Start();
+ proc.WaitForExit();
+ }
+
+ public void svcMain()
+ {
+ // Set up our master socket
+ Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
+ IPEndPoint e = new IPEndPoint(IPAddress.Loopback, 63250);
+ s.Bind(e);
+ s.Listen(0);
+
+ // Main "event" loop
+ while (true)
+ {
+ // See if we're supposed to shut down
+ if (shutdownevt.WaitOne(stopdelay, true))
+ break;
+
+ // Wait 1 second (in us) for a connection
+ if (!s.Poll(10000, SelectMode.SelectRead))
+ continue;
+
+ Socket sock = s.Accept();
+
+ // Got a connection, figure out what to do (first 2 bytes define the operation)
+ byte[] msghdr = new byte[2];
+ sock.Receive(msghdr, 2, SocketFlags.None);
+
+ string shdr = Encoding.ASCII.GetString(msghdr);
+ if (shdr[0].Equals('s'))
+ {
+ // We got a set command. It contains the length of the dotted quad (in
+ // bytes). Read the ip from the socket and set it
+ byte[] ip = new byte[msghdr[1]];
+ sock.Receive(ip, msghdr[1], SocketFlags.None);
+ string ipstr = Encoding.ASCII.GetString(ip);
+ this.setDns(ipstr);
+ }
+ else if (shdr[0].Equals('r'))
+ {
+ // We got a reset command
+ this.resetDns();
+ }
+ else
+ {
+ string data = Encoding.ASCII.GetString(msghdr);
+ Console.WriteLine("Got message: " + data);
+ }
+
+ // Send our response
+ byte[] resp = new byte[2];
+ resp[0] = (byte)'o';
+ resp[1] = (byte)'k';
+ sock.Send(resp);
+ sock.Close();
+ }
+
+ // Clean up!
+ s.Close();
+ }
+
+ protected override void OnStart(string[] args)
+ {
+ ThreadStart ts = new ThreadStart(this.svcMain);
+ shutdownevt = new ManualResetEvent(false);
+ thread = new Thread(ts);
+ thread.Start();
+ }
+
+ protected override void OnStop()
+ {
+ shutdownevt.Set();
+ thread.Join(60000); // wait up to 1 min for thread to stop
+ }
+
+ static void Main(string[] args)
+ {
+ ServiceBase[] svcs = new ServiceBase[] { new NeckoDnsSvc() };
+ ServiceBase.Run(svcs);
+ }
+ }
+}
BIN  windows/srdns.exe
View
Binary file not shown
Please sign in to comment.
Something went wrong with that request. Please try again.