1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-16 09:26:21 -05:00
v2fly/app/proxyman/inbound/inbound.go

159 lines
3.9 KiB
Go
Raw Normal View History

package inbound
2017-12-02 19:04:57 -05:00
//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg inbound -path App,Proxyman,Inbound
2017-04-08 19:43:25 -04:00
import (
"context"
2018-02-05 17:38:24 -05:00
"sync"
"v2ray.com/core"
"v2ray.com/core/app/proxyman"
"v2ray.com/core/common"
)
2017-04-09 08:49:40 -04:00
// Manager is to manage all inbound handlers.
type Manager struct {
2018-02-07 06:34:15 -05:00
access sync.RWMutex
2018-02-05 17:38:24 -05:00
untaggedHandler []core.InboundHandler
taggedHandlers map[string]core.InboundHandler
2018-02-07 06:34:15 -05:00
running bool
}
2018-02-08 16:09:55 -05:00
// New returns a new Manager for inbound handlers.
2017-04-09 08:49:40 -04:00
func New(ctx context.Context, config *proxyman.InboundConfig) (*Manager, error) {
m := &Manager{
taggedHandlers: make(map[string]core.InboundHandler),
}
v := core.FromContext(ctx)
if v == nil {
return nil, newError("V is not in context")
}
if err := v.RegisterFeature((*core.InboundHandlerManager)(nil), m); err != nil {
return nil, newError("unable to register InboundHandlerManager").Base(err)
}
return m, nil
}
2018-02-08 16:09:55 -05:00
// AddHandler implements core.InboundHandlerManager.
func (m *Manager) AddHandler(ctx context.Context, handler core.InboundHandler) error {
2018-02-07 06:34:15 -05:00
m.access.Lock()
defer m.access.Unlock()
2018-02-05 17:38:24 -05:00
tag := handler.Tag()
if len(tag) > 0 {
m.taggedHandlers[tag] = handler
2018-02-05 17:38:24 -05:00
} else {
m.untaggedHandler = append(m.untaggedHandler, handler)
}
2018-02-07 06:34:15 -05:00
if m.running {
return handler.Start()
}
return nil
}
2018-02-08 16:09:55 -05:00
// GetHandler returns core.InboundHandlerManager.
func (m *Manager) GetHandler(ctx context.Context, tag string) (core.InboundHandler, error) {
2018-02-07 06:34:15 -05:00
m.access.RLock()
defer m.access.RUnlock()
2018-02-05 17:38:24 -05:00
handler, found := m.taggedHandlers[tag]
if !found {
2017-04-08 19:43:25 -04:00
return nil, newError("handler not found: ", tag)
}
return handler, nil
}
2018-02-05 17:38:24 -05:00
func (m *Manager) RemoveHandler(ctx context.Context, tag string) error {
if len(tag) == 0 {
return core.ErrNoClue
}
2018-02-07 06:34:15 -05:00
m.access.Lock()
defer m.access.Unlock()
2018-02-05 17:38:24 -05:00
if handler, found := m.taggedHandlers[tag]; found {
handler.Close()
delete(m.taggedHandlers, tag)
return nil
}
return core.ErrNoClue
}
2017-04-09 08:49:40 -04:00
func (m *Manager) Start() error {
2018-02-07 06:34:15 -05:00
m.access.Lock()
defer m.access.Unlock()
m.running = true
2018-02-05 17:38:24 -05:00
for _, handler := range m.taggedHandlers {
if err := handler.Start(); err != nil {
return err
}
}
for _, handler := range m.untaggedHandler {
if err := handler.Start(); err != nil {
return err
}
}
return nil
}
2018-02-08 09:39:46 -05:00
func (m *Manager) Close() error {
2018-02-07 06:34:15 -05:00
m.access.Lock()
defer m.access.Unlock()
m.running = false
2018-02-05 17:38:24 -05:00
for _, handler := range m.taggedHandlers {
handler.Close()
}
for _, handler := range m.untaggedHandler {
handler.Close()
}
2018-02-08 09:39:46 -05:00
return nil
}
func NewHandler(ctx context.Context, config *core.InboundHandlerConfig) (core.InboundHandler, error) {
rawReceiverSettings, err := config.ReceiverSettings.GetInstance()
if err != nil {
return nil, err
}
receiverSettings, ok := rawReceiverSettings.(*proxyman.ReceiverConfig)
if !ok {
return nil, newError("not a ReceiverConfig").AtError()
}
proxySettings, err := config.ProxySettings.GetInstance()
if err != nil {
return nil, err
}
tag := config.Tag
allocStrategy := receiverSettings.AllocationStrategy
if allocStrategy == nil || allocStrategy.Type == proxyman.AllocationStrategy_Always {
return NewAlwaysOnInboundHandler(ctx, tag, receiverSettings, proxySettings)
}
if allocStrategy.Type == proxyman.AllocationStrategy_Random {
return NewDynamicInboundHandler(ctx, tag, receiverSettings, proxySettings)
}
return nil, newError("unknown allocation strategy: ", receiverSettings.AllocationStrategy.Type).AtError()
}
2017-12-27 22:06:51 -05:00
func (m *Manager) asUnixReceiverConfig(ctx context.Context, config *core.InboundHandlerConfig) {
return
2017-12-27 22:06:51 -05:00
}
func init() {
common.Must(common.RegisterConfig((*proxyman.InboundConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
return New(ctx, config.(*proxyman.InboundConfig))
}))
common.Must(common.RegisterConfig((*core.InboundHandlerConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
return NewHandler(ctx, config.(*core.InboundHandlerConfig))
}))
}