mirror of
https://github.com/m13253/dns-over-https.git
synced 2026-03-30 16:25:39 +00:00
Add server support for multiple listen addresses
This commit is contained in:
@@ -30,7 +30,7 @@ import (
|
||||
)
|
||||
|
||||
type config struct {
|
||||
Listen string `toml:"listen"`
|
||||
Listen []string `toml:"listen"`
|
||||
Cert string `toml:"cert"`
|
||||
Key string `toml:"key"`
|
||||
Path string `toml:"path"`
|
||||
@@ -51,9 +51,10 @@ func loadConfig(path string) (*config, error) {
|
||||
return nil, &configError{fmt.Sprintf("unknown option %q", key.String())}
|
||||
}
|
||||
|
||||
if conf.Listen == "" {
|
||||
conf.Listen = "127.0.0.1:8053"
|
||||
if len(conf.Listen) == 0 {
|
||||
conf.Listen = []string{"127.0.0.1:8053"}
|
||||
}
|
||||
|
||||
if conf.Path == "" {
|
||||
conf.Path = "/dns-query"
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
# HTTP listen port
|
||||
listen = "127.0.0.1:8053"
|
||||
listen = [
|
||||
"127.0.0.1:8053"
|
||||
]
|
||||
|
||||
# TLS certification file
|
||||
cert = ""
|
||||
|
||||
@@ -75,10 +75,27 @@ func (s *Server) Start() error {
|
||||
if s.conf.Verbose {
|
||||
servemux = handlers.CombinedLoggingHandler(os.Stdout, servemux)
|
||||
}
|
||||
if s.conf.Cert != "" || s.conf.Key != "" {
|
||||
return http.ListenAndServeTLS(s.conf.Listen, s.conf.Cert, s.conf.Key, servemux)
|
||||
listeners := make(chan error, len(s.conf.Listen))
|
||||
for _, addr := range s.conf.Listen {
|
||||
if s.conf.Cert != "" || s.conf.Key != "" {
|
||||
go func() {
|
||||
listeners <- http.ListenAndServeTLS(addr, s.conf.Cert, s.conf.Key, servemux)
|
||||
}()
|
||||
continue
|
||||
}
|
||||
go func() {
|
||||
listeners <- http.ListenAndServe(addr, servemux)
|
||||
}()
|
||||
}
|
||||
return http.ListenAndServe(s.conf.Listen, servemux)
|
||||
// wait for all handlers
|
||||
for i := 0; i < cap(listeners); i++ {
|
||||
err := <-listeners
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
close(listeners)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) handlerFunc(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
Reference in New Issue
Block a user