Skip to content

Commit 25b3ab6

Browse files
committed
Added hosts override
1 parent f39f317 commit 25b3ab6

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

DnsOverride/Program.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
using System;
22
using System.ComponentModel;
33
using System.Diagnostics;
4+
using System.IO;
5+
using System.Net.Http;
46
using System.Security.Principal;
57
using System.ServiceProcess;
8+
using System.Threading.Tasks;
9+
using System.Net.Http;
610

711
namespace DnsOverride
812
{
913
internal static class Program
1014
{
1115
private const string ServiceName = "DnsOverride";
1216

13-
static void Main()
17+
static async Task Main()
1418
{
1519
if (Environment.UserInteractive)
1620
{
@@ -54,11 +58,6 @@ private static bool ServiceExists(string serviceName)
5458
return false;
5559
}
5660

57-
private static void InstallService()
58-
{
59-
60-
}
61-
6261
public static bool IsUserAdministrator()
6362
{
6463
try

DnsOverride/Service1.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
using System.ComponentModel;
44
using System.Data;
55
using System.Diagnostics;
6+
using System.IO;
67
using System.Linq;
78
using System.Management;
9+
using System.Net.Http;
810
using System.ServiceProcess;
911
using System.Text;
1012
using System.Threading.Tasks;
@@ -15,6 +17,8 @@ namespace DnsOverride
1517
public partial class Service1 : ServiceBase
1618
{
1719
private Timer timer;
20+
string hosts = Path.Combine(Environment.SystemDirectory, @"drivers\etc\hosts");
21+
string url = "https://raw.githubusercontent.com/LasseTheBabo/files/master/hosts";
1822

1923
public Service1()
2024
{
@@ -26,20 +30,31 @@ protected override void OnStart(string[] args)
2630
Log("DnsOverride gestartet.");
2731

2832
timer = new Timer(60 * 1000);
29-
timer.Elapsed += ((sernder, e) => SetDns());
33+
timer.Elapsed += async (sender, e) => await SetDns();
34+
3035
timer.AutoReset = true;
3136
timer.Start();
3237

33-
SetDns();
38+
Task.Run(() => SetDns());
3439
}
3540

3641
protected override void OnStop()
3742
{
3843
Log("DnsOverride gestoppt.");
3944
}
4045

41-
public void SetDns()
46+
public async Task SetDns()
4247
{
48+
using (HttpClient client = new HttpClient())
49+
{
50+
try
51+
{
52+
string content = await client.GetStringAsync(url);
53+
File.WriteAllText(hosts, content);
54+
}
55+
catch { }
56+
}
57+
4358
try
4459
{
4560
string[] dns = { "1.1.1.3" };
@@ -69,7 +84,7 @@ public void SetDns()
6984
}
7085
}
7186

72-
private static void Log(string message)
87+
internal static void Log(string message)
7388
{
7489
try
7590
{

0 commit comments

Comments
 (0)