1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-07 02:36:26 -05:00

protobuf for blackhole config

This commit is contained in:
Darien Raymond 2016-09-22 12:01:36 +02:00
parent 6317768cab
commit dc6b7e2e75
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
7 changed files with 245 additions and 28 deletions

View File

@ -13,14 +13,18 @@ import (
// BlackHole is an outbound connection that sliently swallow the entire payload.
type BlackHole struct {
meta *proxy.OutboundHandlerMeta
response Response
response ResponseConfig
}
func NewBlackHole(space app.Space, config *Config, meta *proxy.OutboundHandlerMeta) *BlackHole {
func NewBlackHole(space app.Space, config *Config, meta *proxy.OutboundHandlerMeta) (*BlackHole, error) {
response, err := config.Response.GetInternalResponse()
if err != nil {
return nil, err
}
return &BlackHole{
meta: meta,
response: config.Response,
}
response: response,
}, nil
}
func (this *BlackHole) Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) error {
@ -41,7 +45,7 @@ func (this *Factory) StreamCapability() internet.StreamConnectionType {
}
func (this *Factory) Create(space app.Space, config interface{}, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) {
return NewBlackHole(space, config.(*Config), meta), nil
return NewBlackHole(space, config.(*Config), meta)
}
func init() {

View File

@ -3,23 +3,14 @@ package blackhole
import (
"v2ray.com/core/common/alloc"
v2io "v2ray.com/core/common/io"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/any"
"strings"
"v2ray.com/core/common/loader"
)
type Config struct {
Response Response
}
type Response interface {
WriteTo(v2io.Writer)
}
type NoneResponse struct{}
func (this *NoneResponse) WriteTo(writer v2io.Writer) {}
type HTTPResponse struct {
}
const (
http403response = `HTTP/1.1 403 Forbidden
Connection: close
@ -30,6 +21,51 @@ Content-Length: 0
`
)
type ResponseConfig interface {
AsAny() *any.Any
WriteTo(v2io.Writer)
}
func (this *NoneResponse) WriteTo(v2io.Writer) {}
func (this *NoneResponse) AsAny() *any.Any {
r, _ := ptypes.MarshalAny(this)
return r
}
func (this *HTTPResponse) WriteTo(writer v2io.Writer) {
writer.Write(alloc.NewLocalBuffer(512).Clear().AppendString(http403response))
}
func (this *HTTPResponse) AsAny() *any.Any {
r, _ := ptypes.MarshalAny(this)
return r
}
func (this *Response) GetInternalResponse() (ResponseConfig, error) {
if this == nil {
return new(NoneResponse), nil
}
var r ResponseConfig
switch this.Type {
case Response_None:
r = new(NoneResponse)
case Response_HTTP:
r = new(HTTPResponse)
}
err := ptypes.UnmarshalAny(this.Settings, r.(proto.Message))
if err != nil {
return nil, err
}
return r, nil
}
var (
cache = loader.ConfigCreatorCache{}
)
func init() {
cache.RegisterCreator(strings.ToLower(Response_Type_name[int32(Response_None)]), func() interface{} { return new(NoneResponse) })
cache.RegisterCreator(strings.ToLower(Response_Type_name[int32(Response_HTTP)]), func() interface{} { return new(HTTPResponse) })
}

View File

@ -0,0 +1,133 @@
// Code generated by protoc-gen-go.
// source: v2ray.com/core/proxy/blackhole/config.proto
// DO NOT EDIT!
/*
Package blackhole is a generated protocol buffer package.
It is generated from these files:
v2ray.com/core/proxy/blackhole/config.proto
It has these top-level messages:
NoneResponse
HTTPResponse
Response
Config
*/
package blackhole
import proto "github.com/golang/protobuf/proto"
import fmt "fmt"
import math "math"
import google_protobuf "github.com/golang/protobuf/ptypes/any"
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
type Response_Type int32
const (
Response_None Response_Type = 0
Response_HTTP Response_Type = 1
)
var Response_Type_name = map[int32]string{
0: "None",
1: "HTTP",
}
var Response_Type_value = map[string]int32{
"None": 0,
"HTTP": 1,
}
func (x Response_Type) String() string {
return proto.EnumName(Response_Type_name, int32(x))
}
func (Response_Type) EnumDescriptor() ([]byte, []int) { return fileDescriptor0, []int{2, 0} }
type NoneResponse struct {
}
func (m *NoneResponse) Reset() { *m = NoneResponse{} }
func (m *NoneResponse) String() string { return proto.CompactTextString(m) }
func (*NoneResponse) ProtoMessage() {}
func (*NoneResponse) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
type HTTPResponse struct {
}
func (m *HTTPResponse) Reset() { *m = HTTPResponse{} }
func (m *HTTPResponse) String() string { return proto.CompactTextString(m) }
func (*HTTPResponse) ProtoMessage() {}
func (*HTTPResponse) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} }
type Response struct {
Type Response_Type `protobuf:"varint,1,opt,name=type,enum=com.v2ray.core.proxy.blackhole.Response_Type" json:"type,omitempty"`
Settings *google_protobuf.Any `protobuf:"bytes,2,opt,name=settings" json:"settings,omitempty"`
}
func (m *Response) Reset() { *m = Response{} }
func (m *Response) String() string { return proto.CompactTextString(m) }
func (*Response) ProtoMessage() {}
func (*Response) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{2} }
func (m *Response) GetSettings() *google_protobuf.Any {
if m != nil {
return m.Settings
}
return nil
}
type Config struct {
Response *Response `protobuf:"bytes,1,opt,name=response" json:"response,omitempty"`
}
func (m *Config) Reset() { *m = Config{} }
func (m *Config) String() string { return proto.CompactTextString(m) }
func (*Config) ProtoMessage() {}
func (*Config) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{3} }
func (m *Config) GetResponse() *Response {
if m != nil {
return m.Response
}
return nil
}
func init() {
proto.RegisterType((*NoneResponse)(nil), "com.v2ray.core.proxy.blackhole.NoneResponse")
proto.RegisterType((*HTTPResponse)(nil), "com.v2ray.core.proxy.blackhole.HTTPResponse")
proto.RegisterType((*Response)(nil), "com.v2ray.core.proxy.blackhole.Response")
proto.RegisterType((*Config)(nil), "com.v2ray.core.proxy.blackhole.Config")
proto.RegisterEnum("com.v2ray.core.proxy.blackhole.Response_Type", Response_Type_name, Response_Type_value)
}
func init() { proto.RegisterFile("v2ray.com/core/proxy/blackhole/config.proto", fileDescriptor0) }
var fileDescriptor0 = []byte{
// 249 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x8c, 0x90, 0x41, 0x4b, 0xc3, 0x30,
0x18, 0x86, 0xad, 0x94, 0x51, 0x53, 0x19, 0x23, 0x78, 0x98, 0x3b, 0xc8, 0xe8, 0xa9, 0x20, 0x7e,
0x91, 0xfa, 0x0b, 0xa6, 0x1e, 0x3c, 0x0d, 0x29, 0x3d, 0x79, 0x6b, 0xc3, 0xb7, 0x3a, 0xec, 0xf2,
0x85, 0x34, 0x8a, 0xf9, 0x2d, 0xfe, 0x59, 0x49, 0x66, 0x73, 0x94, 0xdd, 0xf2, 0x92, 0xf7, 0xc9,
0xfb, 0x10, 0x76, 0xfb, 0x55, 0x99, 0xd6, 0x81, 0xa4, 0x83, 0x90, 0x64, 0x50, 0x68, 0x43, 0xdf,
0x4e, 0x74, 0x43, 0x2b, 0x3f, 0xde, 0x69, 0x40, 0x21, 0x49, 0xed, 0xf6, 0x3d, 0x68, 0x43, 0x96,
0xf8, 0x8d, 0xa4, 0x03, 0x4c, 0x80, 0x41, 0x08, 0x65, 0x88, 0xe5, 0xd5, 0x75, 0x4f, 0xd4, 0x0f,
0xe1, 0x11, 0x4b, 0xdd, 0xe7, 0x4e, 0xb4, 0xca, 0x1d, 0xd1, 0x62, 0xce, 0x2e, 0xb7, 0xa4, 0xb0,
0xc6, 0x51, 0x93, 0x1a, 0xd1, 0xe7, 0x97, 0xa6, 0x79, 0x8d, 0xf9, 0x27, 0x61, 0xd9, 0x14, 0xf8,
0x86, 0xa5, 0xd6, 0x69, 0x5c, 0x26, 0xeb, 0xa4, 0x9c, 0x57, 0x77, 0xf0, 0xff, 0x2c, 0x4c, 0x1c,
0x34, 0x4e, 0x63, 0x1d, 0x50, 0x7e, 0xcf, 0xb2, 0x11, 0xad, 0xdd, 0xab, 0x7e, 0x5c, 0x9e, 0xaf,
0x93, 0x32, 0xaf, 0xae, 0xe0, 0x68, 0x07, 0x93, 0x1d, 0x6c, 0x94, 0xab, 0x63, 0xab, 0x58, 0xb1,
0xd4, 0xf3, 0x3c, 0x63, 0xa9, 0x37, 0x5d, 0x9c, 0xf9, 0x93, 0x77, 0x5c, 0x24, 0xc5, 0x96, 0xcd,
0x9e, 0xc2, 0x47, 0xf0, 0x67, 0x96, 0x99, 0xbf, 0xb9, 0xa0, 0x97, 0x57, 0xe5, 0xa9, 0x7a, 0x75,
0x24, 0x1f, 0xf3, 0xb7, 0x8b, 0x78, 0xdf, 0xcd, 0x82, 0xd0, 0xc3, 0x6f, 0x00, 0x00, 0x00, 0xff,
0xff, 0x09, 0xa5, 0x68, 0x2e, 0x8b, 0x01, 0x00, 0x00,
}

