mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-21 09:36:34 -05:00
refactor core
This commit is contained in:
parent
e57124ab87
commit
a32063dfb5
@ -16,6 +16,7 @@ enum ConfigFormat {
|
|||||||
JSON = 1;
|
JSON = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Master config of V2Ray. V2Ray Core takes this config as input and functions accordingly.
|
||||||
message Config {
|
message Config {
|
||||||
// Inbound handler configurations. Must have at least one item.
|
// Inbound handler configurations. Must have at least one item.
|
||||||
repeated v2ray.core.app.proxyman.InboundHandlerConfig inbound = 1;
|
repeated v2ray.core.app.proxyman.InboundHandlerConfig inbound = 1;
|
||||||
@ -27,5 +28,7 @@ message Config {
|
|||||||
|
|
||||||
// App configuration. Must be one in the app directory.
|
// App configuration. Must be one in the app directory.
|
||||||
repeated v2ray.core.common.serial.TypedMessage app = 4;
|
repeated v2ray.core.common.serial.TypedMessage app = 4;
|
||||||
|
|
||||||
|
// Transport settings.
|
||||||
v2ray.core.transport.Config transport = 5;
|
v2ray.core.transport.Config transport = 5;
|
||||||
}
|
}
|
||||||
|
3
core.go
3
core.go
@ -19,8 +19,9 @@ func Version() string {
|
|||||||
return version
|
return version
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PrintVersion prints current version into console.
|
||||||
func PrintVersion() {
|
func PrintVersion() {
|
||||||
fmt.Printf("V2Ray %s (%s) %s%s", version, codename, build, platform.LineSeparator())
|
fmt.Printf("V2Ray %s (%s) %s%s", Version(), codename, build, platform.LineSeparator())
|
||||||
fmt.Printf("%s%s", intro, platform.LineSeparator())
|
fmt.Printf("%s%s", intro, platform.LineSeparator())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,19 +5,21 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
|
||||||
"github.com/golang/protobuf/proto"
|
"github.com/golang/protobuf/proto"
|
||||||
|
|
||||||
"v2ray.com/core/common/errors"
|
"v2ray.com/core/common/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ConfigLoader is an utility to load V2Ray config from external source.
|
||||||
type ConfigLoader func(input io.Reader) (*Config, error)
|
type ConfigLoader func(input io.Reader) (*Config, error)
|
||||||
|
|
||||||
var configLoaderCache = make(map[ConfigFormat]ConfigLoader)
|
var configLoaderCache = make(map[ConfigFormat]ConfigLoader)
|
||||||
|
|
||||||
|
// RegisterConfigLoader add a new ConfigLoader.
|
||||||
func RegisterConfigLoader(format ConfigFormat, loader ConfigLoader) error {
|
func RegisterConfigLoader(format ConfigFormat, loader ConfigLoader) error {
|
||||||
configLoaderCache[format] = loader
|
configLoaderCache[format] = loader
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LoadConfig loads config with given format from given source.
|
||||||
func LoadConfig(format ConfigFormat, input io.Reader) (*Config, error) {
|
func LoadConfig(format ConfigFormat, input io.Reader) (*Config, error) {
|
||||||
loader, found := configLoaderCache[format]
|
loader, found := configLoaderCache[format]
|
||||||
if !found {
|
if !found {
|
||||||
@ -26,7 +28,7 @@ func LoadConfig(format ConfigFormat, input io.Reader) (*Config, error) {
|
|||||||
return loader(input)
|
return loader(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadProtobufConfig(input io.Reader) (*Config, error) {
|
func loadProtobufConfig(input io.Reader) (*Config, error) {
|
||||||
config := new(Config)
|
config := new(Config)
|
||||||
data, _ := ioutil.ReadAll(input)
|
data, _ := ioutil.ReadAll(input)
|
||||||
if err := proto.Unmarshal(data, config); err != nil {
|
if err := proto.Unmarshal(data, config); err != nil {
|
||||||
@ -36,5 +38,5 @@ func LoadProtobufConfig(input io.Reader) (*Config, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
RegisterConfigLoader(ConfigFormat_Protobuf, LoadProtobufConfig)
|
RegisterConfigLoader(ConfigFormat_Protobuf, loadProtobufConfig)
|
||||||
}
|
}
|
||||||
|
17
v2ray.go
17
v2ray.go
@ -11,11 +11,26 @@ import (
|
|||||||
"v2ray.com/core/common/net"
|
"v2ray.com/core/common/net"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Server is an instance of V2Ray. At any time, there must be at most one Server instance running.
|
||||||
|
type Server interface {
|
||||||
|
// Start starts the V2Ray server, and return any error during the process.
|
||||||
|
// In the case of any errors, the state of the server is unpredicatable.
|
||||||
|
Start() error
|
||||||
|
|
||||||
|
// Close closes the V2Ray server. All inbound and outbound connections will be closed immediately.
|
||||||
|
Close()
|
||||||
|
}
|
||||||
|
|
||||||
// Point shell of V2Ray.
|
// Point shell of V2Ray.
|
||||||
type Point struct {
|
type Point struct {
|
||||||
space app.Space
|
space app.Space
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// New creates a new V2Ray server with given config.
|
||||||
|
func New(config *Config) (Server, error) {
|
||||||
|
return NewPoint(config)
|
||||||
|
}
|
||||||
|
|
||||||
// NewPoint returns a new Point server based on given configuration.
|
// NewPoint returns a new Point server based on given configuration.
|
||||||
// The server is not started at this point.
|
// The server is not started at this point.
|
||||||
func NewPoint(config *Config) (*Point, error) {
|
func NewPoint(config *Config) (*Point, error) {
|
||||||
@ -125,8 +140,6 @@ func (v *Point) Close() {
|
|||||||
v.space.Close()
|
v.space.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start starts the Point server, and return any error during the process.
|
|
||||||
// In the case of any errors, the state of the server is unpredicatable.
|
|
||||||
func (v *Point) Start() error {
|
func (v *Point) Start() error {
|
||||||
if err := v.space.Start(); err != nil {
|
if err := v.space.Start(); err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -58,8 +58,8 @@ func TestV2RayClose(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
point, err := NewPoint(config)
|
server, err := New(config)
|
||||||
assert.Error(err).IsNil()
|
assert.Error(err).IsNil()
|
||||||
|
|
||||||
point.Close()
|
server.Close()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user