1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-26 20:16:55 -05:00

try protobuf

This commit is contained in:
Darien Raymond 2016-08-25 21:55:49 +02:00
parent 04d956462c
commit bbca180dba
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
20 changed files with 216 additions and 51 deletions

View File

@ -0,0 +1,5 @@
syntax = "proto3";
message Config {
}

View File

@ -11,15 +11,15 @@ var (
) )
type TimeOutReader struct { type TimeOutReader struct {
timeout int timeout uint32
connection net.Conn connection net.Conn
worker io.Reader worker io.Reader
} }
func NewTimeOutReader(timeout int /* seconds */, connection net.Conn) *TimeOutReader { func NewTimeOutReader(timeout uint32 /* seconds */, connection net.Conn) *TimeOutReader {
reader := &TimeOutReader{ reader := &TimeOutReader{
connection: connection, connection: connection,
timeout: -100, timeout: 0,
} }
reader.SetTimeOut(timeout) reader.SetTimeOut(timeout)
return reader return reader
@ -29,12 +29,12 @@ func (reader *TimeOutReader) Read(p []byte) (int, error) {
return reader.worker.Read(p) return reader.worker.Read(p)
} }
func (reader *TimeOutReader) GetTimeOut() int { func (reader *TimeOutReader) GetTimeOut() uint32 {
return reader.timeout return reader.timeout
} }
func (reader *TimeOutReader) SetTimeOut(value int) { func (reader *TimeOutReader) SetTimeOut(value uint32) {
if value == reader.timeout { if reader.worker != nil && value == reader.timeout {
return return
} }
reader.timeout = value reader.timeout = value
@ -56,7 +56,7 @@ func (reader *TimeOutReader) Release() {
} }
type timedReaderWorker struct { type timedReaderWorker struct {
timeout int timeout uint32
connection net.Conn connection net.Conn
} }

View File

@ -11,9 +11,9 @@ func TestTimeOutSettings(t *testing.T) {
assert := assert.On(t) assert := assert.On(t)
reader := NewTimeOutReader(8, nil) reader := NewTimeOutReader(8, nil)
assert.Int(reader.GetTimeOut()).Equals(8) assert.Uint32(reader.GetTimeOut()).Equals(8)
reader.SetTimeOut(8) // no op reader.SetTimeOut(8) // no op
assert.Int(reader.GetTimeOut()).Equals(8) assert.Uint32(reader.GetTimeOut()).Equals(8)
reader.SetTimeOut(9) reader.SetTimeOut(9)
assert.Int(reader.GetTimeOut()).Equals(9) assert.Uint32(reader.GetTimeOut()).Equals(9)
} }

View File

@ -21,7 +21,7 @@ func NewUser(level UserLevel, email string) *User {
} }
type UserSettings struct { type UserSettings struct {
PayloadReadTimeout int PayloadReadTimeout uint32
} }
func GetUserSettings(level UserLevel) UserSettings { func GetUserSettings(level UserLevel) UserSettings {

View File

@ -9,5 +9,5 @@ type Config struct {
Address v2net.Address Address v2net.Address
Port v2net.Port Port v2net.Port
Network *v2net.NetworkList Network *v2net.NetworkList
Timeout int Timeout uint32
} }

View File

@ -15,7 +15,7 @@ func (this *Config) UnmarshalJSON(data []byte) error {
Host *v2net.AddressJson `json:"address"` Host *v2net.AddressJson `json:"address"`
PortValue v2net.Port `json:"port"` PortValue v2net.Port `json:"port"`
NetworkList *v2net.NetworkList `json:"network"` NetworkList *v2net.NetworkList `json:"network"`
TimeoutValue int `json:"timeout"` TimeoutValue uint32 `json:"timeout"`
Redirect bool `json:"followRedirect"` Redirect bool `json:"followRedirect"`
} }
rawConfig := new(DokodemoConfig) rawConfig := new(DokodemoConfig)

View File

@ -1,13 +1,3 @@
//go:generate protoc --go_out=. config.proto
package freedom package freedom
type DomainStrategy int
const (
DomainStrategyAsIs = DomainStrategy(0)
DomainStrategyUseIP = DomainStrategy(1)
)
type Config struct {
DomainStrategy DomainStrategy
Timeout uint32
}

View File

