summaryrefslogtreecommitdiff
path: root/windows/ZeroTierOneService/Service.cs
blob: 306e8e4d8d07b1355a53a539e3bf630e4c0568f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.IO;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Threading;

namespace ZeroTierOneService
{
    public partial class Service : ServiceBase
    {
        public Service()
        {
            InitializeComponent();

            this.ztHome = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + Path.DirectorySeparatorChar + "ZeroTier" + Path.DirectorySeparatorChar + "One";
            this.ztUpdatesFolder = this.ztHome + Path.DirectorySeparatorChar + "updates.d";
            this.ztBinary = this.ztHome + Path.DirectorySeparatorChar + (Environment.Is64BitOperatingSystem ? "zerotier-one_x64.exe" : "zerotier-one_x86.exe");

            this.ztService = null;
            this.ztKiller = null;
        }

        protected override void OnStart(string[] args)
        {
            startZeroTierDaemon();
        }

        protected override void OnStop()
        {
            stopZeroTierDaemon();
        }

        private void startZeroTierDaemon()
        {
            if (ztService != null)
                return;
            ztService = new Process();
            try
            {
                ztService.StartInfo.UseShellExecute = false;
                ztService.StartInfo.FileName = ztBinary;
                ztService.StartInfo.Arguments = "";
                ztService.StartInfo.CreateNoWindow = true;
                ztService.Exited += ztService_Exited;
                ztService.Start();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                ztService = null;
            }
        }

        private void stopZeroTierDaemon()
        {
            while (ztKiller != null)
                Thread.Sleep(250);

            ztKiller = new Process();
            try
            {
                ztKiller.StartInfo.UseShellExecute = false;
                ztKiller.StartInfo.FileName = ztBinary;
                ztKiller.StartInfo.Arguments = "-q terminate ServiceShutdown";
                ztKiller.StartInfo.CreateNoWindow = true;
                ztKiller.Exited += ztKiller_Exited;
                ztKiller.Start();
            }
            catch (Exception e)
            {
                ztKiller = null;
            }

            int waited = 0;
            while (ztKiller != null)
            {
                Thread.Sleep(250);
                if (++waited > 100)
                    break;
            }

            if (ztService != null)
            {
                ztService.Kill();
                ztService = null;
            }
        }

        // Event generated when ztService exits
        private void ztService_Exited(object sender, System.EventArgs e)
        {
            ztService = null;
        }

        // Event generated when ztKiller is done
        private void ztKiller_Exited(object sender, System.EventArgs e)
        {
            ztKiller = null;
        }

        private string ztHome;
        private string ztUpdatesFolder;
        private string ztBinary;

        private volatile Process ztService;
        private volatile Process ztKiller;
    }
}