1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-07 10:47:48 -05:00
v2fly/app/proxyman/inbound/inbound.go

179 lines
4.4 KiB
Go
Raw Normal View History

package inbound
2018-09-30 17:08:41 -04:00
//go:generate errorgen
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"
2018-05-31 05:55:11 -04:00
"v2ray.com/core/common/serial"
"v2ray.com/core/common/session"
"v2ray.com/core/features/inbound"
)
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
untaggedHandler []inbound.Handler
taggedHandlers map[string]inbound.Handler
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]inbound.Handler),
}
return m, nil
}
2018-10-12 17:57:56 -04:00
// Type implements common.HasType.
func (*Manager) Type() interface{} {
return inbound.ManagerType()
}
// AddHandler implements inbound.Manager.
func (m *Manager) AddHandler(ctx context.Context, handler inbound.Handler) 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
}
// GetHandler implements inbound.Manager.
func (m *Manager) GetHandler(ctx context.Context, tag string) (inbound.Handler, 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
}
// RemoveHandler implements inbound.Manager.
2018-02-05 17:38:24 -05:00
func (m *Manager) RemoveHandler(ctx context.Context, tag string) error {
if tag == "" {
return common.ErrNoClue
2018-02-05 17:38:24 -05:00
}
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 {
2018-04-04 15:32:40 -04:00
if err := handler.Close(); err != nil {
newError("failed to close handler ", tag).Base(err).AtWarning().WriteToLog(session.ExportIDToError(ctx))
2018-04-04 15:32:40 -04:00
}
2018-02-05 17:38:24 -05:00
delete(m.taggedHandlers, tag)
return nil
}
return common.ErrNoClue
2018-02-05 17:38:24 -05:00
}
2018-04-03 18:57:44 -04:00
// Start implements common.Runnable.
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-04-03 18:57:44 -04:00
// Close implements common.Closable.
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-05-31 05:55:11 -04:00
var errors []interface{}
2018-02-05 17:38:24 -05:00
for _, handler := range m.taggedHandlers {
2018-05-31 05:55:11 -04:00
if err := handler.Close(); err != nil {
errors = append(errors, err)
}
2018-02-05 17:38:24 -05:00
}
for _, handler := range m.untaggedHandler {
2018-05-31 05:55:11 -04:00
if err := handler.Close(); err != nil {
errors = append(errors, err)
}
}
if len(errors) > 0 {
return newError("failed to close all handlers").Base(newError(serial.Concat(errors...)))
}
2018-02-08 09:39:46 -05:00
return nil
}
// NewHandler creates a new inbound.Handler based on the given config.
func NewHandler(ctx context.Context, config *core.InboundHandlerConfig) (inbound.Handler, error) {
rawReceiverSettings, err := config.ReceiverSettings.GetInstance()
if err != nil {
return nil, err
}
proxySettings, err := config.ProxySettings.GetInstance()
if err != nil {
return nil, err
}
tag := config.Tag
2018-04-04 05:12:09 -04:00
receiverSettings, ok := rawReceiverSettings.(*proxyman.ReceiverConfig)
if !ok {
return nil, newError("not a ReceiverConfig").AtError()
}
streamSettings := receiverSettings.StreamSettings
if streamSettings != nil && streamSettings.SocketSettings != nil {
ctx = session.ContextWithSockopt(ctx, &session.Sockopt{
Mark: streamSettings.SocketSettings.Mark,
})
}
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()
}
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))
}))
}