mirror of
https://github.com/v2ray/v2ray-core.git
synced 2026-11-02 08:20:25 +00:00
massive refactoring for interoperability
This commit is contained in:
@@ -14,6 +14,7 @@ type AlwaysOnInboundHandler struct {
|
||||
proxy proxy.Inbound
|
||||
workers []worker
|
||||
mux *mux.Server
|
||||
tag string
|
||||
}
|
||||
|
||||
func NewAlwaysOnInboundHandler(ctx context.Context, tag string, receiverConfig *proxyman.ReceiverConfig, proxyConfig interface{}) (*AlwaysOnInboundHandler, error) {
|
||||
@@ -25,6 +26,7 @@ func NewAlwaysOnInboundHandler(ctx context.Context, tag string, receiverConfig *
|
||||
h := &AlwaysOnInboundHandler{
|
||||
proxy: p,
|
||||
mux: mux.NewServer(ctx),
|
||||
tag: tag,
|
||||
}
|
||||
|
||||
nl := p.Network()
|
||||
@@ -80,10 +82,14 @@ func (h *AlwaysOnInboundHandler) Close() {
|
||||
}
|
||||
}
|
||||
|
||||
func (h *AlwaysOnInboundHandler) GetRandomInboundProxy() (proxy.Inbound, net.Port, int) {
|
||||
func (h *AlwaysOnInboundHandler) GetRandomInboundProxy() (interface{}, net.Port, int) {
|
||||
if len(h.workers) == 0 {
|
||||
return nil, 0, 0
|
||||
}
|
||||
w := h.workers[dice.Roll(len(h.workers))]
|
||||
return w.Proxy(), w.Port(), 9999
|
||||
}
|
||||
|
||||
func (h *AlwaysOnInboundHandler) Tag() string {
|
||||
return h.tag
|
||||
}
|
||||
|
||||
@@ -163,7 +163,7 @@ func (h *DynamicInboundHandler) Close() {
|
||||
h.cancel()
|
||||
}
|
||||
|
||||
func (h *DynamicInboundHandler) GetRandomInboundProxy() (proxy.Inbound, net.Port, int) {
|
||||
func (h *DynamicInboundHandler) GetRandomInboundProxy() (interface{}, net.Port, int) {
|
||||
h.workerMutex.RLock()
|
||||
defer h.workerMutex.RUnlock()
|
||||
|
||||
@@ -174,3 +174,7 @@ func (h *DynamicInboundHandler) GetRandomInboundProxy() (proxy.Inbound, net.Port
|
||||
expire := h.receiverConfig.AllocationStrategy.GetRefreshValue() - uint32(time.Since(h.lastRefresh)/time.Minute)
|
||||
return w.Proxy(), w.Port(), int(expire)
|
||||
}
|
||||
|
||||
func (h *DynamicInboundHandler) Tag() string {
|
||||
return h.tag
|
||||
}
|
||||
|
||||
@@ -5,64 +5,41 @@ package inbound
|
||||
import (
|
||||
"context"
|
||||
|
||||
"v2ray.com/core"
|
||||
"v2ray.com/core/app/proxyman"
|
||||
"v2ray.com/core/common"
|
||||
)
|
||||
|
||||
// Manager is to manage all inbound handlers.
|
||||
type Manager struct {
|
||||
handlers []proxyman.InboundHandler
|
||||
taggedHandlers map[string]proxyman.InboundHandler
|
||||
handlers []core.InboundHandler
|
||||
taggedHandlers map[string]core.InboundHandler
|
||||
}
|
||||
|
||||
func New(ctx context.Context, config *proxyman.InboundConfig) (*Manager, error) {
|
||||
return &Manager{
|
||||
taggedHandlers: make(map[string]proxyman.InboundHandler),
|
||||
}, nil
|
||||
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
|
||||
}
|
||||
|
||||
func (m *Manager) AddHandler(ctx context.Context, config *proxyman.InboundHandlerConfig) error {
|
||||
rawReceiverSettings, err := config.ReceiverSettings.GetInstance()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
receiverSettings, ok := rawReceiverSettings.(*proxyman.ReceiverConfig)
|
||||
if !ok {
|
||||
return newError("not a ReceiverConfig").AtError()
|
||||
}
|
||||
proxySettings, err := config.ProxySettings.GetInstance()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var handler proxyman.InboundHandler
|
||||
tag := config.Tag
|
||||
allocStrategy := receiverSettings.AllocationStrategy
|
||||
if allocStrategy == nil || allocStrategy.Type == proxyman.AllocationStrategy_Always {
|
||||
h, err := NewAlwaysOnInboundHandler(ctx, tag, receiverSettings, proxySettings)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
handler = h
|
||||
} else if allocStrategy.Type == proxyman.AllocationStrategy_Random {
|
||||
h, err := NewDynamicInboundHandler(ctx, tag, receiverSettings, proxySettings)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
handler = h
|
||||
}
|
||||
|
||||
if handler == nil {
|
||||
return newError("unknown allocation strategy: ", receiverSettings.AllocationStrategy.Type).AtError()
|
||||
}
|
||||
|
||||
func (m *Manager) AddHandler(ctx context.Context, handler core.InboundHandler) error {
|
||||
m.handlers = append(m.handlers, handler)
|
||||
tag := handler.Tag()
|
||||
if len(tag) > 0 {
|
||||
m.taggedHandlers[tag] = handler
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Manager) GetHandler(ctx context.Context, tag string) (proxyman.InboundHandler, error) {
|
||||
func (m *Manager) GetHandler(ctx context.Context, tag string) (core.InboundHandler, error) {
|
||||
handler, found := m.taggedHandlers[tag]
|
||||
if !found {
|
||||
return nil, newError("handler not found: ", tag)
|
||||
@@ -85,12 +62,36 @@ func (m *Manager) Close() {
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Manager) Interface() interface{} {
|
||||
return (*proxyman.InboundHandlerManager)(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()
|
||||
}
|
||||
|
||||
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))
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"v2ray.com/core/app/dispatcher"
|
||||
"v2ray.com/core"
|
||||
"v2ray.com/core/app/proxyman"
|
||||
"v2ray.com/core/common/buf"
|
||||
"v2ray.com/core/common/net"
|
||||
@@ -31,7 +31,7 @@ type tcpWorker struct {
|
||||
stream *internet.StreamConfig
|
||||
recvOrigDest bool
|
||||
tag string
|
||||
dispatcher dispatcher.Interface
|
||||
dispatcher core.Dispatcher
|
||||
sniffers []proxyman.KnownProtocols
|
||||
|
||||
ctx context.Context
|
||||
@@ -185,7 +185,7 @@ type udpWorker struct {
|
||||
port net.Port
|
||||
recvOrigDest bool
|
||||
tag string
|
||||
dispatcher dispatcher.Interface
|
||||
dispatcher core.Dispatcher
|
||||
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
|
||||
Reference in New Issue
Block a user