2019-03-14 08:30:04 +00:00
|
|
|
package tcp
|
|
|
|
|
|
|
|
import (
|
2023-02-03 14:24:05 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/safe"
|
2019-03-14 08:30:04 +00:00
|
|
|
)
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// HandlerSwitcher is a TCP handler switcher.
|
2019-03-14 08:30:04 +00:00
|
|
|
type HandlerSwitcher struct {
|
|
|
|
router safe.Safe
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// ServeTCP forwards the TCP connection to the current active handler.
|
2019-09-13 15:46:04 +00:00
|
|
|
func (s *HandlerSwitcher) ServeTCP(conn WriteCloser) {
|
2019-03-14 08:30:04 +00:00
|
|
|
handler := s.router.Get()
|
|
|
|
h, ok := handler.(Handler)
|
|
|
|
if ok {
|
|
|
|
h.ServeTCP(conn)
|
|
|
|
} else {
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// Switch sets the new TCP handler to use for new connections.
|
2019-03-14 08:30:04 +00:00
|
|
|
func (s *HandlerSwitcher) Switch(handler Handler) {
|
|
|
|
s.router.Set(handler)
|
|
|
|
}
|