Skip to content

Commit

Permalink
protobuf for kcp config
Browse files Browse the repository at this point in the history
  • Loading branch information
DarienRaymond committed Sep 21, 2016
1 parent 8f6a972 commit 1a3f51a
Show file tree
Hide file tree
Showing 23 changed files with 463 additions and 82 deletions.
7 changes: 4 additions & 3 deletions proxy/blackhole/config_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ func (this *Config) UnmarshalJSON(data []byte) error {

this.Response = new(NoneResponse)
if jsonConfig.Response != nil {
loader := loader.NewJSONConfigLoader(loader.ConfigCreatorCache{}, "type", "")
loader.RegisterCreator("none", func() interface{} { return new(NoneResponse) })
loader.RegisterCreator("http", func() interface{} { return new(HTTPResponse) })
cache := loader.ConfigCreatorCache{}
loader := loader.NewJSONConfigLoader(cache, "type", "")
cache.RegisterCreator("none", func() interface{} { return new(NoneResponse) })
cache.RegisterCreator("http", func() interface{} { return new(HTTPResponse) })
response, _, err := loader.Load(jsonConfig.Response)
if err != nil {
return errors.New("Blackhole: Failed to parse response config: " + err.Error())
Expand Down
11 changes: 7 additions & 4 deletions proxy/registry/config_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@ package registry
import "v2ray.com/core/common/loader"

var (
inboundConfigCache loader.ConfigLoader
outboundConfigCache loader.ConfigLoader
inboundConfigCreatorCache = loader.ConfigCreatorCache{}
inboundConfigCache loader.ConfigLoader

outboundConfigCreatorCache = loader.ConfigCreatorCache{}
outboundConfigCache loader.ConfigLoader
)

func RegisterInboundConfig(protocol string, creator loader.ConfigCreator) error {
return inboundConfigCache.RegisterCreator(protocol, creator)
return inboundConfigCreatorCache.RegisterCreator(protocol, creator)
}

func RegisterOutboundConfig(protocol string, creator loader.ConfigCreator) error {
return outboundConfigCache.RegisterCreator(protocol, creator)
return outboundConfigCreatorCache.RegisterCreator(protocol, creator)
}

func CreateInboundConfig(protocol string, data []byte) (interface{}, error) {
Expand Down
4 changes: 2 additions & 2 deletions proxy/registry/config_cache_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ import (
)

func init() {
inboundConfigCache = loader.NewJSONConfigLoader(loader.ConfigCreatorCache{}, "protocol", "settings")
outboundConfigCache = loader.NewJSONConfigLoader(loader.ConfigCreatorCache{}, "protocol", "settings")
inboundConfigCache = loader.NewJSONConfigLoader(inboundConfigCreatorCache, "protocol", "settings")
outboundConfigCache = loader.NewJSONConfigLoader(outboundConfigCreatorCache, "protocol", "settings")
}
41 changes: 38 additions & 3 deletions transport/internet/authenticator.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package internet

import (
"errors"

"v2ray.com/core/common"
"v2ray.com/core/common/alloc"
"v2ray.com/core/common/loader"

"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
)

type Authenticator interface {
Expand All @@ -12,14 +18,39 @@ type Authenticator interface {
}

type AuthenticatorFactory interface {
Create(AuthenticatorConfig) Authenticator
Create(interface{}) Authenticator
}

func (this *AuthenticatorConfig) GetInternalConfig() (interface{}, error) {
return configCache.CreateConfig(this.Name)
}

type AuthenticatorConfig interface {
func NewAuthenticatorConfig(name string, config interface{}) (*AuthenticatorConfig, error) {
pbMsg, ok := config.(proto.Message)
if !ok {
return nil, errors.New("Internet|Authenticator: Failed to convert config into proto message.")
}
anyConfig, err := ptypes.MarshalAny(pbMsg)
if err != nil {
return nil, err
}
return &AuthenticatorConfig{
Name: name,
Settings: anyConfig,
}, nil
}

func (this *AuthenticatorConfig) CreateAuthenticator() (Authenticator, error) {
config, err := this.GetInternalConfig()
if err != nil {
return nil, err
}
return CreateAuthenticator(this.Name, config)
}

var (
authenticatorCache = make(map[string]AuthenticatorFactory)
configCache = loader.ConfigCreatorCache{}
)

func RegisterAuthenticator(name string, factory AuthenticatorFactory) error {
Expand All @@ -30,7 +61,11 @@ func RegisterAuthenticator(name string, factory AuthenticatorFactory) error {
return nil
}

func CreateAuthenticator(name string, config AuthenticatorConfig) (Authenticator, error) {
func RegisterAuthenticatorConfig(name string, configCreator loader.ConfigCreator) error {
return configCache.RegisterCreator(name, configCreator)
}

func CreateAuthenticator(name string, config interface{}) (Authenticator, error) {
factory, found := authenticatorCache[name]
if !found {
return nil, common.ErrObjectNotFound
Expand Down
71 changes: 71 additions & 0 deletions transport/internet/authenticator.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions transport/internet/authenticator.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
syntax = "proto3";

package com.v2ray.core.transport.internet;
option go_package = "internet";

import "google/protobuf/any.proto";

message AuthenticatorConfig {
string name = 1;
google.protobuf.Any settings = 2;
}
8 changes: 4 additions & 4 deletions transport/internet/authenticator_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
)

func CreateAuthenticatorConfig(rawConfig []byte) (string, interface{}, error) {
config, name, err := configCache.Load(rawConfig)
config, name, err := configLoader.Load(rawConfig)
if err != nil {
return name, nil, err
}
return name, config, nil
}

func init() {
configCache = loader.NewJSONConfigLoader(loader.ConfigCreatorCache{}, "type", "")
}
var (
configLoader = loader.NewJSONConfigLoader(loader.ConfigCreatorCache{}, "type", "")
)
57 changes: 57 additions & 0 deletions transport/internet/authenticators/noop/config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions transport/internet/authenticators/noop/config.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
syntax = "proto3";

package com.v2ray.core.transport.internet.authenticators.noop;
option go_package = "noop";

message Config {}
5 changes: 2 additions & 3 deletions transport/internet/authenticators/noop/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ func (this NoOpAuthenticator) Seal(payload *alloc.Buffer) {}

type NoOpAuthenticatorFactory struct{}

func (this NoOpAuthenticatorFactory) Create(config internet.AuthenticatorConfig) internet.Authenticator {
func (this NoOpAuthenticatorFactory) Create(config interface{}) internet.Authenticator {
return NoOpAuthenticator{}
}

type NoOpAuthenticatorConfig struct{}

func init() {
internet.RegisterAuthenticator("none", NoOpAuthenticatorFactory{})
internet.RegisterAuthenticatorConfig("none", func() interface{} { return &Config{} })
}
11 changes: 0 additions & 11 deletions transport/internet/authenticators/noop/noop_json.go

This file was deleted.

70 changes: 70 additions & 0 deletions transport/internet/authenticators/srtp/config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions transport/internet/authenticators/srtp/config.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
syntax = "proto3";

package com.v2ray.core.transport.internet.authenticators.srtp;
option go_package = "srtp";

message Config {
uint32 version = 1;
bool padding = 2;
bool extension = 3;
uint32 csrc_count = 4;
bool marker = 5;
uint32 payload_type = 6;
}
Loading

0 comments on commit 1a3f51a

Please sign in to comment.