Skip to content

Commit

Permalink
try protobuf
Browse files Browse the repository at this point in the history
  • Loading branch information
DarienRaymond committed Aug 25, 2016
1 parent 04d9564 commit bbca180
Show file tree
Hide file tree
Showing 20 changed files with 216 additions and 51 deletions.
5 changes: 5 additions & 0 deletions app/router/proto/config.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
syntax = "proto3";

message Config {

}
14 changes: 7 additions & 7 deletions common/net/timed_io.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ var (
)

type TimeOutReader struct {
timeout int
timeout uint32
connection net.Conn
worker io.Reader
}

func NewTimeOutReader(timeout int /* seconds */, connection net.Conn) *TimeOutReader {
func NewTimeOutReader(timeout uint32 /* seconds */, connection net.Conn) *TimeOutReader {
reader := &TimeOutReader{
connection: connection,
timeout: -100,
timeout: 0,
}
reader.SetTimeOut(timeout)
return reader
Expand All @@ -29,12 +29,12 @@ func (reader *TimeOutReader) Read(p []byte) (int, error) {
return reader.worker.Read(p)
}

func (reader *TimeOutReader) GetTimeOut() int {
func (reader *TimeOutReader) GetTimeOut() uint32 {
return reader.timeout
}

func (reader *TimeOutReader) SetTimeOut(value int) {
if value == reader.timeout {
func (reader *TimeOutReader) SetTimeOut(value uint32) {
if reader.worker != nil && value == reader.timeout {
return
}
reader.timeout = value
Expand All @@ -56,7 +56,7 @@ func (reader *TimeOutReader) Release() {
}

type timedReaderWorker struct {
timeout int
timeout uint32
connection net.Conn
}

Expand Down
6 changes: 3 additions & 3 deletions common/net/timed_io_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ func TestTimeOutSettings(t *testing.T) {
assert := assert.On(t)

reader := NewTimeOutReader(8, nil)
assert.Int(reader.GetTimeOut()).Equals(8)
assert.Uint32(reader.GetTimeOut()).Equals(8)
reader.SetTimeOut(8) // no op
assert.Int(reader.GetTimeOut()).Equals(8)
assert.Uint32(reader.GetTimeOut()).Equals(8)
reader.SetTimeOut(9)
assert.Int(reader.GetTimeOut()).Equals(9)
assert.Uint32(reader.GetTimeOut()).Equals(9)
}
2 changes: 1 addition & 1 deletion common/protocol/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func NewUser(level UserLevel, email string) *User {
}

type UserSettings struct {
PayloadReadTimeout int
PayloadReadTimeout uint32
}

func GetUserSettings(level UserLevel) UserSettings {
Expand Down
2 changes: 1 addition & 1 deletion proxy/dokodemo/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ type Config struct {
Address v2net.Address
Port v2net.Port
Network *v2net.NetworkList
Timeout int
Timeout uint32
}
2 changes: 1 addition & 1 deletion proxy/dokodemo/config_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (this *Config) UnmarshalJSON(data []byte) error {
Host *v2net.AddressJson `json:"address"`
PortValue v2net.Port `json:"port"`
NetworkList *v2net.NetworkList `json:"network"`
TimeoutValue int `json:"timeout"`
TimeoutValue uint32 `json:"timeout"`
Redirect bool `json:"followRedirect"`
}
rawConfig := new(DokodemoConfig)
Expand Down
14 changes: 2 additions & 12 deletions proxy/freedom/config.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
package freedom

type DomainStrategy int
//go:generate protoc --go_out=. config.proto

const (
DomainStrategyAsIs = DomainStrategy(0)
DomainStrategyUseIP = DomainStrategy(1)
)

type Config struct {
DomainStrategy DomainStrategy
Timeout uint32
}
package freedom
82 changes: 82 additions & 0 deletions proxy/freedom/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 proxy/freedom/config.proto
Original file line number Diff line number Diff line change
@@ -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;
}
6 changes: 3 additions & 3 deletions proxy/freedom/config_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ func (this *Config) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, jsonConfig); err != nil {
return errors.New("Freedom: Failed to parse config: " + err.Error())
}
this.DomainStrategy = DomainStrategyAsIs
this.DomainStrategy = Config_AS_IS
domainStrategy := strings.ToLower(jsonConfig.DomainStrategy)
if domainStrategy == "useip" {
this.DomainStrategy = DomainStrategyUseIP
if domainStrategy == "useip" || domainStrategy == "use_ip" {
this.DomainStrategy = Config_USE_IP
}
this.Timeout = jsonConfig.Timeout
return nil
Expand Down
8 changes: 4 additions & 4 deletions proxy/freedom/freedom.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

type FreedomConnection struct {
domainStrategy DomainStrategy
domainStrategy Config_DomainStrategy
timeout uint32
dns dns.Server
meta *proxy.OutboundHandlerMeta
Expand All @@ -32,7 +32,7 @@ func NewFreedomConnection(config *Config, space app.Space, meta *proxy.OutboundH
meta: meta,
}
space.InitializeApplication(func() error {
if config.DomainStrategy == DomainStrategyUseIP {
if config.DomainStrategy == Config_USE_IP {
if !space.HasApp(dns.APP_ID) {
log.Error("Freedom: DNS server is not found in the space.")
return app.ErrMissingApplication
Expand Down Expand Up @@ -75,7 +75,7 @@ func (this *FreedomConnection) Dispatch(destination v2net.Destination, payload *
defer ray.OutboundOutput().Close()

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)
}
err := retry.Timed(5, 100).On(func() error {
Expand Down Expand Up @@ -116,7 +116,7 @@ func (this *FreedomConnection) Dispatch(destination v2net.Destination, payload *
timeout = 16
}
if timeout > 0 {
reader = v2net.NewTimeOutReader(int(timeout) /* seconds */, conn)
reader = v2net.NewTimeOutReader(timeout /* seconds */, conn)
}

v2reader := v2io.NewAdaptiveReader(reader)
Expand Down
2 changes: 1 addition & 1 deletion proxy/freedom/freedom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func TestIPResolution(t *testing.T) {
space.BindApp(dns.APP_ID, dnsServer)

freedom := NewFreedomConnection(
&Config{DomainStrategy: DomainStrategyUseIP},
&Config{DomainStrategy: Config_USE_IP},
space,
&proxy.OutboundHandlerMeta{
Address: v2net.AnyIP,
Expand Down
11 changes: 2 additions & 9 deletions proxy/http/config.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
package http

// Config for HTTP proxy server.
type Config struct {
Timeout int
}
//go:generate protoc --go_out=. config.proto

// ClientConfig for HTTP proxy client.
type ClientConfig struct {
}
package http
68 changes: 68 additions & 0 deletions proxy/http/config.pb.go

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

14 changes: 14 additions & 0 deletions proxy/http/config.proto
Original file line number Diff line number Diff line change
@@ -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 {

}
6 changes: 3 additions & 3 deletions proxy/http/config_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
)

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

func init() {
registry.RegisterInboundConfig("http", func() interface{} { return new(Config) })
registry.RegisterInboundConfig("http", func() interface{} { return new(ServerConfig) })
}
Loading

0 comments on commit bbca180

Please sign in to comment.