mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-02 23:47:07 -05:00
code ready for selective routing
This commit is contained in:
parent
f4ec19625e
commit
12e72509a5
@ -1,6 +1,7 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
routerconfig "github.com/v2ray/v2ray-core/app/router/config"
|
||||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -30,7 +31,9 @@ type OutboundDetourConfig interface {
|
|||||||
type PointConfig interface {
|
type PointConfig interface {
|
||||||
Port() uint16
|
Port() uint16
|
||||||
LogConfig() LogConfig
|
LogConfig() LogConfig
|
||||||
|
RouterConfig() routerconfig.RouterConfig
|
||||||
InboundConfig() ConnectionConfig
|
InboundConfig() ConnectionConfig
|
||||||
OutboundConfig() ConnectionConfig
|
OutboundConfig() ConnectionConfig
|
||||||
InboundDetours() []InboundDetourConfig
|
InboundDetours() []InboundDetourConfig
|
||||||
|
OutboundDetours() []OutboundDetourConfig
|
||||||
}
|
}
|
||||||
|
@ -6,18 +6,21 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/v2ray/v2ray-core/app/point/config"
|
"github.com/v2ray/v2ray-core/app/point/config"
|
||||||
|
routerconfig "github.com/v2ray/v2ray-core/app/router/config"
|
||||||
|
routerconfigjson "github.com/v2ray/v2ray-core/app/router/config/json"
|
||||||
"github.com/v2ray/v2ray-core/common/log"
|
"github.com/v2ray/v2ray-core/common/log"
|
||||||
proxyconfig "github.com/v2ray/v2ray-core/proxy/common/config"
|
proxyconfig "github.com/v2ray/v2ray-core/proxy/common/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config is the config for Point server.
|
// Config is the config for Point server.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
PortValue uint16 `json:"port"` // Port of this Point server.
|
PortValue uint16 `json:"port"` // Port of this Point server.
|
||||||
LogConfigValue *LogConfig `json:"log"`
|
LogConfigValue *LogConfig `json:"log"`
|
||||||
InboundConfigValue *ConnectionConfig `json:"inbound"`
|
RouterConfigValue *routerconfigjson.RouterConfig `json:"router"`
|
||||||
OutboundConfigValue *ConnectionConfig `json:"outbound"`
|
InboundConfigValue *ConnectionConfig `json:"inbound"`
|
||||||
InboundDetoursValue []*InboundDetourConfig `json:"inboundDetour"`
|
OutboundConfigValue *ConnectionConfig `json:"outbound"`
|
||||||
OutboundDetoursValue []*OutboundDetourConfig `json:"outboundDetour"`
|
InboundDetoursValue []*InboundDetourConfig `json:"inboundDetour"`
|
||||||
|
OutboundDetoursValue []*OutboundDetourConfig `json:"outboundDetour"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (config *Config) Port() uint16 {
|
func (config *Config) Port() uint16 {
|
||||||
@ -31,6 +34,13 @@ func (config *Config) LogConfig() config.LogConfig {
|
|||||||
return config.LogConfigValue
|
return config.LogConfigValue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *Config) RouterConfig() routerconfig.RouterConfig {
|
||||||
|
if this.RouterConfigValue == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return this.RouterConfigValue
|
||||||
|
}
|
||||||
|
|
||||||
func (config *Config) InboundConfig() config.ConnectionConfig {
|
func (config *Config) InboundConfig() config.ConnectionConfig {
|
||||||
if config.InboundConfigValue == nil {
|
if config.InboundConfigValue == nil {
|
||||||
return nil
|
return nil
|
||||||
|
@ -2,6 +2,7 @@ package mocks
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/v2ray/v2ray-core/app/point/config"
|
"github.com/v2ray/v2ray-core/app/point/config"
|
||||||
|
routerconfig "github.com/v2ray/v2ray-core/app/router/config"
|
||||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -44,16 +45,27 @@ func (this *InboundDetourConfig) PortRange() v2net.PortRange {
|
|||||||
return this.PortRangeValue
|
return this.PortRangeValue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type OutboundDetourConfig struct {
|
||||||
|
ConnectionConfig
|
||||||
|
TagValue config.DetourTag
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *OutboundDetourConfig) Tag() config.DetourTag {
|
||||||
|
return this.TagValue
|
||||||
|
}
|
||||||
|
|
||||||
func (config *LogConfig) AccessLog() string {
|
func (config *LogConfig) AccessLog() string {
|
||||||
return config.AccessLogValue
|
return config.AccessLogValue
|
||||||
}
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
PortValue uint16
|
PortValue uint16
|
||||||
LogConfigValue *LogConfig
|
LogConfigValue *LogConfig
|
||||||
InboundConfigValue *ConnectionConfig
|
RouterConfigValue routerconfig.RouterConfig
|
||||||
OutboundConfigValue *ConnectionConfig
|
InboundConfigValue *ConnectionConfig
|
||||||
InboundDetoursValue []*InboundDetourConfig
|
OutboundConfigValue *ConnectionConfig
|
||||||
|
InboundDetoursValue []*InboundDetourConfig
|
||||||
|
OutboundDetoursValue []*OutboundDetourConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func (config *Config) Port() uint16 {
|
func (config *Config) Port() uint16 {
|
||||||
@ -64,6 +76,10 @@ func (config *Config) LogConfig() config.LogConfig {
|
|||||||
return config.LogConfigValue
|
return config.LogConfigValue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *Config) RouterConfig() routerconfig.RouterConfig {
|
||||||
|
return this.RouterConfigValue
|
||||||
|
}
|
||||||
|
|
||||||
func (config *Config) InboundConfig() config.ConnectionConfig {
|
func (config *Config) InboundConfig() config.ConnectionConfig {
|
||||||
return config.InboundConfigValue
|
return config.InboundConfigValue
|
||||||
}
|
}
|
||||||
@ -79,3 +95,11 @@ func (this *Config) InboundDetours() []config.InboundDetourConfig {
|
|||||||
}
|
}
|
||||||
return detours
|
return detours
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *Config) OutboundDetours() []config.OutboundDetourConfig {
|
||||||
|
detours := make([]config.OutboundDetourConfig, len(this.OutboundDetoursValue))
|
||||||
|
for idx, detour := range this.OutboundDetoursValue {
|
||||||
|
detours[idx] = detour
|
||||||
|
}
|
||||||
|
return detours
|
||||||
|
}
|
||||||
|
@ -2,6 +2,7 @@ package point
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/v2ray/v2ray-core/app/point/config"
|
"github.com/v2ray/v2ray-core/app/point/config"
|
||||||
|
"github.com/v2ray/v2ray-core/app/router"
|
||||||
"github.com/v2ray/v2ray-core/common/log"
|
"github.com/v2ray/v2ray-core/common/log"
|
||||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||||
"github.com/v2ray/v2ray-core/common/retry"
|
"github.com/v2ray/v2ray-core/common/retry"
|
||||||
@ -11,10 +12,12 @@ import (
|
|||||||
|
|
||||||
// Point is an single server in V2Ray system.
|
// Point is an single server in V2Ray system.
|
||||||
type Point struct {
|
type Point struct {
|
||||||
port uint16
|
port uint16
|
||||||
ich connhandler.InboundConnectionHandler
|
ich connhandler.InboundConnectionHandler
|
||||||
och connhandler.OutboundConnectionHandler
|
och connhandler.OutboundConnectionHandler
|
||||||
idh []*InboundDetourHandler
|
idh []*InboundDetourHandler
|
||||||
|
odh map[config.DetourTag]connhandler.OutboundConnectionHandler
|
||||||
|
router router.Router
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewPoint returns a new Point server based on given configuration.
|
// NewPoint returns a new Point server based on given configuration.
|
||||||
@ -65,6 +68,34 @@ func NewPoint(pConfig config.PointConfig) (*Point, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
outboundDetours := pConfig.OutboundDetours()
|
||||||
|
if len(outboundDetours) > 0 {
|
||||||
|
vpoint.odh = make(map[config.DetourTag]connhandler.OutboundConnectionHandler)
|
||||||
|
for _, detourConfig := range outboundDetours {
|
||||||
|
detourFactory := connhandler.GetOutboundConnectionHandlerFactory(detourConfig.Protocol())
|
||||||
|
if detourFactory == nil {
|
||||||
|
log.Error("Unknown detour outbound connection handler factory %s", detourConfig.Protocol())
|
||||||
|
return nil, config.BadConfiguration
|
||||||
|
}
|
||||||
|
detourHandler, err := detourFactory.Create(detourConfig.Settings())
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Failed to create detour outbound connection handler: %v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
vpoint.odh[detourConfig.Tag()] = detourHandler
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
routerConfig := pConfig.RouterConfig()
|
||||||
|
if routerConfig != nil {
|
||||||
|
r, err := router.CreateRouter(routerConfig.Strategy(), routerConfig.Settings())
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Failed to create router: %v", err)
|
||||||
|
return nil, config.BadConfiguration
|
||||||
|
}
|
||||||
|
vpoint.router = r
|
||||||
|
}
|
||||||
|
|
||||||
return vpoint, nil
|
return vpoint, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,6 +131,19 @@ func (vp *Point) Start() error {
|
|||||||
|
|
||||||
func (p *Point) DispatchToOutbound(packet v2net.Packet) ray.InboundRay {
|
func (p *Point) DispatchToOutbound(packet v2net.Packet) ray.InboundRay {
|
||||||
direct := ray.NewRay()
|
direct := ray.NewRay()
|
||||||
|
dest := packet.Destination()
|
||||||
|
|
||||||
|
if p.router != nil {
|
||||||
|
tag, err := p.router.TakeDetour(dest)
|
||||||
|
if err == nil {
|
||||||
|
handler, found := p.odh[tag]
|
||||||
|
if found {
|
||||||
|
go handler.Dispatch(packet, direct)
|
||||||
|
return direct
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
go p.och.Dispatch(packet, direct)
|
go p.och.Dispatch(packet, direct)
|
||||||
return direct
|
return direct
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Router interface {
|
type Router interface {
|
||||||
TakeDetour(v2net.Destination) (*config.DetourTag, error)
|
TakeDetour(v2net.Destination) (config.DetourTag, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type RouterFactory interface {
|
type RouterFactory interface {
|
||||||
|
@ -10,9 +10,8 @@ type Rule struct {
|
|||||||
OutboundTag string `json:"outboundTag"`
|
OutboundTag string `json:"outboundTag"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Rule) Tag() *config.DetourTag {
|
func (this *Rule) Tag() config.DetourTag {
|
||||||
detourTag := config.DetourTag(this.OutboundTag)
|
return config.DetourTag(this.OutboundTag)
|
||||||
return &detourTag
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Rule) Apply(dest v2net.Destination) bool {
|
func (this *Rule) Apply(dest v2net.Destination) bool {
|
||||||
|
@ -6,6 +6,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Rule interface {
|
type Rule interface {
|
||||||
Tag() *config.DetourTag
|
Tag() config.DetourTag
|
||||||
Apply(dest v2net.Destination) bool
|
Apply(dest v2net.Destination) bool
|
||||||
}
|
}
|
||||||
|
@ -11,21 +11,23 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
InvalidRule = errors.New("Invalid Rule")
|
InvalidRule = errors.New("Invalid Rule")
|
||||||
EmptyTag = pointconfig.DetourTag("")
|
NoRuleApplicable = errors.New("No rule applicable")
|
||||||
|
|
||||||
|
EmptyTag = pointconfig.DetourTag("")
|
||||||
)
|
)
|
||||||
|
|
||||||
type Router struct {
|
type Router struct {
|
||||||
rules []config.Rule
|
rules []config.Rule
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Router) TakeDetour(dest v2net.Destination) (*pointconfig.DetourTag, error) {
|
func (this *Router) TakeDetour(dest v2net.Destination) (pointconfig.DetourTag, error) {
|
||||||
for _, rule := range this.rules {
|
for _, rule := range this.rules {
|
||||||
if rule.Apply(dest) {
|
if rule.Apply(dest) {
|
||||||
return rule.Tag(), nil
|
return rule.Tag(), nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return &EmptyTag, nil
|
return EmptyTag, NoRuleApplicable
|
||||||
}
|
}
|
||||||
|
|
||||||
type RouterFactory struct {
|
type RouterFactory struct {
|
||||||
|
Loading…
Reference in New Issue
Block a user