Add backend weight round robin select (#34)
[dns-over-https.git] / doh-client / main.go
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
111
112
113
114
115
116
117
118
119
/*
   DNS-over-HTTPS
   Copyright (C) 2017-2018 Star Brilliant <m13253@hotmail.com>

   Permission is hereby granted, free of charge, to any person obtaining a
   copy of this software and associated documentation files (the "Software"),
   to deal in the Software without restriction, including without limitation
   the rights to use, copy, modify, merge, publish, distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the
   Software is furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
   DEALINGS IN THE SOFTWARE.
*/

package main

import (
	"flag"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"os"
	"runtime"
	"strconv"
)

func checkPIDFile(pidFile string) (bool, error) {
retry:
	f, err := os.OpenFile(pidFile, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0666)
	if os.IsExist(err) {
		pidStr, err := ioutil.ReadFile(pidFile)
		if err != nil {
			return false, err
		}
		pid, err := strconv.ParseUint(string(pidStr), 10, 0)
		if err != nil {
			return false, err
		}
		_, err = os.Stat(fmt.Sprintf("/proc/%d", pid))
		if os.IsNotExist(err) {
			err = os.Remove(pidFile)
			if err != nil {
				return false, err
			}
			goto retry
		} else if err != nil {
			return false, err
		}
		log.Printf("Already running on PID %d, exiting.\n", pid)
		return false, nil
	} else if err != nil {
		return false, err
	}
	defer f.Close()
	_, err = io.WriteString(f, strconv.FormatInt(int64(os.Getpid()), 10))
	if err != nil {
		return false, err
	}
	return true, nil
}

func main() {
	confPath := flag.String("conf", "doh-client.conf", "Configuration file")
	verbose := flag.Bool("verbose", false, "Enable logging")
	showVersion := flag.Bool("version", false, "Show software version and exit")
	var pidFile *string

	// I really want to push the technology forward by recommending cgroup-based
	// process tracking. But I understand some cloud service providers have
	// their own monitoring system. So this feature is only enabled on Linux and
	// BSD series platforms which lacks functionality similar to cgroup.
	switch runtime.GOOS {
	case "dragonfly", "freebsd", "linux", "netbsd", "openbsd":
		pidFile = flag.String("pid-file", "", "PID file for legacy supervision systems lacking support for reliable cgroup-based process tracking")
	}

	flag.Parse()

	if *showVersion {
		fmt.Printf("doh-server %s\nHomepage: https://github.com/m13253/dns-over-https\n", VERSION)
		return
	}

	if pidFile != nil && *pidFile != "" {
		ok, err := checkPIDFile(*pidFile)
		if err != nil {
			log.Printf("Error checking PID file: %v\n", err)
		}
		if !ok {
			return
		}
	}

	conf, err := loadConfig(*confPath)
	if err != nil {
		log.Fatalln(err)
	}

	if *verbose {
		conf.Verbose = true
	}

	client, err := NewClient(conf)
	if err != nil {
		log.Fatalln(err)
	}
	_ = client.Start()
}