Files
EdgeNode/internal/nodes/listener.go

180 lines
4.3 KiB
Go
Raw Normal View History

2020-07-21 11:18:47 +08:00
package nodes
import (
2020-07-22 22:18:47 +08:00
"context"
2020-07-21 11:18:47 +08:00
"errors"
2020-09-13 20:37:40 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
2020-10-28 11:19:06 +08:00
"github.com/TeaOSLab/EdgeNode/internal/events"
"github.com/TeaOSLab/EdgeNode/internal/goman"
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
2020-07-22 22:18:47 +08:00
"net"
2020-07-21 11:18:47 +08:00
"sync"
)
type Listener struct {
2021-09-22 19:40:11 +08:00
group *serverconfigs.ServerAddressGroup
2020-07-22 22:18:47 +08:00
isListening bool
listener ListenerInterface // 监听器
2020-07-22 22:18:47 +08:00
2020-07-21 11:18:47 +08:00
locker sync.RWMutex
}
func NewListener() *Listener {
return &Listener{}
}
2021-09-22 19:40:11 +08:00
func (this *Listener) Reload(group *serverconfigs.ServerAddressGroup) {
2020-07-21 11:18:47 +08:00
this.locker.Lock()
this.group = group
if this.listener != nil {
this.listener.Reload(group)
}
this.locker.Unlock()
2020-07-21 11:18:47 +08:00
}
func (this *Listener) FullAddr() string {
if this.group != nil {
return this.group.FullAddr()
}
return ""
}
func (this *Listener) Listen() error {
if this.group == nil {
return nil
}
protocol := this.group.Protocol()
2021-06-07 15:45:47 +08:00
if protocol.IsUDPFamily() {
return this.listenUDP()
}
return this.listenTCP()
}
2020-07-21 11:18:47 +08:00
2021-06-07 15:45:47 +08:00
func (this *Listener) listenTCP() error {
if this.group == nil {
return nil
}
protocol := this.group.Protocol()
2021-12-18 19:17:40 +08:00
tcpListener, err := this.createTCPListener()
2020-07-22 22:18:47 +08:00
if err != nil {
return err
}
2021-12-22 16:43:16 +08:00
var netListener = NewClientListener(tcpListener, protocol.IsHTTPFamily() || protocol.IsHTTPSFamily())
2022-01-12 20:31:04 +08:00
events.OnKey(events.EventQuit, this, func() {
remotelogs.Println("LISTENER", "quit "+this.group.FullAddr())
2020-10-28 11:19:06 +08:00
_ = netListener.Close()
})
2020-07-22 22:18:47 +08:00
2020-09-09 18:53:53 +08:00
switch protocol {
case serverconfigs.ProtocolHTTP, serverconfigs.ProtocolHTTP4, serverconfigs.ProtocolHTTP6:
this.listener = &HTTPListener{
2020-10-01 16:00:52 +08:00
BaseListener: BaseListener{Group: this.group},
Listener: netListener,
2020-09-09 18:53:53 +08:00
}
case serverconfigs.ProtocolHTTPS, serverconfigs.ProtocolHTTPS4, serverconfigs.ProtocolHTTPS6:
2021-12-18 19:17:40 +08:00
netListener.SetIsTLS(true)
2020-09-09 18:53:53 +08:00
this.listener = &HTTPListener{
2020-10-01 16:00:52 +08:00
BaseListener: BaseListener{Group: this.group},
2020-10-01 16:51:24 +08:00
Listener: netListener,
2020-09-09 18:53:53 +08:00
}
case serverconfigs.ProtocolTCP, serverconfigs.ProtocolTCP4, serverconfigs.ProtocolTCP6:
this.listener = &TCPListener{
2020-10-01 16:51:24 +08:00
BaseListener: BaseListener{Group: this.group},
Listener: netListener,
2020-09-09 18:53:53 +08:00
}
case serverconfigs.ProtocolTLS, serverconfigs.ProtocolTLS4, serverconfigs.ProtocolTLS6:
2021-12-18 19:17:40 +08:00
netListener.SetIsTLS(true)
2020-09-09 18:53:53 +08:00
this.listener = &TCPListener{
2020-10-01 16:51:24 +08:00
BaseListener: BaseListener{Group: this.group},
Listener: netListener,
2020-09-09 18:53:53 +08:00
}
case serverconfigs.ProtocolUnix:
this.listener = &UnixListener{
2020-10-01 16:51:24 +08:00
BaseListener: BaseListener{Group: this.group},
Listener: netListener,
2020-09-09 18:53:53 +08:00
}
default:
2020-09-26 08:07:07 +08:00
return errors.New("unknown protocol '" + protocol.String() + "'")
2020-07-22 22:18:47 +08:00
}
2020-09-09 18:53:53 +08:00
this.listener.Init()
goman.New(func() {
2020-09-09 18:53:53 +08:00
err := this.listener.Serve()
2020-07-22 22:18:47 +08:00
if err != nil {
2020-10-28 11:19:06 +08:00
// 在这里屏蔽accept错误防止在优雅关闭的时候有多余的提示
opErr, ok := err.(*net.OpError)
if ok && opErr.Op == "accept" {
return
}
// 打印其他错误
remotelogs.Error("LISTENER", err.Error())
2020-07-22 22:18:47 +08:00
}
})
2020-07-21 11:18:47 +08:00
return nil
}
2021-06-07 15:45:47 +08:00
func (this *Listener) listenUDP() error {
listener, err := this.createUDPListener()
if err != nil {
return err
}
2022-01-12 20:31:04 +08:00
events.OnKey(events.EventQuit, this, func() {
2021-06-07 15:45:47 +08:00
remotelogs.Println("LISTENER", "quit "+this.group.FullAddr())
_ = listener.Close()
})
this.listener = &UDPListener{
BaseListener: BaseListener{Group: this.group},
Listener: listener,
}
goman.New(func() {
2021-06-07 15:45:47 +08:00
err := this.listener.Serve()
if err != nil {
remotelogs.Error("LISTENER", err.Error())
}
})
2021-06-07 15:45:47 +08:00
return nil
}
2020-09-09 18:53:53 +08:00
func (this *Listener) Close() error {
2022-01-12 20:31:04 +08:00
events.Remove(this)
2020-09-09 18:53:53 +08:00
if this.listener == nil {
return nil
}
return this.listener.Close()
2020-07-21 11:18:47 +08:00
}
2020-07-22 22:18:47 +08:00
2021-06-07 15:45:47 +08:00
// 创建TCP监听器
func (this *Listener) createTCPListener() (net.Listener, error) {
2020-07-22 22:18:47 +08:00
listenConfig := net.ListenConfig{
Control: nil,
KeepAlive: 0,
}
switch this.group.Protocol() {
2020-09-09 18:53:53 +08:00
case serverconfigs.ProtocolHTTP4, serverconfigs.ProtocolHTTPS4, serverconfigs.ProtocolTLS4:
2020-07-22 22:18:47 +08:00
return listenConfig.Listen(context.Background(), "tcp4", this.group.Addr())
2020-09-09 18:53:53 +08:00
case serverconfigs.ProtocolHTTP6, serverconfigs.ProtocolHTTPS6, serverconfigs.ProtocolTLS6:
2020-07-22 22:18:47 +08:00
return listenConfig.Listen(context.Background(), "tcp6", this.group.Addr())
}
return listenConfig.Listen(context.Background(), "tcp", this.group.Addr())
}
2021-06-07 15:45:47 +08:00
// 创建UDP监听器
func (this *Listener) createUDPListener() (*net.UDPConn, error) {
// TODO 将来支持udp4/udp6
addr, err := net.ResolveUDPAddr("udp", this.group.Addr())
if err != nil {
return nil, err
}
return net.ListenUDP("udp", addr)
}