@ -0,0 +1,82 @@
// Code generated by protoc-gen-go.
// source: config.proto
// DO NOT EDIT!
/*
Package freedom is a generated protocol buffer package.
It is generated from these files:
config.proto
It has these top-level messages:
Config
*/
package freedom
import proto "github.com/golang/protobuf/proto"
import fmt "fmt"
import math "math"
// 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 Config_DomainStrategy int32
const (
Config_AS_IS Config_DomainStrategy = 0
Config_USE_IP Config_DomainStrategy = 1
)
var Config_DomainStrategy_name = map[int32]string{
0: "AS_IS",
1: "USE_IP",
}
var Config_DomainStrategy_value = map[string]int32{
"AS_IS": 0,
"USE_IP": 1,
}
func (x Config_DomainStrategy) String() string {
return proto.EnumName(Config_DomainStrategy_name, int32(x))
}
func (Config_DomainStrategy) EnumDescriptor() ([]byte, []int) { return fileDescriptor0, []int{0, 0} }
type Config struct {
DomainStrategy Config_DomainStrategy `protobuf:"varint,1,opt,name=domainStrategy,enum=com.v2ray.core.proxy.freedom.Config_DomainStrategy" json:"domainStrategy,omitempty"`
Timeout uint32 `protobuf:"varint,2,opt,name=timeout" json:"timeout,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{0} }
func init() {
proto.RegisterType((*Config)(nil), "com.v2ray.core.proxy.freedom.Config")
proto.RegisterEnum("com.v2ray.core.proxy.freedom.Config_DomainStrategy", Config_DomainStrategy_name, Config_DomainStrategy_value)
}
func init() { proto.RegisterFile("config.proto", fileDescriptor0) }
var fileDescriptor0 = []byte{
// 174 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0xe2, 0x49, 0xce, 0xcf, 0x4b,
0xcb, 0x4c, 0xd7, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x92, 0x49, 0xce, 0xcf, 0xd5, 0x2b, 0x33,
0x2a, 0x4a, 0xac, 0xd4, 0x4b, 0xce, 0x2f, 0x4a, 0x05, 0x89, 0x56, 0x54, 0xea, 0xa5, 0x15, 0xa5,
0xa6, 0xa6, 0xe4, 0xe7, 0x2a, 0xad, 0x60, 0xe4, 0x62, 0x73, 0x06, 0x2b, 0x17, 0x8a, 0xe6, 0xe2,
0x4b, 0xc9, 0xcf, 0x4d, 0xcc, 0xcc, 0x0b, 0x2e, 0x29, 0x4a, 0x2c, 0x49, 0x4d, 0xaf, 0x94, 0x60,
0x54, 0x60, 0xd4, 0xe0, 0x33, 0x32, 0xd6, 0xc3, 0x67, 0x82, 0x1e, 0x44, 0xb7, 0x9e, 0x0b, 0x8a,
0xd6, 0x20, 0x34, 0xa3, 0x84, 0x24, 0xb8, 0xd8, 0x4b, 0x32, 0x73, 0x53, 0xf3, 0x4b, 0x4b, 0x24,
0x98, 0x14, 0x18, 0x35, 0x78, 0x83, 0x60, 0x5c, 0x25, 0x75, 0x2e, 0x3e, 0x54, 0xbd, 0x42, 0x9c,
0x5c, 0xac, 0x8e, 0xc1, 0xf1, 0x9e, 0xc1, 0x02, 0x0c, 0x42, 0x5c, 0x5c, 0x6c, 0xa1, 0xc1, 0xae,
0xf1, 0x9e, 0x01, 0x02, 0x8c, 0x4e, 0x9c, 0x51, 0xec, 0x50, 0x3b, 0x93, 0xd8, 0xc0, 0x5e, 0x33,
0x06, 0x04, 0x00, 0x00, 0xff, 0xff, 0x3c, 0x68, 0xdc, 0x2d, 0xea, 0x00, 0x00, 0x00,
}

View File

@ -0,0 +1,13 @@
syntax = "proto3";
package com.v2ray.core.proxy.freedom;
option go_package = "freedom";
message Config {
enum DomainStrategy {
AS_IS = 0;
USE_IP = 1;
}
DomainStrategy domainStrategy = 1;
uint32 timeout = 2;
}

View File

@ -19,10 +19,10 @@ func (this *Config) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, jsonConfig); err != nil { if err := json.Unmarshal(data, jsonConfig); err != nil {
return errors.New("Freedom: Failed to parse config: " + err.Error()) return errors.New("Freedom: Failed to parse config: " + err.Error())
} }
this.DomainStrategy = DomainStrategyAsIs this.DomainStrategy = Config_AS_IS
domainStrategy := strings.ToLower(jsonConfig.DomainStrategy) domainStrategy := strings.ToLower(jsonConfig.DomainStrategy)
if domainStrategy == "useip" { if domainStrategy == "useip" || domainStrategy == "use_ip" {
this.DomainStrategy = DomainStrategyUseIP this.DomainStrategy = Config_USE_IP
} }
this.Timeout = jsonConfig.Timeout this.Timeout = jsonConfig.Timeout
return nil return nil

View File

@ -19,7 +19,7 @@ import (
) )
type FreedomConnection struct { type FreedomConnection struct {
domainStrategy DomainStrategy domainStrategy Config_DomainStrategy
timeout uint32 timeout uint32
dns dns.Server dns dns.Server
meta *proxy.OutboundHandlerMeta meta *proxy.OutboundHandlerMeta
@ -32,7 +32,7 @@ func NewFreedomConnection(config *Config, space app.Space, meta *proxy.OutboundH
meta: meta, meta: meta,
} }
space.InitializeApplication(func() error { space.InitializeApplication(func() error {
if config.DomainStrategy == DomainStrategyUseIP { if config.DomainStrategy == Config_USE_IP {
if !space.HasApp(dns.APP_ID) { if !space.HasApp(dns.APP_ID) {
log.Error("Freedom: DNS server is not found in the space.") log.Error("Freedom: DNS server is not found in the space.")
return app.ErrMissingApplication return app.ErrMissingApplication
@ -75,7 +75,7 @@ func (this *FreedomConnection) Dispatch(destination v2net.Destination, payload *
defer ray.OutboundOutput().Close() defer ray.OutboundOutput().Close()
var conn internet.Connection var conn internet.Connection
if this.domainStrategy == DomainStrategyUseIP && destination.Address().Family().IsDomain() { if this.domainStrategy == Config_USE_IP && destination.Address().Family().IsDomain() {
destination = this.ResolveIP(destination) destination = this.ResolveIP(destination)
} }
err := retry.Timed(5, 100).On(func() error { err := retry.Timed(5, 100).On(func() error {
@ -116,7 +116,7 @@ func (this *FreedomConnection) Dispatch(destination v2net.Destination, payload *
timeout = 16 timeout = 16
} }
if timeout > 0 { if timeout > 0 {
reader = v2net.NewTimeOutReader(int(timeout) /* seconds */, conn) reader = v2net.NewTimeOutReader(timeout /* seconds */, conn)
} }
v2reader := v2io.NewAdaptiveReader(reader) v2reader := v2io.NewAdaptiveReader(reader)

View File

@ -97,7 +97,7 @@ func TestIPResolution(t *testing.T) {
space.BindApp(dns.APP_ID, dnsServer) space.BindApp(dns.APP_ID, dnsServer)
freedom := NewFreedomConnection( freedom := NewFreedomConnection(
&Config{DomainStrategy: DomainStrategyUseIP}, &Config{DomainStrategy: Config_USE_IP},
space, space,
&proxy.OutboundHandlerMeta{ &proxy.OutboundHandlerMeta{
Address: v2net.AnyIP, Address: v2net.AnyIP,

View File

@ -1,10 +1,3 @@
//go:generate protoc --go_out=. config.proto
package http package http
// Config for HTTP proxy server.
type Config struct {
Timeout int
}
// ClientConfig for HTTP proxy client.
type ClientConfig struct {
}

68
proxy/http/config.pb.go Normal file
View File

@ -0,0 +1,68 @@
// Code generated by protoc-gen-go.
// source: config.proto
// DO NOT EDIT!
/*
Package http is a generated protocol buffer package.
It is generated from these files:
config.proto
It has these top-level messages:
ServerConfig
ClientConfig
*/
package http
import proto "github.com/golang/protobuf/proto"
import fmt "fmt"
import math "math"
// 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
// Config for HTTP proxy server.
type ServerConfig struct {
Timeout uint32 `protobuf:"varint,1,opt,name=timeout" json:"timeout,omitempty"`
}
func (m *ServerConfig) Reset() { *m = ServerConfig{} }
func (m *ServerConfig) String() string { return proto.CompactTextString(m) }
func (*ServerConfig) ProtoMessage() {}
func (*ServerConfig) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
// ClientConfig for HTTP proxy client.
type ClientConfig struct {
}
func (m *ClientConfig) Reset() { *m = ClientConfig{} }
func (m *ClientConfig) String() string { return proto.CompactTextString(m) }
func (*ClientConfig) ProtoMessage() {}
func (*ClientConfig) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} }
func init() {
proto.RegisterType((*ServerConfig)(nil), "com.v2ray.core.proxy.http.ServerConfig")
proto.RegisterType((*ClientConfig)(nil), "com.v2ray.core.proxy.http.ClientConfig")
}
func init() { proto.RegisterFile("config.proto", fileDescriptor0) }
var fileDescriptor0 = []byte{
// 119 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0xe2, 0x49, 0xce, 0xcf, 0x4b,
0xcb, 0x4c, 0xd7, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x92, 0x4c, 0xce, 0xcf, 0xd5, 0x2b, 0x33,
0x2a, 0x4a, 0xac, 0xd4, 0x4b, 0xce, 0x2f, 0x4a, 0x05, 0x89, 0x56, 0x54, 0xea, 0x65, 0x94, 0x94,
0x14, 0x28, 0x69, 0x70, 0xf1, 0x04, 0xa7, 0x16, 0x95, 0xa5, 0x16, 0x39, 0x83, 0x35, 0x08, 0x49,
0x70, 0xb1, 0x97, 0x64, 0xe6, 0xa6, 0xe6, 0x97, 0x96, 0x48, 0x30, 0x2a, 0x30, 0x6a, 0xf0, 0x06,
0xc1, 0xb8, 0x4a, 0x7c, 0x5c, 0x3c, 0xce, 0x39, 0x99, 0xa9, 0x79, 0x25, 0x10, 0x95, 0x4e, 0x6c,
0x51, 0x2c, 0x20, 0x13, 0x92, 0xd8, 0xc0, 0x76, 0x18, 0x03, 0x02, 0x00, 0x00, 0xff, 0xff, 0xd6,
0xea, 0x59, 0xf8, 0x73, 0x00, 0x00, 0x00,
}

14
proxy/http/config.proto Normal file
View File

@ -0,0 +1,14 @@
syntax = "proto3";
package com.v2ray.core.proxy.http;
option go_package = "http";
// Config for HTTP proxy server.
message ServerConfig {
uint32 timeout = 1;
}
// ClientConfig for HTTP proxy client.
message ClientConfig {
}

View File

@ -10,9 +10,9 @@ import (
) )
// UnmarshalJSON implements json.Unmarshaler // UnmarshalJSON implements json.Unmarshaler
func (this *Config) UnmarshalJSON(data []byte) error { func (this *ServerConfig) UnmarshalJSON(data []byte) error {
type JsonConfig struct { type JsonConfig struct {
Timeout int `json:"timeout"` Timeout uint32 `json:"timeout"`
} }
jsonConfig := new(JsonConfig) jsonConfig := new(JsonConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil { if err := json.Unmarshal(data, jsonConfig); err != nil {
@ -24,5 +24,5 @@ func (this *Config) UnmarshalJSON(data []byte) error {
} }
func init() { func init() {
registry.RegisterInboundConfig("http", func() interface{} { return new(Config) }) registry.RegisterInboundConfig("http", func() interface{} { return new(ServerConfig) })
} }

View File

@ -26,12 +26,12 @@ type Server struct {
sync.Mutex sync.Mutex
accepting bool accepting bool
packetDispatcher dispatcher.PacketDispatcher packetDispatcher dispatcher.PacketDispatcher
config *Config config *ServerConfig
tcpListener *internet.TCPHub tcpListener *internet.TCPHub
meta *proxy.InboundHandlerMeta meta *proxy.InboundHandlerMeta
} }
func NewServer(config *Config, packetDispatcher dispatcher.PacketDispatcher, meta *proxy.InboundHandlerMeta) *Server { func NewServer(config *ServerConfig, packetDispatcher dispatcher.PacketDispatcher, meta *proxy.InboundHandlerMeta) *Server {
return &Server{ return &Server{
packetDispatcher: packetDispatcher, packetDispatcher: packetDispatcher,
config: config, config: config,
@ -273,7 +273,7 @@ func (this *ServerFactory) Create(space app.Space, rawConfig interface{}, meta *
return nil, common.ErrBadConfiguration return nil, common.ErrBadConfiguration
} }
return NewServer( return NewServer(
rawConfig.(*Config), rawConfig.(*ServerConfig),
space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher), space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher),
meta), nil meta), nil
} }

View File

@ -58,7 +58,7 @@ func TestNormalGetRequest(t *testing.T) {
port := v2net.Port(dice.Roll(20000) + 10000) port := v2net.Port(dice.Roll(20000) + 10000)
httpProxy := NewServer( httpProxy := NewServer(
&Config{}, &ServerConfig{},
testPacketDispatcher, testPacketDispatcher,
&proxy.InboundHandlerMeta{ &proxy.InboundHandlerMeta{
Address: v2net.LocalHostIP, Address: v2net.LocalHostIP,

View File

@ -14,7 +14,7 @@ type Config struct {
Accounts map[string]string Accounts map[string]string
Address v2net.Address Address v2net.Address
UDPEnabled bool UDPEnabled bool
Timeout int Timeout uint32
} }
func (this *Config) HasAccount(username, password string) bool { func (this *Config) HasAccount(username, password string) bool {

View File

@ -23,7 +23,7 @@ func (this *Config) UnmarshalJSON(data []byte) error {
Accounts []*Account `json:"accounts"` Accounts []*Account `json:"accounts"`
UDP bool `json:"udp"` UDP bool `json:"udp"`
Host *v2net.AddressJson `json:"ip"` Host *v2net.AddressJson `json:"ip"`
Timeout int `json:"timeout"` Timeout uint32 `json:"timeout"`
} }
rawConfig := new(SocksConfig) rawConfig := new(SocksConfig)