diff --git a/config.proto b/config.proto index 273c2ec3d..af93288b2 100644 --- a/config.proto +++ b/config.proto @@ -16,6 +16,7 @@ enum ConfigFormat { JSON = 1; } +// Master config of V2Ray. V2Ray Core takes this config as input and functions accordingly. message Config { // Inbound handler configurations. Must have at least one item. repeated v2ray.core.app.proxyman.InboundHandlerConfig inbound = 1; @@ -27,5 +28,7 @@ message Config { // App configuration. Must be one in the app directory. repeated v2ray.core.common.serial.TypedMessage app = 4; + + // Transport settings. v2ray.core.transport.Config transport = 5; } diff --git a/core.go b/core.go index d9fd58197..5f096aad8 100644 --- a/core.go +++ b/core.go @@ -19,8 +19,9 @@ func Version() string { return version } +// PrintVersion prints current version into console. 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()) } diff --git a/loader.go b/loader.go index a480a2f06..eb8118301 100644 --- a/loader.go +++ b/loader.go @@ -5,19 +5,21 @@ import ( "io/ioutil" "github.com/golang/protobuf/proto" - "v2ray.com/core/common/errors" ) +// ConfigLoader is an utility to load V2Ray config from external source. type ConfigLoader func(input io.Reader) (*Config, error) var configLoaderCache = make(map[ConfigFormat]ConfigLoader) +// RegisterConfigLoader add a new ConfigLoader. func RegisterConfigLoader(format ConfigFormat, loader ConfigLoader) error { configLoaderCache[format] = loader return nil } +// LoadConfig loads config with given format from given source. func LoadConfig(format ConfigFormat, input io.Reader) (*Config, error) { loader, found := configLoaderCache[format] if !found { @@ -26,7 +28,7 @@ func LoadConfig(format ConfigFormat, input io.Reader) (*Config, error) { return loader(input) } -func LoadProtobufConfig(input io.Reader) (*Config, error) { +func loadProtobufConfig(input io.Reader) (*Config, error) { config := new(Config) data, _ := ioutil.ReadAll(input) if err := proto.Unmarshal(data, config); err != nil { @@ -36,5 +38,5 @@ func LoadProtobufConfig(input io.Reader) (*Config, error) { } func init() { - RegisterConfigLoader(ConfigFormat_Protobuf, LoadProtobufConfig) + RegisterConfigLoader(ConfigFormat_Protobuf, loadProtobufConfig) } diff --git a/v2ray.go b/v2ray.go index 5890171a1..2bc775520 100644 --- a/v2ray.go +++ b/v2ray.go @@ -11,11 +11,26 @@ import ( "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. type Point struct { 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. // The server is not started at this point. func NewPoint(config *Config) (*Point, error) { @@ -125,8 +140,6 @@ func (v *Point) 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 { if err := v.space.Start(); err != nil { return err diff --git a/v2ray_test.go b/v2ray_test.go index 8a1d79d2e..2566520df 100644 --- a/v2ray_test.go +++ b/v2ray_test.go @@ -58,8 +58,8 @@ func TestV2RayClose(t *testing.T) { }, } - point, err := NewPoint(config) + server, err := New(config) assert.Error(err).IsNil() - point.Close() + server.Close() }