diff --git a/client.go b/client.go index fd99aff..74804e6 100644 --- a/client.go +++ b/client.go @@ -1213,11 +1213,22 @@ func (c *Client) RunCtlTask(wg *sync.WaitGroup) { for idx, ctl = range c.ctl { l_wg.Add(1) go func(i int, cs *http.Server) { + var l net.Listener + c.log.Write ("", LOG_INFO, "Control channel[%d] started on %s", i, c.ctl_addr[i]) - if c.ctltlscfg == nil { - err = cs.ListenAndServe() - } else { - err = cs.ListenAndServeTLS("", "") // c.tlscfg must provide a certificate and a key + + // defeat hard-coded "tcp" in ListenAndServe() and ListenAndServeTLS() + // by creating the listener explicitly. + // err = cs.ListenAndServe() + // err = cs.ListenAndServeTLS("", "") // c.tlscfg must provide a certificate and a key + l, err = net.Listen(tcp_addr_str_class(cs.Addr), cs.Addr) + if err == nil { + if c.ctltlscfg == nil { + err = cs.Serve(l) + } else { + err = cs.ServeTLS(l, "", "") // c.ctltlscfg must provide a certificate and a key + } + l.Close() } if errors.Is(err, http.ErrServerClosed) { c.log.Write("", LOG_DEBUG, "Control channel[%d] ended", i) diff --git a/cmd/main.go b/cmd/main.go index 41ff135..68bcebf 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -17,7 +17,7 @@ import "syscall" import "time" -// Don't change these items to 'const' as they can be overridden with a linker option +// Don't change these items to 'const' as they can be overridden externally with a linker option var HODU_NAME string = "hodu" var HODU_VERSION string = "0.0.0" diff --git a/hodu.go b/hodu.go index bb4ef60..aa7faf5 100644 --- a/hodu.go +++ b/hodu.go @@ -25,3 +25,17 @@ type Service interface { WriteLog(id string, level LogLevel, fmtstr string, args ...interface{}) } +func tcp_addr_str_class(addr string) string { + if len(addr) > 0 { + switch addr[0] { + case '[': + return "tcp6" + case ':': + return "tcp" + default: + return "tcp4" + } + } + + return "tcp" +} diff --git a/server.go b/server.go index 63b8ee4..823ca7e 100644 --- a/server.go +++ b/server.go @@ -978,11 +978,21 @@ func (s *Server) RunCtlTask(wg *sync.WaitGroup) { for idx, ctl = range s.ctl { l_wg.Add(1) go func(i int, cs *http.Server) { + var l net.Listener + s.log.Write ("", LOG_INFO, "Control channel[%d] started on %s", i, s.ctl_addr[i]) - if s.ctltlscfg == nil { - err = cs.ListenAndServe() - } else { - err = cs.ListenAndServeTLS("", "") // c.ctltlscfg must provide a certificate and a key + + // defeat hard-coded "tcp" in ListenAndServe() and ListenAndServeTLS() + // err = cs.ListenAndServe() + // err = cs.ListenAndServeTLS("", "") + l, err = net.Listen(tcp_addr_str_class(cs.Addr), cs.Addr) + if err == nil { + if s.ctltlscfg == nil { + err = cs.Serve(l) + } else { + err = cs.ServeTLS(l, "", "") // s.ctltlscfg must provide a certificate and a key + } + l.Close() } if errors.Is(err, http.ErrServerClosed) { s.log.Write("", LOG_DEBUG, "Control channel[%d] ended", i)