2017-01-26 14:46:44 -05:00
|
|
|
package inbound
|
|
|
|
|
2021-02-16 15:31:50 -05:00
|
|
|
//go:generate go run github.com/v2fly/v2ray-core/v4/common/errors/errorgen
|
2017-04-08 19:43:25 -04:00
|
|
|
|
2017-01-26 14:46:44 -05:00
|
|
|
import (
|
|
|
|
"context"
|
2018-02-05 17:38:24 -05:00
|
|
|
"sync"
|
2017-01-26 14:46:44 -05:00
|
|
|
|
2021-02-16 15:31:50 -05:00
|
|
|
core "github.com/v2fly/v2ray-core/v4"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/app/proxyman"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/serial"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/session"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/features/inbound"
|
2017-01-26 14:46:44 -05:00
|
|
|
)
|
|
|
|
|
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-10-11 15:14:53 -04:00
|
|
|
untaggedHandler []inbound.Handler
|
|
|
|
taggedHandlers map[string]inbound.Handler
|
2018-02-07 06:34:15 -05:00
|
|
|
running bool
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
|
|
|
|
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) {
|
2018-01-10 06:22:37 -05:00
|
|
|
m := &Manager{
|
2018-10-11 15:14:53 -04:00
|
|
|
taggedHandlers: make(map[string]inbound.Handler),
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
2018-01-10 06:22:37 -05:00
|
|
|
return m, nil
|
|
|
|
}
|
2017-01-26 14:46:44 -05:00
|
|
|
|
2018-10-12 17:57:56 -04:00
|
|
|
// Type implements common.HasType.
|
|
|
|
func (*Manager) Type() interface{} {
|
|
|
|
return inbound.ManagerType()
|
|
|
|
}
|
|
|
|
|
2018-10-11 15:14:53 -04:00
|
|
|
// 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
|
|
|
|
2018-01-10 06:22:37 -05:00
|
|
|
tag := handler.Tag()
|
2017-01-26 14:46:44 -05:00
|
|
|
if len(tag) > 0 {
|
|
|
|
m.taggedHandlers[tag] = handler
|
2018-02-05 17:38:24 -05:00
|
|
|
} else {
|
|
|
|
m.untaggedHandler = append(m.untaggedHandler, handler)
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
2018-02-07 06:34:15 -05:00
|
|
|
|
|
|
|
if m.running {
|
|
|
|
return handler.Start()
|
|
|
|
}
|
|
|
|
|
2017-01-26 14:46:44 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-11 15:14:53 -04:00
|
|
|
// 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
|
|
|
|
2017-01-26 14:46:44 -05:00
|
|
|
handler, found := m.taggedHandlers[tag]
|
|
|
|
if !found {
|
2017-04-08 19:43:25 -04:00
|
|
|
return nil, newError("handler not found: ", tag)
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
|
|
|
return handler, nil
|
|
|
|
}
|
|
|
|
|
2018-10-11 15:14:53 -04:00
|
|
|
// RemoveHandler implements inbound.Manager.
|
2018-02-05 17:38:24 -05:00
|
|
|
func (m *Manager) RemoveHandler(ctx context.Context, tag string) error {
|
2019-06-14 09:47:28 -04:00
|
|
|
if tag == "" {
|
2018-10-11 14:43:37 -04:00
|
|
|
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 {
|
2018-06-24 19:09:02 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-10-11 14:43:37 -04:00
|
|
|
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 {
|
2017-01-26 14:46:44 -05:00
|
|
|
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...)))
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
2018-02-08 09:39:46 -05:00
|
|
|
|
|
|
|
return nil
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
|
|
|
|
2018-10-11 15:14:53 -04:00
|
|
|
// NewHandler creates a new inbound.Handler based on the given config.
|
|
|
|
func NewHandler(ctx context.Context, config *core.InboundHandlerConfig) (inbound.Handler, error) {
|
2018-01-10 06:22:37 -05:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2020-07-31 14:04:06 -04:00
|
|
|
streamSettings := receiverSettings.StreamSettings
|
|
|
|
if streamSettings != nil && streamSettings.SocketSettings != nil {
|
|
|
|
ctx = session.ContextWithSockopt(ctx, &session.Sockopt{
|
|
|
|
Mark: streamSettings.SocketSettings.Mark,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-01-10 06:22:37 -05:00
|
|
|
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-01-26 14:46:44 -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))
|
|
|
|
}))
|
2018-01-10 06:22:37 -05:00
|
|
|
common.Must(common.RegisterConfig((*core.InboundHandlerConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
|
|
|
return NewHandler(ctx, config.(*core.InboundHandlerConfig))
|
|
|
|
}))
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|