Refine runtime.GOOS check, use switch case to replace a long if
Refine runtime.GOOS check, use switch case to replace a long if

/* /*
DNS-over-HTTPS DNS-over-HTTPS
Copyright (C) 2017-2018 Star Brilliant <m13253@hotmail.com> Copyright (C) 2017-2018 Star Brilliant <m13253@hotmail.com>
   
Permission is hereby granted, free of charge, to any person obtaining a Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"), copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the 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: Software is furnished to do so, subject to the following conditions:
   
The above copyright notice and this permission notice shall be included in The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software. all copies or substantial portions of the Software.
   
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 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 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE. DEALINGS IN THE SOFTWARE.
*/ */
   
package main package main
   
import ( import (
"flag" "flag"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
"runtime" "runtime"
"strconv" "strconv"
) )
   
func checkPIDFile(pidFile string) (bool, error) { func checkPIDFile(pidFile string) (bool, error) {
retry: retry:
f, err := os.OpenFile(pidFile, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0666) f, err := os.OpenFile(pidFile, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0666)
if os.IsExist(err) { if os.IsExist(err) {
pidStr, err := ioutil.ReadFile(pidFile) pidStr, err := ioutil.ReadFile(pidFile)
if err != nil { if err != nil {
return false, err return false, err
} }
pid, err := strconv.ParseUint(string(pidStr), 10, 0) pid, err := strconv.ParseUint(string(pidStr), 10, 0)
if err != nil { if err != nil {
return false, err return false, err
} }
_, err = os.Stat(fmt.Sprintf("/proc/%d", pid)) _, err = os.Stat(fmt.Sprintf("/proc/%d", pid))
if os.IsNotExist(err) { if os.IsNotExist(err) {
err = os.Remove(pidFile) err = os.Remove(pidFile)
if err != nil { if err != nil {
return false, err return false, err
} }
goto retry goto retry
} else if err != nil { } else if err != nil {
return false, err return false, err
} }
log.Printf("Already running on PID %d, exiting.\n", pid) log.Printf("Already running on PID %d, exiting.\n", pid)
return false, nil return false, nil
} else if err != nil { } else if err != nil {
return false, err return false, err
} }
defer f.Close() defer f.Close()
_, err = io.WriteString(f, strconv.FormatInt(int64(os.Getpid()), 10)) _, err = io.WriteString(f, strconv.FormatInt(int64(os.Getpid()), 10))
if err != nil { if err != nil {
return false, err return false, err
} }
return true, nil return true, nil
} }
   
func main() { func main() {
confPath := flag.String("conf", "doh-client.conf", "Configuration file") confPath := flag.String("conf", "doh-client.conf", "Configuration file")
verbose := flag.Bool("verbose", false, "Enable logging") verbose := flag.Bool("verbose", false, "Enable logging")
showVersion := flag.Bool("version", false, "Show software version and exit") showVersion := flag.Bool("version", false, "Show software version and exit")
var pidFile *string var pidFile *string
   
// I really want to push the technology forward by recommending cgroup-based // I really want to push the technology forward by recommending cgroup-based
// process tracking. But I understand some cloud service providers have // process tracking. But I understand some cloud service providers have
// their own monitoring system. So this feature is only enabled on Linux and // their own monitoring system. So this feature is only enabled on Linux and
// BSD series platforms which lacks functionality similar to cgroup. // BSD series platforms which lacks functionality similar to cgroup.
if runtime.GOOS == "dragonfly" || runtime.GOOS == "freebsd" || runtime.GOOS == "linux" || runtime.GOOS == "netbsd" || runtime.GOOS == "openbsd" { 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") pidFile = flag.String("pid-file", "", "PID file for legacy supervision systems lacking support for reliable cgroup-based process tracking")
} }
   
flag.Parse() flag.Parse()
   
if *showVersion { if *showVersion {
fmt.Printf("doh-server %s\nHomepage: https://github.com/m13253/dns-over-https\n", VERSION) fmt.Printf("doh-server %s\nHomepage: https://github.com/m13253/dns-over-https\n", VERSION)
return return
} }
   
if pidFile != nil && *pidFile != "" { if pidFile != nil && *pidFile != "" {
ok, err := checkPIDFile(*pidFile) ok, err := checkPIDFile(*pidFile)
if err != nil { if err != nil {
log.Printf("Error checking PID file: %v\n", err) log.Printf("Error checking PID file: %v\n", err)
} }
if !ok { if !ok {
return return
} }
} }
   
conf, err := loadConfig(*confPath) conf, err := loadConfig(*confPath)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
   
if *verbose { if *verbose {
conf.Verbose = true conf.Verbose = true
} }
   
client, err := NewClient(conf) client, err := NewClient(conf)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
_ = client.Start() _ = client.Start()
} }
   
