阶段性提交

This commit is contained in:
刘祥超
2020-07-22 22:18:47 +08:00
parent 219732c1df
commit fa7edac94b
19 changed files with 716 additions and 14 deletions

View File

@@ -1,13 +1,20 @@
package nodes
import (
"context"
"errors"
"github.com/TeaOSLab/EdgeNode/internal/configs"
"github.com/iwind/TeaGo/logs"
"net"
"net/http"
"sync"
)
type Listener struct {
group *configs.ServerGroup
group *configs.ServerGroup
isListening bool
listener interface{} // 监听器
locker sync.RWMutex
}
@@ -34,13 +41,13 @@ func (this *Listener) Listen() error {
}
protocol := this.group.Protocol()
switch protocol {
case configs.ProtocolHTTP:
case configs.ProtocolHTTP, configs.ProtocolHTTP4, configs.ProtocolHTTP6:
return this.listenHTTP()
case configs.ProtocolHTTPS:
case configs.ProtocolHTTPS, configs.ProtocolHTTPS4, configs.ProtocolHTTPS6:
return this.ListenHTTPS()
case configs.ProtocolTCP:
case configs.ProtocolTCP, configs.ProtocolTCP4, configs.ProtocolTCP6:
return this.listenTCP()
case configs.ProtocolTLS:
case configs.ProtocolTLS, configs.ProtocolTLS4, configs.ProtocolTLS6:
return this.listenTLS()
case configs.ProtocolUnix:
return this.listenUnix()
@@ -57,25 +64,66 @@ func (this *Listener) Close() error {
}
func (this *Listener) listenHTTP() error {
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())
}
}()
return nil
}
func (this *Listener) ListenHTTPS() error {
// TODO 需要实现
return nil
}
func (this *Listener) listenTCP() error {
// TODO 需要实现
return nil
}
func (this *Listener) listenTLS() error {
// TODO 需要实现
return nil
}
func (this *Listener) listenUnix() error {
// TODO 需要实现
return nil
}
func (this *Listener) listenUDP() error {
// TODO 需要实现
return nil
}
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())
}