Files
EdgeNode/internal/nodes/listener.go

130 lines
2.7 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"
"github.com/TeaOSLab/EdgeNode/internal/configs"
2020-07-22 22:18:47 +08:00
"github.com/iwind/TeaGo/logs"
"net"
"net/http"
2020-07-21 11:18:47 +08:00
"sync"
)
type Listener struct {
2020-07-22 22:18:47 +08:00
group *configs.ServerGroup
isListening bool
listener interface{} // 监听器
2020-07-21 11:18:47 +08:00
locker sync.RWMutex
}
func NewListener() *Listener {
return &Listener{}
}
func (this *Listener) Reload(group *configs.ServerGroup) {
this.locker.Lock()
defer this.locker.Unlock()
this.group = group
}
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()
switch protocol {
2020-07-22 22:18:47 +08:00
case configs.ProtocolHTTP, configs.ProtocolHTTP4, configs.ProtocolHTTP6:
2020-07-21 11:18:47 +08:00
return this.listenHTTP()
2020-07-22 22:18:47 +08:00
case configs.ProtocolHTTPS, configs.ProtocolHTTPS4, configs.ProtocolHTTPS6:
2020-07-21 11:18:47 +08:00
return this.ListenHTTPS()
2020-07-22 22:18:47 +08:00
case configs.ProtocolTCP, configs.ProtocolTCP4, configs.ProtocolTCP6:
2020-07-21 11:18:47 +08:00
return this.listenTCP()
2020-07-22 22:18:47 +08:00
case configs.ProtocolTLS, configs.ProtocolTLS4, configs.ProtocolTLS6:
2020-07-21 11:18:47 +08:00
return this.listenTLS()
case configs.ProtocolUnix:
return this.listenUnix()
case configs.ProtocolUDP:
return this.listenUDP()
default:
return errors.New("unknown protocol '" + protocol + "'")
}
}
func (this *Listener) Close() error {
// TODO 需要实现
return nil
}
func (this *Listener) listenHTTP() error {
2020-07-22 22:18:47 +08:00
listener, err := this.createListener()
if err != nil {
return err
}
mux := http.NewServeMux()
mux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
_, _ = writer.Write([]byte("Hello, World"))
})
server := &http.Server{
Addr: this.group.Addr(),
Handler: mux,
}
go func() {
err = server.Serve(listener)
if err != nil {
logs.Println("[LISTENER]" + err.Error())
}
}()
2020-07-21 11:18:47 +08:00
return nil
}
func (this *Listener) ListenHTTPS() error {
2020-07-22 22:18:47 +08:00
// TODO 需要实现
2020-07-21 11:18:47 +08:00
return nil
}
func (this *Listener) listenTCP() error {
2020-07-22 22:18:47 +08:00
// TODO 需要实现
2020-07-21 11:18:47 +08:00
return nil
}
func (this *Listener) listenTLS() error {
2020-07-22 22:18:47 +08:00
// TODO 需要实现
2020-07-21 11:18:47 +08:00
return nil
}
func (this *Listener) listenUnix() error {
2020-07-22 22:18:47 +08:00
// TODO 需要实现
2020-07-21 11:18:47 +08:00
return nil
}
func (this *Listener) listenUDP() error {
2020-07-22 22:18:47 +08:00
// TODO 需要实现
2020-07-21 11:18:47 +08:00
return nil
}
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() {
case configs.ProtocolHTTP4, configs.ProtocolHTTPS4, configs.ProtocolTLS4:
return listenConfig.Listen(context.Background(), "tcp4", this.group.Addr())
case configs.ProtocolHTTP6, configs.ProtocolHTTPS6, configs.ProtocolTLS6:
return listenConfig.Listen(context.Background(), "tcp6", this.group.Addr())
}
return listenConfig.Listen(context.Background(), "tcp", this.group.Addr())
}