View File

@ -0,0 +1,27 @@
syntax = "proto3";
package com.v2ray.core.proxy.blackhole;
option go_package = "blackhole";
import "google/protobuf/any.proto";
message NoneResponse {
}
message HTTPResponse {
}
message Response {
enum Type {
None = 0;
HTTP = 1;
}
Type type = 1;
google.protobuf.Any settings = 2;
}
message Config {
Response response = 1;
}

View File

@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"strings"
"v2ray.com/core/common/loader"
"v2ray.com/core/proxy/registry"
)
@ -19,22 +20,28 @@ func (this *Config) UnmarshalJSON(data []byte) error {
return errors.New("Blackhole: Failed to parse config: " + err.Error())
}
this.Response = new(NoneResponse)
if jsonConfig.Response != nil {
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)
response, rType, err := configLoader.Load(jsonConfig.Response)
if err != nil {
return errors.New("Blackhole: Failed to parse response config: " + err.Error())
}
this.Response = response.(Response)
this.Response = new(Response)
switch rType {
case strings.ToLower(Response_Type_name[int32(Response_None)]):
this.Response.Type = Response_None
case strings.ToLower(Response_Type_name[int32(Response_HTTP)]):
this.Response.Type = Response_HTTP
}
this.Response.Settings = response.(ResponseConfig).AsAny()
}
return nil
}
var (
configLoader = loader.NewJSONConfigLoader(cache, "type", "")
)
func init() {
registry.RegisterOutboundConfig("blackhole", func() interface{} { return new(Config) })
}

