Files
EdgeNode/internal/nodes/listener.go

121 lines
2.9 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-07-22 22:18:47 +08:00
"github.com/iwind/TeaGo/logs"
"net"
2020-07-21 11:18:47 +08:00
"sync"
)
type Listener struct {
2020-09-09 18:53:53 +08:00
group *serverconfigs.ServerGroup
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{}
}
2020-09-09 18:53:53 +08:00
func (this *Listener) Reload(group *serverconfigs.ServerGroup) {
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()
2020-09-09 18:53:53 +08:00
netListener, err := this.createListener()
2020-07-22 22:18:47 +08:00
if err != nil {
return err
}
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:
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:
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
}
case serverconfigs.ProtocolUDP:
this.listener = &UDPListener{
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()
2020-07-22 22:18:47 +08:00
go func() {
2020-09-09 18:53:53 +08:00
err := this.listener.Serve()
2020-07-22 22:18:47 +08:00
if err != nil {
logs.Println("[LISTENER]" + err.Error())
}
}()
2020-07-21 11:18:47 +08:00
return nil
}
2020-09-09 18:53:53 +08:00
func (this *Listener) Close() error {
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
2020-09-09 18:53:53 +08:00
// 创建监听器
2020-07-22 22:18:47 +08:00
func (this *Listener) createListener() (net.Listener, error) {
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())
}