enhanced to http server to distinguish between ipv4 and ipv6 address when the ip address part is explicitly specified before colon port (e.g. 0.0.0.0:9999, [::]:9999, :9999)

This commit is contained in:
hyung-hwan 2024-12-07 23:03:23 +09:00
parent 8821506fb1
commit c81e05b4a3
4 changed files with 44 additions and 9 deletions

View File

@ -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)

View File

@ -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"

14
hodu.go
View File

@ -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"
}

View File

@ -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)