mirror of
https://github.com/v2ray/v2ray-core.git
synced 2026-11-13 14:44:15 +00:00
massive refactoring for configuration
This commit is contained in:
1 parent
791ac780f0
commit
0ca7dab49b
15 files changed
+212
-147
No files matched your search
@@ -1,31 +1,76 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/v2ray/v2ray-core/log"
|
||||
v2net "github.com/v2ray/v2ray-core/net"
|
||||
)
|
||||
|
||||
var (
|
||||
inboundFactories = make(map[string]InboundConnectionHandlerFactory)
|
||||
outboundFactories = make(map[string]OutboundConnectionHandlerFactory)
|
||||
)
|
||||
|
||||
func RegisterInboundConnectionHandlerFactory(name string, factory InboundConnectionHandlerFactory) error {
|
||||
// TODO check name
|
||||
inboundFactories[name] = factory
|
||||
return nil
|
||||
}
|
||||
|
||||
func RegisterOutboundConnectionHandlerFactory(name string, factory OutboundConnectionHandlerFactory) error {
|
||||
// TODO check name
|
||||
outboundFactories[name] = factory
|
||||
return nil
|
||||
}
|
||||
|
||||
// VPoint is an single server in V2Ray system.
|
||||
type VPoint struct {
|
||||
Config VConfig
|
||||
port uint16
|
||||
ichFactory InboundConnectionHandlerFactory
|
||||
ichConfig []byte
|
||||
ochFactory OutboundConnectionHandlerFactory
|
||||
ochConfig []byte
|
||||
}
|
||||
|
||||
// NewVPoint returns a new VPoint server based on given configuration.
|
||||
// The server is not started at this point.
|
||||
func NewVPoint(config *VConfig, ichFactory InboundConnectionHandlerFactory, ochFactory OutboundConnectionHandlerFactory) (*VPoint, error) {
|
||||
func NewVPoint(config VConfig) (*VPoint, error) {
|
||||
var vpoint = new(VPoint)
|
||||
vpoint.Config = *config
|
||||
vpoint.port = config.Port
|
||||
|
||||
ichFactory, ok := inboundFactories[config.InboundConfig.Protocol]
|
||||
if !ok {
|
||||
panic(log.Error("Unknown inbound connection handler factory %s", config.InboundConfig.Protocol))
|
||||
}
|
||||
vpoint.ichFactory = ichFactory
|
||||
if len(config.InboundConfig.File) > 0 {
|
||||
ichConfig, err := ioutil.ReadFile(config.InboundConfig.File)
|
||||
if err != nil {
|
||||
panic(log.Error("Unable to read config file %v", err))
|
||||
}
|
||||
vpoint.ichConfig = ichConfig
|
||||
}
|
||||
|
||||
ochFactory, ok := outboundFactories[config.OutboundConfig.Protocol]
|
||||
if !ok {
|
||||
panic(log.Error("Unknown outbound connection handler factory %s", config.OutboundConfig.Protocol))
|
||||
}
|
||||
|
||||
vpoint.ochFactory = ochFactory
|
||||
if len(config.OutboundConfig.File) > 0 {
|
||||
ochConfig, err := ioutil.ReadFile(config.OutboundConfig.File)
|
||||
if err != nil {
|
||||
panic(log.Error("Unable to read config file %v", err))
|
||||
}
|
||||
vpoint.ochConfig = ochConfig
|
||||
}
|
||||
|
||||
return vpoint, nil
|
||||
}
|
||||
|
||||
type InboundConnectionHandlerFactory interface {
|
||||
Create(vPoint *VPoint) (InboundConnectionHandler, error)
|
||||
Create(vp *VPoint, config []byte) (InboundConnectionHandler, error)
|
||||
}
|
||||
|
||||
type InboundConnectionHandler interface {
|
||||
@@ -33,7 +78,7 @@ type InboundConnectionHandler interface {
|
||||
}
|
||||
|
||||
type OutboundConnectionHandlerFactory interface {
|
||||
Create(vPoint *VPoint, dest v2net.VAddress) (OutboundConnectionHandler, error)
|
||||
Create(VP *VPoint, config []byte, dest v2net.VAddress) (OutboundConnectionHandler, error)
|
||||
}
|
||||
|
||||
type OutboundConnectionHandler interface {
|
||||
@@ -43,21 +88,21 @@ type OutboundConnectionHandler interface {
|
||||
// Start starts the VPoint server, and return any error during the process.
|
||||
// In the case of any errors, the state of the server is unpredicatable.
|
||||
func (vp *VPoint) Start() error {
|
||||
if vp.Config.Port <= 0 {
|
||||
return fmt.Errorf("Invalid port %d", vp.Config.Port)
|
||||
if vp.port <= 0 {
|
||||
return log.Error("Invalid port %d", vp.port)
|
||||
}
|
||||
inboundConnectionHandler, err := vp.ichFactory.Create(vp)
|
||||
inboundConnectionHandler, err := vp.ichFactory.Create(vp, vp.ichConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = inboundConnectionHandler.Listen(vp.Config.Port)
|
||||
err = inboundConnectionHandler.Listen(vp.port)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (vp *VPoint) NewInboundConnectionAccepted(destination v2net.VAddress) InboundVRay {
|
||||
ray := NewVRay()
|
||||
// TODO: handle error
|
||||
och, _ := vp.ochFactory.Create(vp, destination)
|
||||
och, _ := vp.ochFactory.Create(vp, vp.ochConfig, destination)
|
||||
_ = och.Start(ray)
|
||||
return ray
|
||||
}
|
||||
Reference in new issue
Block a user