initial commit
This commit is contained in:
96
listener/pht/listener.go
Normal file
96
listener/pht/listener.go
Normal file
@ -0,0 +1,96 @@
|
||||
// plain http tunnel
|
||||
|
||||
package pht
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/go-gost/gost/v3/pkg/common/metrics"
|
||||
"github.com/go-gost/gost/v3/pkg/listener"
|
||||
"github.com/go-gost/gost/v3/pkg/logger"
|
||||
md "github.com/go-gost/gost/v3/pkg/metadata"
|
||||
"github.com/go-gost/gost/v3/pkg/registry"
|
||||
pht_util "github.com/go-gost/x/internal/util/pht"
|
||||
)
|
||||
|
||||
func init() {
|
||||
registry.ListenerRegistry().Register("pht", NewListener)
|
||||
registry.ListenerRegistry().Register("phts", NewTLSListener)
|
||||
}
|
||||
|
||||
type phtListener struct {
|
||||
addr net.Addr
|
||||
tlsEnabled bool
|
||||
server *pht_util.Server
|
||||
logger logger.Logger
|
||||
md metadata
|
||||
options listener.Options
|
||||
}
|
||||
|
||||
func NewListener(opts ...listener.Option) listener.Listener {
|
||||
options := listener.Options{}
|
||||
for _, opt := range opts {
|
||||
opt(&options)
|
||||
}
|
||||
return &phtListener{
|
||||
logger: options.Logger,
|
||||
options: options,
|
||||
}
|
||||
}
|
||||
|
||||
func NewTLSListener(opts ...listener.Option) listener.Listener {
|
||||
options := listener.Options{}
|
||||
for _, opt := range opts {
|
||||
opt(&options)
|
||||
}
|
||||
return &phtListener{
|
||||
tlsEnabled: true,
|
||||
logger: options.Logger,
|
||||
options: options,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *phtListener) Init(md md.Metadata) (err error) {
|
||||
if err = l.parseMetadata(md); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
l.addr, err = net.ResolveTCPAddr("tcp", l.options.Addr)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
l.server = pht_util.NewServer(
|
||||
l.options.Addr,
|
||||
pht_util.TLSConfigServerOption(l.options.TLSConfig),
|
||||
pht_util.EnableTLSServerOption(l.tlsEnabled),
|
||||
pht_util.BacklogServerOption(l.md.backlog),
|
||||
pht_util.PathServerOption(l.md.authorizePath, l.md.pushPath, l.md.pullPath),
|
||||
pht_util.LoggerServerOption(l.options.Logger),
|
||||
)
|
||||
|
||||
go func() {
|
||||
if err := l.server.ListenAndServe(); err != nil {
|
||||
l.logger.Error(err)
|
||||
}
|
||||
}()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (l *phtListener) Accept() (conn net.Conn, err error) {
|
||||
conn, err = l.server.Accept()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
conn = metrics.WrapConn(l.options.Service, conn)
|
||||
return
|
||||
}
|
||||
|
||||
func (l *phtListener) Addr() net.Addr {
|
||||
return l.addr
|
||||
}
|
||||
|
||||
func (l *phtListener) Close() (err error) {
|
||||
return l.server.Close()
|
||||
}
|
51
listener/pht/metadata.go
Normal file
51
listener/pht/metadata.go
Normal file
@ -0,0 +1,51 @@
|
||||
package pht
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
mdata "github.com/go-gost/gost/v3/pkg/metadata"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultAuthorizePath = "/authorize"
|
||||
defaultPushPath = "/push"
|
||||
defaultPullPath = "/pull"
|
||||
defaultBacklog = 128
|
||||
)
|
||||
|
||||
type metadata struct {
|
||||
authorizePath string
|
||||
pushPath string
|
||||
pullPath string
|
||||
backlog int
|
||||
}
|
||||
|
||||
func (l *phtListener) parseMetadata(md mdata.Metadata) (err error) {
|
||||
const (
|
||||
authorizePath = "authorizePath"
|
||||
pushPath = "pushPath"
|
||||
pullPath = "pullPath"
|
||||
|
||||
backlog = "backlog"
|
||||
)
|
||||
|
||||
l.md.authorizePath = mdata.GetString(md, authorizePath)
|
||||
if !strings.HasPrefix(l.md.authorizePath, "/") {
|
||||
l.md.authorizePath = defaultAuthorizePath
|
||||
}
|
||||
l.md.pushPath = mdata.GetString(md, pushPath)
|
||||
if !strings.HasPrefix(l.md.pushPath, "/") {
|
||||
l.md.pushPath = defaultPushPath
|
||||
}
|
||||
l.md.pullPath = mdata.GetString(md, pullPath)
|
||||
if !strings.HasPrefix(l.md.pullPath, "/") {
|
||||
l.md.pullPath = defaultPullPath
|
||||
}
|
||||
|
||||
l.md.backlog = mdata.GetInt(md, backlog)
|
||||
if l.md.backlog <= 0 {
|
||||
l.md.backlog = defaultBacklog
|
||||
}
|
||||
|
||||
return
|
||||
}
|
Reference in New Issue
Block a user