mirror of
				https://github.com/TeaOSLab/EdgeNode.git
				synced 2025-11-04 07:40:56 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			121 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			121 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package nodes
 | 
						|
 | 
						|
import (
 | 
						|
	"context"
 | 
						|
	"errors"
 | 
						|
	"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
 | 
						|
	"github.com/iwind/TeaGo/logs"
 | 
						|
	"net"
 | 
						|
	"sync"
 | 
						|
)
 | 
						|
 | 
						|
type Listener struct {
 | 
						|
	group       *serverconfigs.ServerGroup
 | 
						|
	isListening bool
 | 
						|
	listener    ListenerInterface // 监听器
 | 
						|
 | 
						|
	locker sync.RWMutex
 | 
						|
}
 | 
						|
 | 
						|
func NewListener() *Listener {
 | 
						|
	return &Listener{}
 | 
						|
}
 | 
						|
 | 
						|
func (this *Listener) Reload(group *serverconfigs.ServerGroup) {
 | 
						|
	this.locker.Lock()
 | 
						|
	this.group = group
 | 
						|
	if this.listener != nil {
 | 
						|
		this.listener.Reload(group)
 | 
						|
	}
 | 
						|
	this.locker.Unlock()
 | 
						|
}
 | 
						|
 | 
						|
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()
 | 
						|
 | 
						|
	netListener, err := this.createListener()
 | 
						|
	if err != nil {
 | 
						|
		return err
 | 
						|
	}
 | 
						|
 | 
						|
	switch protocol {
 | 
						|
	case serverconfigs.ProtocolHTTP, serverconfigs.ProtocolHTTP4, serverconfigs.ProtocolHTTP6:
 | 
						|
		this.listener = &HTTPListener{
 | 
						|
			BaseListener: BaseListener{Group: this.group},
 | 
						|
			Listener:     netListener,
 | 
						|
		}
 | 
						|
	case serverconfigs.ProtocolHTTPS, serverconfigs.ProtocolHTTPS4, serverconfigs.ProtocolHTTPS6:
 | 
						|
		this.listener = &HTTPListener{
 | 
						|
			BaseListener: BaseListener{Group: this.group},
 | 
						|
			Listener:     netListener,
 | 
						|
		}
 | 
						|
	case serverconfigs.ProtocolTCP, serverconfigs.ProtocolTCP4, serverconfigs.ProtocolTCP6:
 | 
						|
		this.listener = &TCPListener{
 | 
						|
			BaseListener: BaseListener{Group: this.group},
 | 
						|
			Listener:     netListener,
 | 
						|
		}
 | 
						|
	case serverconfigs.ProtocolTLS, serverconfigs.ProtocolTLS4, serverconfigs.ProtocolTLS6:
 | 
						|
		this.listener = &TCPListener{
 | 
						|
			BaseListener: BaseListener{Group: this.group},
 | 
						|
			Listener:     netListener,
 | 
						|
		}
 | 
						|
	case serverconfigs.ProtocolUnix:
 | 
						|
		this.listener = &UnixListener{
 | 
						|
			BaseListener: BaseListener{Group: this.group},
 | 
						|
			Listener:     netListener,
 | 
						|
		}
 | 
						|
	case serverconfigs.ProtocolUDP:
 | 
						|
		this.listener = &UDPListener{
 | 
						|
			BaseListener: BaseListener{Group: this.group},
 | 
						|
			Listener:     netListener,
 | 
						|
		}
 | 
						|
	default:
 | 
						|
		return errors.New("unknown protocol '" + protocol.String() + "'")
 | 
						|
	}
 | 
						|
 | 
						|
	this.listener.Init()
 | 
						|
 | 
						|
	go func() {
 | 
						|
		err := this.listener.Serve()
 | 
						|
		if err != nil {
 | 
						|
			logs.Println("[LISTENER]" + err.Error())
 | 
						|
		}
 | 
						|
	}()
 | 
						|
 | 
						|
	return nil
 | 
						|
}
 | 
						|
 | 
						|
func (this *Listener) Close() error {
 | 
						|
	if this.listener == nil {
 | 
						|
		return nil
 | 
						|
	}
 | 
						|
	return this.listener.Close()
 | 
						|
}
 | 
						|
 | 
						|
// 创建监听器
 | 
						|
func (this *Listener) createListener() (net.Listener, error) {
 | 
						|
	listenConfig := net.ListenConfig{
 | 
						|
		Control:   nil,
 | 
						|
		KeepAlive: 0,
 | 
						|
	}
 | 
						|
 | 
						|
	switch this.group.Protocol() {
 | 
						|
	case serverconfigs.ProtocolHTTP4, serverconfigs.ProtocolHTTPS4, serverconfigs.ProtocolTLS4:
 | 
						|
		return listenConfig.Listen(context.Background(), "tcp4", this.group.Addr())
 | 
						|
	case serverconfigs.ProtocolHTTP6, serverconfigs.ProtocolHTTPS6, serverconfigs.ProtocolTLS6:
 | 
						|
		return listenConfig.Listen(context.Background(), "tcp6", this.group.Addr())
 | 
						|
	}
 | 
						|
 | 
						|
	return listenConfig.Listen(context.Background(), "tcp", this.group.Addr())
 | 
						|
}
 |