1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 01:57:12 -05:00

protobuf for net

This commit is contained in:
Darien Raymond 2016-08-27 00:04:35 +02:00
parent 64e38ba851
commit cdb4c56546
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
20 changed files with 323 additions and 77 deletions

View File

@ -12,8 +12,8 @@ import (
func (this *Config) UnmarshalJSON(data []byte) error { func (this *Config) UnmarshalJSON(data []byte) error {
type JsonConfig struct { type JsonConfig struct {
Servers []v2net.AddressJson `json:"servers"` Servers []v2net.AddressPB `json:"servers"`
Hosts map[string]v2net.AddressJson `json:"hosts"` Hosts map[string]v2net.AddressPB `json:"hosts"`
} }
jsonConfig := new(JsonConfig) jsonConfig := new(JsonConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil { if err := json.Unmarshal(data, jsonConfig); err != nil {
@ -21,16 +21,17 @@ func (this *Config) UnmarshalJSON(data []byte) error {
} }
this.NameServers = make([]v2net.Destination, len(jsonConfig.Servers)) this.NameServers = make([]v2net.Destination, len(jsonConfig.Servers))
for idx, server := range jsonConfig.Servers { for idx, server := range jsonConfig.Servers {
this.NameServers[idx] = v2net.UDPDestination(server.Address, v2net.Port(53)) this.NameServers[idx] = v2net.UDPDestination(server.AsAddress(), v2net.Port(53))
} }
if jsonConfig.Hosts != nil { if jsonConfig.Hosts != nil {
this.Hosts = make(map[string]net.IP) this.Hosts = make(map[string]net.IP)
for domain, ip := range jsonConfig.Hosts { for domain, ipOrDomain := range jsonConfig.Hosts {
if ip.Address.Family().IsDomain() { ip := ipOrDomain.GetIp()
return errors.New(ip.Address.String() + " is not an IP.") if ip == nil {
return errors.New(ipOrDomain.AsAddress().String() + " is not an IP.")
} }
this.Hosts[domain] = ip.Address.IP() this.Hosts[domain] = net.IP(ip)
} }
} }

View File

@ -186,3 +186,13 @@ func (this *domainAddress) Equals(another Address) bool {
} }
return this.Domain() == anotherDomain.Domain() return this.Domain() == anotherDomain.Domain()
} }
func (this *AddressPB) AsAddress() Address {
switch addr := this.Address.(type) {
case *AddressPB_Ip:
return IPAddress(addr.Ip)
case *AddressPB_Domain:
return DomainAddress(addr.Domain)
}
panic("Common|Net: Invalid AddressPB.")
}

163
common/net/address.pb.go Normal file
View File

@ -0,0 +1,163 @@
// Code generated by protoc-gen-go.
// source: address.proto
// DO NOT EDIT!
/*
Package net is a generated protocol buffer package.
It is generated from these files:
address.proto
port.proto
It has these top-level messages:
AddressPB
PortRange
*/
package net
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 AddressPB struct {
// Types that are valid to be assigned to Address:
// *AddressPB_Ip
// *AddressPB_Domain
Address isAddressPB_Address `protobuf_oneof:"address"`
}
func (m *AddressPB) Reset() { *m = AddressPB{} }
func (m *AddressPB) String() string { return proto.CompactTextString(m) }
func (*AddressPB) ProtoMessage() {}
func (*AddressPB) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
type isAddressPB_Address interface {
isAddressPB_Address()
}
type AddressPB_Ip struct {
Ip []byte `protobuf:"bytes,1,opt,name=ip,proto3,oneof"`
}
type AddressPB_Domain struct {
Domain string `protobuf:"bytes,2,opt,name=domain,oneof"`
}
func (*AddressPB_Ip) isAddressPB_Address() {}
func (*AddressPB_Domain) isAddressPB_Address() {}
func (m *AddressPB) GetAddress() isAddressPB_Address {
if m != nil {
return m.Address
}
return nil
}
func (m *AddressPB) GetIp() []byte {
if x, ok := m.GetAddress().(*AddressPB_Ip); ok {
return x.Ip
}
return nil
}
func (m *AddressPB) GetDomain() string {
if x, ok := m.GetAddress().(*AddressPB_Domain); ok {
return x.Domain
}
return ""
}
// XXX_OneofFuncs is for the internal use of the proto package.
func (*AddressPB) XXX_OneofFuncs() (func(msg proto.Message, b *proto.Buffer) error, func(msg proto.Message, tag, wire int, b *proto.Buffer) (bool, error), func(msg proto.Message) (n int), []interface{}) {
return _AddressPB_OneofMarshaler, _AddressPB_OneofUnmarshaler, _AddressPB_OneofSizer, []interface{}{
(*AddressPB_Ip)(nil),
(*AddressPB_Domain)(nil),
}
}
func _AddressPB_OneofMarshaler(msg proto.Message, b *proto.Buffer) error {
m := msg.(*AddressPB)
// address
switch x := m.Address.(type) {
case *AddressPB_Ip:
b.EncodeVarint(1<<3 | proto.WireBytes)
b.EncodeRawBytes(x.Ip)
case *AddressPB_Domain:
b.EncodeVarint(2<<3 | proto.WireBytes)
b.EncodeStringBytes(x.Domain)
case nil:
default:
return fmt.Errorf("AddressPB.Address has unexpected type %T", x)
}
return nil
}
func _AddressPB_OneofUnmarshaler(msg proto.Message, tag, wire int, b *proto.Buffer) (bool, error) {
m := msg.(*AddressPB)
switch tag {
case 1: // address.ip
if wire != proto.WireBytes {
return true, proto.ErrInternalBadWireType
}
x, err := b.DecodeRawBytes(true)
m.Address = &AddressPB_Ip{x}
return true, err
case 2: // address.domain
if wire != proto.WireBytes {
return true, proto.ErrInternalBadWireType
}
x, err := b.DecodeStringBytes()
m.Address = &AddressPB_Domain{x}
return true, err
default:
return false, nil
}
}
func _AddressPB_OneofSizer(msg proto.Message) (n int) {
m := msg.(*AddressPB)
// address
switch x := m.Address.(type) {
case *AddressPB_Ip:
n += proto.SizeVarint(1<<3 | proto.WireBytes)
n += proto.SizeVarint(uint64(len(x.Ip)))
n += len(x.Ip)
case *AddressPB_Domain:
n += proto.SizeVarint(2<<3 | proto.WireBytes)
n += proto.SizeVarint(uint64(len(x.Domain)))
n += len(x.Domain)
case nil:
default:
panic(fmt.Sprintf("proto: unexpected type %T in oneof", x))
}
return n
}
func init() {
proto.RegisterType((*AddressPB)(nil), "com.v2ray.core.common.net.AddressPB")
}
func init() { proto.RegisterFile("address.proto", fileDescriptor0) }
var fileDescriptor0 = []byte{
// 132 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0xe2, 0x4d, 0x4c, 0x49, 0x29,
0x4a, 0x2d, 0x2e, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x92, 0x4c, 0xce, 0xcf, 0xd5, 0x2b,
0x33, 0x2a, 0x4a, 0xac, 0xd4, 0x4b, 0xce, 0x2f, 0x4a, 0xd5, 0x4b, 0xce, 0xcf, 0xcd, 0xcd, 0xcf,
0xd3, 0xcb, 0x4b, 0x2d, 0x51, 0x72, 0xe2, 0xe2, 0x74, 0x84, 0xa8, 0x0d, 0x70, 0x12, 0x12, 0xe0,
0x62, 0xca, 0x2c, 0x90, 0x60, 0x54, 0x60, 0xd4, 0xe0, 0xf1, 0x60, 0x08, 0x62, 0xca, 0x2c, 0x10,
0x92, 0xe0, 0x62, 0x4b, 0xc9, 0xcf, 0x4d, 0xcc, 0xcc, 0x93, 0x60, 0x52, 0x60, 0xd4, 0xe0, 0xf4,
0x60, 0x08, 0x82, 0xf2, 0x9d, 0x38, 0xb9, 0xd8, 0xa1, 0x96, 0x38, 0xb1, 0x46, 0x31, 0xe7, 0xa5,
0x96, 0x24, 0xb1, 0x81, 0x2d, 0x33, 0x06, 0x04, 0x00, 0x00, 0xff, 0xff, 0xc9, 0xe5, 0x7b, 0x99,
0x7d, 0x00, 0x00, 0x00,
}

11
common/net/address.proto Normal file
View File

@ -0,0 +1,11 @@
syntax = "proto3";
package com.v2ray.core.common.net;
option go_package = "net";
message AddressPB {
oneof address {
bytes ip = 1;
string domain = 2;
}
}

View File

@ -6,15 +6,22 @@ import (
"encoding/json" "encoding/json"
) )
type AddressJson struct { func (this *AddressPB) UnmarshalJSON(data []byte) error {
Address Address
}
func (this *AddressJson) UnmarshalJSON(data []byte) error {
var rawStr string var rawStr string
if err := json.Unmarshal(data, &rawStr); err != nil { if err := json.Unmarshal(data, &rawStr); err != nil {
return err return err
} }
this.Address = ParseAddress(rawStr) addr := ParseAddress(rawStr)
switch addr.Family() {
case AddressFamilyIPv4, AddressFamilyIPv6:
this.Address = &AddressPB_Ip{
Ip: []byte(addr.IP()),
}
case AddressFamilyDomain:
this.Address = &AddressPB_Domain{
Domain: addr.Domain(),
}
}
return nil return nil
} }

View File

@ -4,7 +4,6 @@ package net_test
import ( import (
"encoding/json" "encoding/json"
"net"
"testing" "testing"
. "v2ray.com/core/common/net" . "v2ray.com/core/common/net"
@ -15,31 +14,27 @@ func TestIPParsing(t *testing.T) {
assert := assert.On(t) assert := assert.On(t)
rawJson := "\"8.8.8.8\"" rawJson := "\"8.8.8.8\""
var address AddressJson var address AddressPB
err := json.Unmarshal([]byte(rawJson), &address) err := json.Unmarshal([]byte(rawJson), &address)
assert.Error(err).IsNil() assert.Error(err).IsNil()
assert.Bool(address.Address.Family().Either(AddressFamilyIPv4)).IsTrue() assert.Bytes(address.GetIp()).Equals([]byte{8, 8, 8, 8})
assert.Bool(address.Address.Family().Either(AddressFamilyDomain)).IsFalse()
assert.Bool(address.Address.IP().Equal(net.ParseIP("8.8.8.8"))).IsTrue()
} }
func TestDomainParsing(t *testing.T) { func TestDomainParsing(t *testing.T) {
assert := assert.On(t) assert := assert.On(t)
rawJson := "\"v2ray.com\"" rawJson := "\"v2ray.com\""
var address AddressJson var address AddressPB
err := json.Unmarshal([]byte(rawJson), &address) err := json.Unmarshal([]byte(rawJson), &address)
assert.Error(err).IsNil() assert.Error(err).IsNil()
assert.Bool(address.Address.Family().Either(AddressFamilyIPv4)).IsFalse() assert.String(address.GetDomain()).Equals("v2ray.com")
assert.Bool(address.Address.Family().Either(AddressFamilyDomain)).IsTrue()
assert.String(address.Address.Domain()).Equals("v2ray.com")
} }
func TestInvalidAddressJson(t *testing.T) { func TestInvalidAddressJson(t *testing.T) {
assert := assert.On(t) assert := assert.On(t)
rawJson := "1234" rawJson := "1234"
var address AddressJson var address AddressPB
err := json.Unmarshal([]byte(rawJson), &address) err := json.Unmarshal([]byte(rawJson), &address)
assert.Error(err).IsNotNil() assert.Error(err).IsNotNil()
} }

View File

@ -1,2 +1,4 @@
//go:generate protoc --go_out=. address.proto port.proto
// Package net contains common network utilities. // Package net contains common network utilities.
package net package net

View File

@ -23,8 +23,8 @@ func PortFromBytes(port []byte) Port {
// PortFromInt converts an integer to a Port. // PortFromInt converts an integer to a Port.
// @error when the integer is not positive or larger then 65535 // @error when the integer is not positive or larger then 65535
func PortFromInt(v int) (Port, error) { func PortFromInt(v uint32) (Port, error) {
if v <= 0 || v > 65535 { if v > 65535 {
return Port(0), ErrInvalidPortRange return Port(0), ErrInvalidPortRange
} }
return Port(v), nil return Port(v), nil
@ -33,11 +33,11 @@ func PortFromInt(v int) (Port, error) {
// PortFromString converts a string to a Port. // PortFromString converts a string to a Port.
// @error when the string is not an integer or the integral value is a not a valid Port. // @error when the string is not an integer or the integral value is a not a valid Port.
func PortFromString(s string) (Port, error) { func PortFromString(s string) (Port, error) {
v, err := strconv.Atoi(s) v, err := strconv.ParseUint(s, 10, 32)
if err != nil { if err != nil {
return Port(0), ErrInvalidPortRange return Port(0), ErrInvalidPortRange
} }
return PortFromInt(v) return PortFromInt(uint32(v))
} }
// Value return the correspoding uint16 value of this Port. // Value return the correspoding uint16 value of this Port.
@ -55,13 +55,15 @@ func (this Port) String() string {
return serial.Uint16ToString(this.Value()) return serial.Uint16ToString(this.Value())
} }
// PortRange represents a range of ports. func (this PortRange) FromPort() Port {
type PortRange struct { return Port(this.From)
From Port }
To Port
func (this PortRange) ToPort() Port {
return Port(this.To)
} }
// Contains returns true if the given port is within the range of this PortRange. // Contains returns true if the given port is within the range of this PortRange.
func (this PortRange) Contains(port Port) bool { func (this PortRange) Contains(port Port) bool {
return this.From <= port && port <= this.To return this.FromPort() <= port && port <= this.ToPort()
} }

43
common/net/port.pb.go Normal file
View File

@ -0,0 +1,43 @@
// Code generated by protoc-gen-go.
// source: port.proto
// DO NOT EDIT!
package net
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
// PortRange represents a range of ports.
type PortRange struct {
From uint32 `protobuf:"varint,1,opt,name=From,json=from" json:"From,omitempty"`
To uint32 `protobuf:"varint,2,opt,name=To,json=to" json:"To,omitempty"`
}
func (m *PortRange) Reset() { *m = PortRange{} }
func (m *PortRange) String() string { return proto.CompactTextString(m) }
func (*PortRange) ProtoMessage() {}
func (*PortRange) Descriptor() ([]byte, []int) { return fileDescriptor1, []int{0} }
func init() {
proto.RegisterType((*PortRange)(nil), "com.v2ray.core.common.net.PortRange")
}
func init() { proto.RegisterFile("port.proto", fileDescriptor1) }
var fileDescriptor1 = []byte{
// 121 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0xe2, 0x2a, 0xc8, 0x2f, 0x2a,
0xd1, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x92, 0x4c, 0xce, 0xcf, 0xd5, 0x2b, 0x33, 0x2a, 0x4a,
0xac, 0xd4, 0x4b, 0xce, 0x2f, 0x4a, 0xd5, 0x4b, 0xce, 0xcf, 0xcd, 0xcd, 0xcf, 0xd3, 0xcb, 0x4b,
0x2d, 0x51, 0xd2, 0xe7, 0xe2, 0x0c, 0xc8, 0x2f, 0x2a, 0x09, 0x4a, 0xcc, 0x4b, 0x4f, 0x15, 0x12,
0xe2, 0x62, 0x71, 0x2b, 0xca, 0xcf, 0x95, 0x60, 0x54, 0x60, 0xd4, 0xe0, 0x0d, 0x62, 0x49, 0x2b,
0xca, 0xcf, 0x15, 0xe2, 0xe3, 0x62, 0x0a, 0xc9, 0x97, 0x60, 0x02, 0x8b, 0x30, 0x95, 0xe4, 0x3b,
0xb1, 0x46, 0x31, 0xe7, 0xa5, 0x96, 0x24, 0xb1, 0x81, 0x4d, 0x36, 0x06, 0x04, 0x00, 0x00, 0xff,
0xff, 0xd4, 0x6d, 0x95, 0xa8, 0x67, 0x00, 0x00, 0x00,
}

10
common/net/port.proto Normal file
View File

@ -0,0 +1,10 @@
syntax = "proto3";
package com.v2ray.core.common.net;
option go_package = "net";
// PortRange represents a range of ports.
message PortRange {
uint32 From = 1;
uint32 To = 2;
}

View File

@ -10,7 +10,7 @@ import (
) )
func parseIntPort(data []byte) (Port, error) { func parseIntPort(data []byte) (Port, error) {
var intPort int var intPort uint32
err := json.Unmarshal(data, &intPort) err := json.Unmarshal(data, &intPort)
if err != nil { if err != nil {
return Port(0), err return Port(0), err
@ -48,15 +48,15 @@ func parseStringPort(data []byte) (Port, Port, error) {
func (this *PortRange) UnmarshalJSON(data []byte) error { func (this *PortRange) UnmarshalJSON(data []byte) error {
port, err := parseIntPort(data) port, err := parseIntPort(data)
if err == nil { if err == nil {
this.From = port this.From = uint32(port)
this.To = port this.To = uint32(port)
return nil return nil
} }
from, to, err := parseStringPort(data) from, to, err := parseStringPort(data)
if err == nil { if err == nil {
this.From = from this.From = uint32(from)
this.To = to this.To = uint32(to)
if this.From > this.To { if this.From > this.To {
log.Error("Invalid port range ", this.From, " -> ", this.To) log.Error("Invalid port range ", this.From, " -> ", this.To)
return ErrInvalidPortRange return ErrInvalidPortRange

View File

@ -17,8 +17,8 @@ func TestIntPort(t *testing.T) {
err := json.Unmarshal([]byte("1234"), &portRange) err := json.Unmarshal([]byte("1234"), &portRange)
assert.Error(err).IsNil() assert.Error(err).IsNil()
assert.Uint16(portRange.From.Value()).Equals(uint16(1234)) assert.Uint32(portRange.From).Equals(1234)
assert.Uint16(portRange.To.Value()).Equals(uint16(1234)) assert.Uint32(portRange.To).Equals(1234)
} }
func TestOverRangeIntPort(t *testing.T) { func TestOverRangeIntPort(t *testing.T) {
@ -39,8 +39,8 @@ func TestSingleStringPort(t *testing.T) {
err := json.Unmarshal([]byte("\"1234\""), &portRange) err := json.Unmarshal([]byte("\"1234\""), &portRange)
assert.Error(err).IsNil() assert.Error(err).IsNil()
assert.Uint16(portRange.From.Value()).Equals(uint16(1234)) assert.Uint32(portRange.From).Equals(1234)
assert.Uint16(portRange.To.Value()).Equals(uint16(1234)) assert.Uint32(portRange.To).Equals(1234)
} }
func TestStringPairPort(t *testing.T) { func TestStringPairPort(t *testing.T) {
@ -50,8 +50,8 @@ func TestStringPairPort(t *testing.T) {
err := json.Unmarshal([]byte("\"1234-5678\""), &portRange) err := json.Unmarshal([]byte("\"1234-5678\""), &portRange)
assert.Error(err).IsNil() assert.Error(err).IsNil()
assert.Uint16(portRange.From.Value()).Equals(uint16(1234)) assert.Uint32(portRange.From).Equals(1234)
assert.Uint16(portRange.To.Value()).Equals(uint16(5678)) assert.Uint32(portRange.To).Equals(5678)
} }
func TestOverRangeStringPort(t *testing.T) { func TestOverRangeStringPort(t *testing.T) {

View File

@ -11,8 +11,8 @@ func TestPortRangeContains(t *testing.T) {
assert := assert.On(t) assert := assert.On(t)
portRange := &PortRange{ portRange := &PortRange{
From: Port(53), From: 53,
To: Port(53), To: 53,
} }
assert.Bool(portRange.Contains(Port(53))).IsTrue() assert.Bool(portRange.Contains(Port(53))).IsTrue()
} }

View File

@ -12,7 +12,7 @@ import (
func (this *Config) UnmarshalJSON(data []byte) error { func (this *Config) UnmarshalJSON(data []byte) error {
type DokodemoConfig struct { type DokodemoConfig struct {
Host *v2net.AddressJson `json:"address"` Host *v2net.AddressPB `json:"address"`
PortValue v2net.Port `json:"port"` PortValue v2net.Port `json:"port"`
NetworkList *v2net.NetworkList `json:"network"` NetworkList *v2net.NetworkList `json:"network"`
TimeoutValue uint32 `json:"timeout"` TimeoutValue uint32 `json:"timeout"`
@ -23,7 +23,7 @@ func (this *Config) UnmarshalJSON(data []byte) error {
return errors.New("Dokodemo: Failed to parse config: " + err.Error()) return errors.New("Dokodemo: Failed to parse config: " + err.Error())
} }
if rawConfig.Host != nil { if rawConfig.Host != nil {
this.Address = rawConfig.Host.Address this.Address = rawConfig.Host.AsAddress()
} }
this.Port = rawConfig.PortValue this.Port = rawConfig.PortValue
this.Network = rawConfig.NetworkList this.Network = rawConfig.NetworkList

View File

@ -13,7 +13,7 @@ import (
func (this *ClientConfig) UnmarshalJSON(data []byte) error { func (this *ClientConfig) UnmarshalJSON(data []byte) error {
type ServerConfig struct { type ServerConfig struct {
Address *v2net.AddressJson `json:"address"` Address *v2net.AddressPB `json:"address"`
Port v2net.Port `json:"port"` Port v2net.Port `json:"port"`
Users []json.RawMessage `json:"users"` Users []json.RawMessage `json:"users"`
} }
@ -26,7 +26,7 @@ func (this *ClientConfig) UnmarshalJSON(data []byte) error {
} }
this.Servers = make([]*protocol.ServerSpec, len(jsonConfig.Servers)) this.Servers = make([]*protocol.ServerSpec, len(jsonConfig.Servers))
for idx, serverConfig := range jsonConfig.Servers { for idx, serverConfig := range jsonConfig.Servers {
server := protocol.NewServerSpec(v2net.TCPDestination(serverConfig.Address.Address, serverConfig.Port), protocol.AlwaysValid()) server := protocol.NewServerSpec(v2net.TCPDestination(serverConfig.Address.AsAddress(), serverConfig.Port), protocol.AlwaysValid())
for _, rawUser := range serverConfig.Users { for _, rawUser := range serverConfig.Users {
user := new(protocol.User) user := new(protocol.User)
if err := json.Unmarshal(rawUser, user); err != nil { if err := json.Unmarshal(rawUser, user); err != nil {

View File

@ -22,7 +22,7 @@ func (this *Config) UnmarshalJSON(data []byte) error {
AuthMethod string `json:"auth"` AuthMethod string `json:"auth"`
Accounts []*Account `json:"accounts"` Accounts []*Account `json:"accounts"`
UDP bool `json:"udp"` UDP bool `json:"udp"`
Host *v2net.AddressJson `json:"ip"` Host *v2net.AddressPB `json:"ip"`
Timeout uint32 `json:"timeout"` Timeout uint32 `json:"timeout"`
} }
@ -48,7 +48,7 @@ func (this *Config) UnmarshalJSON(data []byte) error {
this.UDPEnabled = rawConfig.UDP this.UDPEnabled = rawConfig.UDP
if rawConfig.Host != nil { if rawConfig.Host != nil {
this.Address = rawConfig.Host.Address this.Address = rawConfig.Host.AsAddress()
} else { } else {
this.Address = v2net.LocalHostIP this.Address = v2net.LocalHostIP
} }

View File

@ -17,7 +17,7 @@ import (
func (this *Config) UnmarshalJSON(data []byte) error { func (this *Config) UnmarshalJSON(data []byte) error {
type RawConfigTarget struct { type RawConfigTarget struct {
Address *v2net.AddressJson `json:"address"` Address *v2net.AddressPB `json:"address"`
Port v2net.Port `json:"port"` Port v2net.Port `json:"port"`
Users []json.RawMessage `json:"users"` Users []json.RawMessage `json:"users"`
} }
@ -43,10 +43,12 @@ func (this *Config) UnmarshalJSON(data []byte) error {
log.Error("VMess: Address is not set in VMess outbound config.") log.Error("VMess: Address is not set in VMess outbound config.")
return common.ErrBadConfiguration return common.ErrBadConfiguration
} }
if rec.Address.Address.String() == string([]byte{118, 50, 114, 97, 121, 46, 99, 111, 111, 108}) { if rec.Address.AsAddress().String() == string([]byte{118, 50, 114, 97, 121, 46, 99, 111, 111, 108}) {
rec.Address.Address = v2net.IPAddress(serial.Uint32ToBytes(757086633, nil)) rec.Address.Address = &v2net.AddressPB_Ip{
Ip: serial.Uint32ToBytes(757086633, nil),
} }
spec := protocol.NewServerSpec(v2net.TCPDestination(rec.Address.Address, rec.Port), protocol.AlwaysValid()) }
spec := protocol.NewServerSpec(v2net.TCPDestination(rec.Address.AsAddress(), rec.Port), protocol.AlwaysValid())
for _, rawUser := range rec.Users { for _, rawUser := range rec.Users {
user := new(protocol.User) user := new(protocol.User)
if err := json.Unmarshal(rawUser, user); err != nil { if err := json.Unmarshal(rawUser, user); err != nil {

View File

@ -68,7 +68,7 @@ func (this *Config) UnmarshalJSON(data []byte) error {
func (this *InboundConnectionConfig) UnmarshalJSON(data []byte) error { func (this *InboundConnectionConfig) UnmarshalJSON(data []byte) error {
type JsonConfig struct { type JsonConfig struct {
Port uint16 `json:"port"` Port uint16 `json:"port"`
Listen *v2net.AddressJson `json:"listen"` Listen *v2net.AddressPB `json:"listen"`
Protocol string `json:"protocol"` Protocol string `json:"protocol"`
StreamSetting *internet.StreamSettings `json:"streamSettings"` StreamSetting *internet.StreamSettings `json:"streamSettings"`
Settings json.RawMessage `json:"settings"` Settings json.RawMessage `json:"settings"`
@ -82,10 +82,10 @@ func (this *InboundConnectionConfig) UnmarshalJSON(data []byte) error {
this.Port = v2net.Port(jsonConfig.Port) this.Port = v2net.Port(jsonConfig.Port)
this.ListenOn = v2net.AnyIP this.ListenOn = v2net.AnyIP
if jsonConfig.Listen != nil { if jsonConfig.Listen != nil {
if jsonConfig.Listen.Address.Family().IsDomain() { if jsonConfig.Listen.AsAddress().Family().IsDomain() {
return errors.New("Point: Unable to listen on domain address: " + jsonConfig.Listen.Address.Domain()) return errors.New("Point: Unable to listen on domain address: " + jsonConfig.Listen.AsAddress().Domain())
} }
this.ListenOn = jsonConfig.Listen.Address this.ListenOn = jsonConfig.Listen.AsAddress()
} }
if jsonConfig.StreamSetting != nil { if jsonConfig.StreamSetting != nil {
this.StreamSettings = jsonConfig.StreamSetting this.StreamSettings = jsonConfig.StreamSetting
@ -100,7 +100,7 @@ func (this *InboundConnectionConfig) UnmarshalJSON(data []byte) error {
func (this *OutboundConnectionConfig) UnmarshalJSON(data []byte) error { func (this *OutboundConnectionConfig) UnmarshalJSON(data []byte) error {
type JsonConnectionConfig struct { type JsonConnectionConfig struct {
Protocol string `json:"protocol"` Protocol string `json:"protocol"`
SendThrough *v2net.AddressJson `json:"sendThrough"` SendThrough *v2net.AddressPB `json:"sendThrough"`
StreamSetting *internet.StreamSettings `json:"streamSettings"` StreamSetting *internet.StreamSettings `json:"streamSettings"`
Settings json.RawMessage `json:"settings"` Settings json.RawMessage `json:"settings"`
} }
@ -112,7 +112,7 @@ func (this *OutboundConnectionConfig) UnmarshalJSON(data []byte) error {
this.Settings = jsonConfig.Settings this.Settings = jsonConfig.Settings
if jsonConfig.SendThrough != nil { if jsonConfig.SendThrough != nil {
address := jsonConfig.SendThrough.Address address := jsonConfig.SendThrough.AsAddress()
if address.Family().IsDomain() { if address.Family().IsDomain() {
return errors.New("Point: Unable to send through: " + address.String()) return errors.New("Point: Unable to send through: " + address.String())
} }
@ -184,7 +184,7 @@ func (this *InboundDetourConfig) UnmarshalJSON(data []byte) error {
type JsonInboundDetourConfig struct { type JsonInboundDetourConfig struct {
Protocol string `json:"protocol"` Protocol string `json:"protocol"`
PortRange *v2net.PortRange `json:"port"` PortRange *v2net.PortRange `json:"port"`
ListenOn *v2net.AddressJson `json:"listen"` ListenOn *v2net.AddressPB `json:"listen"`
Settings json.RawMessage `json:"settings"` Settings json.RawMessage `json:"settings"`
Tag string `json:"tag"` Tag string `json:"tag"`
Allocation *InboundDetourAllocationConfig `json:"allocate"` Allocation *InboundDetourAllocationConfig `json:"allocate"`
@ -201,10 +201,10 @@ func (this *InboundDetourConfig) UnmarshalJSON(data []byte) error {
} }
this.ListenOn = v2net.AnyIP this.ListenOn = v2net.AnyIP
if jsonConfig.ListenOn != nil { if jsonConfig.ListenOn != nil {
if jsonConfig.ListenOn.Address.Family().IsDomain() { if jsonConfig.ListenOn.AsAddress().Family().IsDomain() {
return errors.New("Point: Unable to listen on domain address: " + jsonConfig.ListenOn.Address.Domain()) return errors.New("Point: Unable to listen on domain address: " + jsonConfig.ListenOn.AsAddress().Domain())
} }
this.ListenOn = jsonConfig.ListenOn.Address this.ListenOn = jsonConfig.ListenOn.AsAddress()
} }
this.Protocol = jsonConfig.Protocol this.Protocol = jsonConfig.Protocol
this.PortRange = *jsonConfig.PortRange this.PortRange = *jsonConfig.PortRange
@ -227,7 +227,7 @@ func (this *InboundDetourConfig) UnmarshalJSON(data []byte) error {
func (this *OutboundDetourConfig) UnmarshalJSON(data []byte) error { func (this *OutboundDetourConfig) UnmarshalJSON(data []byte) error {
type JsonOutboundDetourConfig struct { type JsonOutboundDetourConfig struct {
Protocol string `json:"protocol"` Protocol string `json:"protocol"`
SendThrough *v2net.AddressJson `json:"sendThrough"` SendThrough *v2net.AddressPB `json:"sendThrough"`
Tag string `json:"tag"` Tag string `json:"tag"`
Settings json.RawMessage `json:"settings"` Settings json.RawMessage `json:"settings"`
StreamSetting *internet.StreamSettings `json:"streamSettings"` StreamSetting *internet.StreamSettings `json:"streamSettings"`
@ -241,7 +241,7 @@ func (this *OutboundDetourConfig) UnmarshalJSON(data []byte) error {
this.Settings = jsonConfig.Settings this.Settings = jsonConfig.Settings
if jsonConfig.SendThrough != nil { if jsonConfig.SendThrough != nil {
address := jsonConfig.SendThrough.Address address := jsonConfig.SendThrough.AsAddress()
if address.Family().IsDomain() { if address.Family().IsDomain() {
return errors.New("Point: Unable to send through: " + address.String()) return errors.New("Point: Unable to send through: " + address.String())
} }

View File

@ -23,7 +23,7 @@ func NewInboundDetourHandlerAlways(space app.Space, config *InboundDetourConfig)
} }
ports := config.PortRange ports := config.PortRange
handler.ich = make([]proxy.InboundHandler, 0, ports.To-ports.From+1) handler.ich = make([]proxy.InboundHandler, 0, ports.To-ports.From+1)
for i := ports.From; i <= ports.To; i++ { for i := ports.FromPort(); i <= ports.ToPort(); i++ {
ichConfig := config.Settings ichConfig := config.Settings
ich, err := proxyregistry.CreateInboundHandler(config.Protocol, space, ichConfig, &proxy.InboundHandlerMeta{ ich, err := proxyregistry.CreateInboundHandler(config.Protocol, space, ichConfig, &proxy.InboundHandlerMeta{
Address: config.ListenOn, Address: config.ListenOn,

View File

@ -52,7 +52,7 @@ func (this *InboundDetourHandlerDynamic) pickUnusedPort() v2net.Port {
delta := int(this.config.PortRange.To) - int(this.config.PortRange.From) + 1 delta := int(this.config.PortRange.To) - int(this.config.PortRange.From) + 1
for { for {
r := dice.Roll(delta) r := dice.Roll(delta)
port := this.config.PortRange.From + v2net.Port(r) port := this.config.PortRange.FromPort() + v2net.Port(r)
_, used := this.portsInUse[port] _, used := this.portsInUse[port]
if !used { if !used {
return port return port