/* /*
DNS-over-HTTPS DNS-over-HTTPS
Copyright (C) 2017-2018 Star Brilliant <m13253@hotmail.com> Copyright (C) 2017-2018 Star Brilliant <m13253@hotmail.com>
   
Permission is hereby granted, free of charge, to any person obtaining a Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"), copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the 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: Software is furnished to do so, subject to the following conditions:
   
The above copyright notice and this permission notice shall be included in The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software. all copies or substantial portions of the Software.
   
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 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 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE. DEALINGS IN THE SOFTWARE.
*/ */
   
package main package main
   
import ( import (
"flag" "flag"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
"runtime" "runtime"
"strconv" "strconv"
) )
   
func checkPIDFile(pidFile string) (bool, error) { func checkPIDFile(pidFile string) (bool, error) {
retry: retry:
f, err := os.OpenFile(pidFile, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0666) f, err := os.OpenFile(pidFile, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0666)
if os.IsExist(err) { if os.IsExist(err) {
pidStr, err := ioutil.ReadFile(pidFile) pidStr, err := ioutil.ReadFile(pidFile)
if err != nil { if err != nil {
return false, err return false, err
} }
pid, err := strconv.ParseUint(string(pidStr), 10, 0) pid, err := strconv.ParseUint(string(pidStr), 10, 0)
if err != nil { if err != nil {
return false, err return false, err
} }
_, err = os.Stat(fmt.Sprintf("/proc/%d", pid)) _, err = os.Stat(fmt.Sprintf("/proc/%d", pid))
if os.IsNotExist(err) { if os.IsNotExist(err) {
err = os.Remove(pidFile) err = os.Remove(pidFile)
if err != nil { if err != nil {
return false, err return false, err
} }
goto retry goto retry
} else if err != nil { } else if err != nil {
return false, err return false, err
} }
log.Printf("Already running on PID %d, exiting.\n", pid) log.Printf("Already running on PID %d, exiting.\n", pid)
return false, nil return false, nil
} else if err != nil { } else if err != nil {
return false, err return false, err
} }
defer f.Close() defer f.Close()
_, err = io.WriteString(f, strconv.FormatInt(int64(os.Getpid()), 10)) _, err = io.WriteString(f, strconv.FormatInt(int64(os.Getpid()), 10))
if err != nil { if err != nil {
return false, err return false, err
} }
return true, nil return true, nil
} }
   
func main() { func main() {
confPath := flag.String("conf", "doh-server.conf", "Configuration file") confPath := flag.String("conf", "doh-server.conf", "Configuration file")
verbose := flag.Bool("verbose", false, "Enable logging") verbose := flag.Bool("verbose", false, "Enable logging")
showVersion := flag.Bool("version", false, "Show software version and exit") showVersion := flag.Bool("version", false, "Show software version and exit")
var pidFile *string var pidFile *string
   
// I really want to push the technology forward by recommending cgroup-based // I really want to push the technology forward by recommending cgroup-based
// process tracking. But I understand some cloud service providers have // process tracking. But I understand some cloud service providers have
// their own monitoring system. So this feature is only enabled on Linux and // their own monitoring system. So this feature is only enabled on Linux and
// BSD series platforms which lacks functionality similar to cgroup. // BSD series platforms which lacks functionality similar to cgroup.
if runtime.GOOS == "dragonfly" || runtime.GOOS == "freebsd" || runtime.GOOS == "linux" || runtime.GOOS == "netbsd" || runtime.GOOS == "openbsd" { 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") pidFile = flag.String("pid-file", "", "PID file for legacy supervision systems lacking support for reliable cgroup-based process tracking")
} }
   
flag.Parse() flag.Parse()
   
if *showVersion { if *showVersion {
fmt.Printf("doh-server %s\nHomepage: https://github.com/m13253/dns-over-https\n", VERSION) fmt.Printf("doh-server %s\nHomepage: https://github.com/m13253/dns-over-https\n", VERSION)
return return
} }
   
if pidFile != nil && *pidFile != "" { if pidFile != nil && *pidFile != "" {
ok, err := checkPIDFile(*pidFile) ok, err := checkPIDFile(*pidFile)
if err != nil { if err != nil {
log.Printf("Error checking PID file: %v\n", err) log.Printf("Error checking PID file: %v\n", err)
} }
if !ok { if !ok {
return return
} }
} }
   
conf, err := loadConfig(*confPath) conf, err := loadConfig(*confPath)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
   
if *verbose { if *verbose {
conf.Verbose = true conf.Verbose = true
} }
   
server := NewServer(conf) server := NewServer(conf)
_ = server.Start() _ = server.Start()
} }