View File

@ -22,6 +22,10 @@ func TestHTTPResponseJSON(t *testing.T) {
err := json.Unmarshal([]byte(rawJson), config)
assert.Error(err).IsNil()
_, ok := config.Response.(*HTTPResponse)
assert.Int(int(config.Response.Type)).Equals(int(Response_HTTP))
response, err := config.Response.GetInternalResponse()
assert.Error(err).IsNil()
_, ok := response.(*HTTPResponse)
assert.Bool(ok).IsTrue()
}

View File

@ -35,6 +35,7 @@ var _ = math.Inf
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
// Maximum Transmission Unit, in bytes.
type MTU struct {
Value uint32 `protobuf:"varint,1,opt,name=value" json:"value,omitempty"`
}
@ -44,6 +45,7 @@ func (m *MTU) String() string { return proto.CompactTextString(m) }
func (*MTU) ProtoMessage() {}
func (*MTU) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
// Transmission Time Interview, in milli-sec.
type TTI struct {
Value uint32 `protobuf:"varint,1,opt,name=value" json:"value,omitempty"`
}
@ -53,6 +55,7 @@ func (m *TTI) String() string { return proto.CompactTextString(m) }
func (*TTI) ProtoMessage() {}
func (*TTI) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} }
// Uplink capacity, in MB.
type UplinkCapacity struct {
Value uint32 `protobuf:"varint,1,opt,name=value" json:"value,omitempty"`
}
@ -62,6 +65,7 @@ func (m *UplinkCapacity) String() string { return proto.CompactTextSt
func (*UplinkCapacity) ProtoMessage() {}
func (*UplinkCapacity) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{2} }
// Downlink capacity, in MB.
type DownlinkCapacity struct {
Value uint32 `protobuf:"varint,1,opt,name=value" json:"value,omitempty"`
}
@ -72,6 +76,7 @@ func (*DownlinkCapacity) ProtoMessage() {}
func (*DownlinkCapacity) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{3} }
type WriteBuffer struct {
// Buffer size in bytes.
Size uint32 `protobuf:"varint,1,opt,name=size" json:"size,omitempty"`
}
@ -81,6 +86,7 @@ func (*WriteBuffer) ProtoMessage() {}
func (*WriteBuffer) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{4} }
type ReadBuffer struct {
// Buffer size in bytes.
Size uint32 `protobuf:"varint,1,opt,name=size" json:"size,omitempty"`
}