Compare commits

..

77 Commits

Author SHA1 Message Date
Darien Raymond
1d13f47f9c protobuf for stream settings 2016-10-02 23:43:58 +02:00
Darien Raymond
5ec948f690 integrate tls settings in ws 2016-09-30 16:53:40 +02:00
Darien Raymond
af6abfa3e3 update proto generated code 2016-09-26 15:14:39 +02:00
Darien Raymond
91f2a40dae refine proto package names 2016-09-26 15:14:16 +02:00
Darien Raymond
922e761bef rename server spec user 2016-09-25 22:57:27 +02:00
Darien Raymond
c5f09c64a2 refine socks config 2016-09-25 22:54:18 +02:00
Darien Raymond
c6a7389817 refine cipher settings 2016-09-25 22:19:49 +02:00
Darien Raymond
ce5bc72f0c refine shadowsocks proto 2016-09-25 22:07:32 +02:00
Darien Raymond
5e9017720e check nil 2016-09-24 23:36:26 +02:00
Darien Raymond
24e575f2cd protobuf for vmess 2016-09-24 23:11:58 +02:00
Darien Raymond
60f4292882 protobuf for dokodemo config 2016-09-22 16:49:20 +02:00
Darien Raymond
88ae8248a5 protobuf for network list 2016-09-22 12:14:50 +02:00
Darien Raymond
dc6b7e2e75 protobuf for blackhole config 2016-09-22 12:01:36 +02:00
Darien Raymond
6317768cab use golang 1.7.1 2016-09-21 21:08:22 +02:00
Darien Raymond
b28d718b79 optional field in kcp config 2016-09-21 21:08:05 +02:00
Darien Raymond
1a3f51ade7 protobuf for kcp config 2016-09-21 14:39:07 +02:00
Darien Raymond
8f6a972970 refactor config cache 2016-09-21 13:52:16 +02:00
Darien Raymond
d38e62932d proto for dns config 2016-09-20 16:05:35 +02:00
Darien Raymond
7de7588ec2 proto for destination 2016-09-20 11:53:05 +02:00
Darien Raymond
c518726910 network proto 2016-09-20 10:44:44 +02:00
Darien Raymond
fbdb1e09d7 remove incorrect check on config name 2016-09-19 23:54:48 +02:00
Darien Raymond
9ade07db03 remove IsTCP() and IsUDP() 2016-09-19 16:39:11 +02:00
Darien Raymond
5291624165 format log 2016-09-19 14:41:58 +02:00
Darien Raymond
5f5d28009f fix typo in utp def 2016-09-19 14:41:54 +02:00
Darien Raymond
28432cfd4c Update version 2016-09-19 11:52:48 +02:00
Darien Raymond
9ad96949b0 Update version 2016-09-19 11:23:03 +02:00
Darien Raymond
8b37edd02d format log 2016-09-18 00:46:16 +02:00
Darien Raymond
d08cba000f accounts in protobuf 2016-09-18 00:41:21 +02:00
Darien Raymond
3423adaea4 fix test break 2016-08-29 12:25:16 +02:00
Darien Raymond
7a31e7a0c2 fix test break 2016-08-29 12:23:17 +02:00
Darien Raymond
7f2ba0d106 remove unnecessary code 2016-08-28 23:47:39 +02:00
Darien Raymond
948f04921d update socks code for server config 2016-08-28 23:46:50 +02:00
Darien Raymond
ea2c491ade global proto gen 2016-08-28 23:41:51 +02:00
Darien Raymond
11e310298c proto for socks server 2016-08-28 23:41:45 +02:00
Darien Raymond
0f74c618de close input stream early 2016-08-28 12:01:37 +02:00
Darien Raymond
9b72d3cbd7 simplify socks transport 2016-08-28 11:59:03 +02:00
Darien Raymond
bd4ca7b37f remote protobuf code from coverage 2016-08-27 12:00:51 +02:00
Darien Raymond
cdb4c56546 protobuf for net 2016-08-27 00:04:35 +02:00
Darien Raymond
64e38ba851 update chinaip 2016-08-25 22:17:57 +02:00
Darien Raymond
bbca180dba try protobuf 2016-08-25 21:55:49 +02:00
Darien Raymond
04d956462c fix test without -json tag 2016-08-25 13:25:59 +02:00
Darien Raymond
3fd66ad795 remove PingNecessary() 2016-08-25 12:00:31 +02:00
Darien Raymond
e49fb2f50d no compacting buffers 2016-08-25 11:41:05 +02:00
Darien Raymond
59125f665b allow env speicified buffer size 2016-08-25 11:21:32 +02:00
Darien Raymond
fcad4aa212 adjust receiving buffer size 2016-08-25 09:45:56 +02:00
Darien Raymond
0da987ec43 refine buffer size 2016-08-25 00:04:54 +02:00
Darien Raymond
2ae8e5d033 rename buffer size 2016-08-24 23:54:39 +02:00
Darien Raymond
2839ce7a88 simplify receiving worker 2016-08-24 23:51:53 +02:00
Darien Raymond
5d20e3f70b remove sending queue 2016-08-24 15:47:14 +02:00
Darien Raymond
e6e0419958 format private annotation 2016-08-24 11:17:42 +02:00
Darien Raymond
a9da266bf9 update default in-fligh size 2016-08-24 07:50:33 +02:00
Darien Raymond
98025e1457 change default kcp.tti to 50 2016-08-23 23:32:36 +02:00
Darien Raymond
85c5171d8d update deploy key 2016-08-22 23:09:25 +02:00
Darien Raymond
e7bf0c2e49 fix install script 2016-08-21 18:02:12 +02:00
Darien Raymond
8eab746c96 doc 2016-08-21 09:06:15 +02:00
Darien Raymond
fdeb8b4f89 update file paths 2016-08-20 21:03:50 +02:00
v2ray
07a86f549b unified import path 2016-08-20 20:55:45 +02:00
v2ray
6a878432dc Update version 2016-08-20 20:40:24 +02:00
v2ray
d08ffedf48 reduce ping frequency 2016-08-20 11:33:22 +02:00
v2ray
0a34ec0be6 prepare for 2.0 2016-08-20 09:37:08 +02:00
v2ray
7a1bdfcf00 test case for passive connection 2016-08-20 09:12:44 +02:00
v2ray
2268f3d1bc remove import path restriction 2016-08-19 17:05:15 +02:00
v2ray
d83a0ce5d2 Apply new read function to UDP hub. 2016-08-19 13:00:55 +02:00
v2ray
55e5268ce8 Fix UDP reading on Windows 2016-08-19 12:58:26 +02:00
v2ray
ad1353ac2f merge similar error definitions 2016-08-18 08:34:21 +02:00
v2ray
786775ea80 fix build break 2016-08-18 08:24:47 +02:00
v2ray
39737f6fc1 merge bad configuration error 2016-08-18 08:21:20 +02:00
v2ray
933e244d92 value receiver for StringList.Len() 2016-08-18 07:56:31 +02:00
v2ray
89a876a6d4 rename proxy/internal to proxy/registry 2016-08-17 23:30:15 +02:00
v2ray
d9884b4010 format import 2016-08-17 23:14:13 +02:00
v2ray
8ecec9b49d format log 2016-08-17 23:12:10 +02:00
v2ray
bb3ceb3d7b simplify case 2016-08-17 23:05:47 +02:00
v2ray
49056d9942 Merge branch 'master' of https://github.com/v2ray/v2ray-core 2016-08-17 15:25:40 +02:00
Darien Raymond
8951c4da71 comment 2016-08-17 10:05:53 +00:00
v2ray
99a63f1633 reset timer before benchmark 2016-08-17 10:28:30 +02:00
v2ray
99821dc2fb change to wait group 2016-08-16 17:00:04 +02:00
v2ray
125ef8372c actually follow redirect of UDP 2016-08-16 13:04:07 +02:00
336 changed files with 13457 additions and 10090 deletions

View File

@@ -1,14 +1,16 @@
language: go
go:
- 1.7
- 1.7.1
go_import_path: v2ray.com/core
git:
depth: 5
script:
- go test -tags json github.com/v2ray/v2ray-core/...
- go test -tags json v2ray.com/core/...
after_success:
- ./testing/coverage/coverall
@@ -19,7 +21,7 @@ before_deploy:
deploy:
provider: releases
api_key:
secure: "Nbno1ibFpq0DC6y0mkN6NFTbDQinRAEQo2Jl5lvsbiaX9m4CK59xG8XOcmZ3mqqdeioj6m9X5BVe2UtCZjmXmnyPq0FYNFHAPTfd2VrcOpi0fWLD9ndTYAsqjxrqPGK62XVjPbG8U2pGdq2yCOb0qQ9DlahDhVIMzQ/GYuRDps25fPGftepdPwlrwOfYCIvAHoi3GEozuS67WNEx/YtmZTce/2Qebdmwf44hUNaQibeXEspu9wQcp9M/i7aTuX/tUbn7rVCQ4TfoIJU1C1qKedUw0QYMjqP08Wn6UjwwhbblsJKFv8Kc0nx+i2K9CvvLyaBBIVY2Ggxj4Niy+X4VbyWWzdKx+0FXlmKh3U/hJdMZU3OrnwliBns2X3tCHSq7/+7ynLs3UZ4wCkCl7rQYucJ2mlvGquohQV6MX2vjl73y+pYljf+PKYbSDBb7iUOjllisRD+HmDmGQl81BvdEpN/K/BlfsTwHoHxsALSTjRljSngH51SUvjnaVbi/JolJ3lomTr1vCHl4WUiQVLnh7O2Cg7QLNMNieoblnh9yDQG9wC4xuzOJ/AaxRd/pRZMeIooQ1zsHOe+47C1YAep88b+9xtRqVMozAjk/XgNzWjuT2R8G7lZDq0mL9VajD/NUjM3Yrswoft9A3DRmR+lIFJR+fJb8tNf+k1OczE4lesg="
secure: "FORkI+hXFWSxLkqkBDvo8APQ1XLfuzbOBqhVW474x299AzHpgE/7dYLNo/rQaerYMdmw8gB21tUWOI3FjUYv51x1dO2a2Bu8Por+1BQ+zdUIyS3zJteJ8bAMZGVtg1BCTFjxQsIDMmaziaScSKz3NwEJ1IL8UIBiidFf8EuYyK+EL6yQFodrSeUDsUH52pBO0SBugsAj4lOSTCS25ZcpWYcUMBo7zaD9sNE1Oo8f/bz7cFe4cJLCpazIgkt57IT3u/8BH/EMz4HRbAqj48mjKytp4lm7R3IC8w0ba6yLNXgKqva10B/KNwZQweXqglM5iawQDdSbnIjUnVEe9Oexu8wNV52oBt+8wC7P/ZtAsZTnyWvwIhH2uyhzEHfsM5Up7DnAnaATh/+2upn3lcs4s2p9d/6tIObeIpBvP/3QRNmlmtZFIOnlqkcROj9NQ3IHGbswGiAfBDAsAC8h9CRDy9MtRs6ZbaCih0FLgMzEXi+S50eelNRv4SGaeLlW07cz7uma5C+z+zWb4ZmWsOwWjkOAaFL/zL5O9/w7i4cbXr/sQ6z6YmdDEejbrkHiE8DmKQqeV13xY1FhDqFyEpsdUbftG6pKPt5wcyNEAnw9aPMK7Ec0dAe6vg+oTcfMPAO/rHRXEc4IP56QqdlZTmsZOdDrGgnkRxpbayc+KcvrN28="
file:
- "$GOPATH/bin/v2ray-macos.zip"
- "$GOPATH/bin/v2ray-windows-64.zip"

View File

@@ -2,5 +2,12 @@
{
"editor.tabSize": 2,
"go.buildTags": "json"
"go.buildTags": "json",
"protoc": {
"options": [
"--proto_path=$GOPATH/src/",
"--proto_path=$GOPATH/src/github.com/google/protobuf/src"
]
}
}

4
.vscode/tasks.json vendored
View File

@@ -6,7 +6,7 @@
"tasks": [
{
"taskName": "build",
"args": ["-tags", "json", "github.com/v2ray/v2ray-core/..."],
"args": ["-tags", "json", "v2ray.com/core/..."],
"isBuildCommand": true,
"problemMatcher": {
"owner": "go",
@@ -21,7 +21,7 @@
},
{
"taskName": "test",
"args": ["-tags", "json", "github.com/v2ray/v2ray-core/..."],
"args": ["-tags", "json", "v2ray.com/core/..."],
"isBuildCommand": false
}
]

View File

@@ -5,10 +5,10 @@
[2]: https://travis-ci.org/v2ray/v2ray-core "Travis-CI Build Status"
[3]: https://codecov.io/github/v2ray/v2ray-core/coverage.svg?branch=master "Coverage badge"
[4]: https://codecov.io/github/v2ray/v2ray-core?branch=master "Codecov Status"
[5]: https://goreportcard.com/badge/github.com/v2ray/v2ray-core "Go Report badge"
[6]: https://goreportcard.com/report/github.com/v2ray/v2ray-core "Go Report"
[7]: https://godoc.org/github.com/v2ray/v2ray-core?status.svg "GoDoc badge"
[8]: https://godoc.org/github.com/v2ray/v2ray-core "GoDoc"
[5]: https://goreportcard.com/badge/v2ray.com/core "Go Report badge"
[6]: https://goreportcard.com/report/v2ray.com/core "Go Report"
[7]: https://godoc.org/v2ray.com/core?status.svg "GoDoc badge"
[8]: https://godoc.org/v2ray.com/core "GoDoc"
[9]: https://codebeat.co/badges/f2354ca8-3e24-463d-a2e3-159af73b2477 "Codebeat badge"
[10]: https://codebeat.co/projects/github-com-v2ray-v2ray-core "Codebeat"

View File

@@ -1,7 +1,7 @@
package api
import (
"github.com/v2ray/v2ray-core/app"
"v2ray.com/core/app"
)
const (

View File

@@ -1,7 +1,7 @@
package api
import (
v2net "github.com/v2ray/v2ray-core/common/net"
v2net "v2ray.com/core/common/net"
)
type Config struct {

View File

@@ -1,9 +1,9 @@
package dispatcher
import (
"github.com/v2ray/v2ray-core/app"
"github.com/v2ray/v2ray-core/proxy"
"github.com/v2ray/v2ray-core/transport/ray"
"v2ray.com/core/app"
"v2ray.com/core/proxy"
"v2ray.com/core/transport/ray"
)
const (

View File

@@ -1,14 +1,14 @@
package impl
import (
"github.com/v2ray/v2ray-core/app"
"github.com/v2ray/v2ray-core/app/proxyman"
"github.com/v2ray/v2ray-core/app/router"
"github.com/v2ray/v2ray-core/common/alloc"
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/proxy"
"github.com/v2ray/v2ray-core/transport/ray"
"v2ray.com/core/app"
"v2ray.com/core/app/proxyman"
"v2ray.com/core/app/router"
"v2ray.com/core/common/alloc"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy"
"v2ray.com/core/transport/ray"
)
type DefaultDispatcher struct {
@@ -24,7 +24,7 @@ func NewDefaultDispatcher(space app.Space) *DefaultDispatcher {
return d
}
// @Private
// Private: Used by app.Space only.
func (this *DefaultDispatcher) Initialize(space app.Space) error {
if !space.HasApp(proxyman.APP_ID_OUTBOUND_MANAGER) {
log.Error("DefaultDispatcher: OutboundHandlerManager is not found in the space.")
@@ -70,7 +70,7 @@ func (this *DefaultDispatcher) DispatchToOutbound(meta *proxy.InboundHandlerMeta
return direct
}
// @Private
// Private: Visible for testing.
func (this *DefaultDispatcher) FilterPacketAndDispatch(destination v2net.Destination, link ray.OutboundRay, dispatcher proxy.OutboundHandler) {
payload, err := link.OutboundInput().Read()
if err != nil {

View File

@@ -1,9 +1,9 @@
package testing
import (
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/proxy"
"github.com/v2ray/v2ray-core/transport/ray"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy"
"v2ray.com/core/transport/ray"
)
type TestPacketDispatcher struct {

View File

@@ -3,10 +3,18 @@ package dns
import (
"net"
v2net "github.com/v2ray/v2ray-core/common/net"
"v2ray.com/core/common/log"
)
type Config struct {
Hosts map[string]net.IP
NameServers []v2net.Destination
func (this *Config) GetInternalHosts() map[string]net.IP {
hosts := make(map[string]net.IP)
for domain, addressPB := range this.GetHosts() {
address := addressPB.AsAddress()
if address.Family().IsDomain() {
log.Warning("DNS: Ignoring domain address in static hosts: ", address.Domain())
continue
}
hosts[domain] = address.IP()
}
return hosts
}

82
app/dns/config.pb.go Normal file
View File

@@ -0,0 +1,82 @@
// Code generated by protoc-gen-go.
// source: v2ray.com/core/app/dns/config.proto
// DO NOT EDIT!
/*
Package dns is a generated protocol buffer package.
It is generated from these files:
v2ray.com/core/app/dns/config.proto
It has these top-level messages:
Config
*/
package dns
import proto "github.com/golang/protobuf/proto"
import fmt "fmt"
import math "math"
import v2ray_core_common_net "v2ray.com/core/common/net"
import v2ray_core_common_net2 "v2ray.com/core/common/net"
// 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 struct {
NameServers []*v2ray_core_common_net2.DestinationPB `protobuf:"bytes,1,rep,name=NameServers,json=nameServers" json:"NameServers,omitempty"`
Hosts map[string]*v2ray_core_common_net.AddressPB `protobuf:"bytes,2,rep,name=Hosts,json=hosts" json:"Hosts,omitempty" protobuf_key:"bytes,1,opt,name=key" protobuf_val:"bytes,2,opt,name=value"`
}
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 (m *Config) GetNameServers() []*v2ray_core_common_net2.DestinationPB {
if m != nil {
return m.NameServers
}
return nil
}
func (m *Config) GetHosts() map[string]*v2ray_core_common_net.AddressPB {
if m != nil {
return m.Hosts
}
return nil
}
func init() {
proto.RegisterType((*Config)(nil), "v2ray.core.app.dns.Config")
}
func init() { proto.RegisterFile("v2ray.com/core/app/dns/config.proto", fileDescriptor0) }
var fileDescriptor0 = []byte{
// 270 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x74, 0x90, 0xcb, 0x4a, 0xc4, 0x30,
0x14, 0x86, 0x49, 0x4b, 0x07, 0x4c, 0x37, 0x92, 0x85, 0x94, 0xae, 0x8a, 0x17, 0x2c, 0x0a, 0x09,
0x54, 0x10, 0xd1, 0x95, 0xf5, 0x82, 0x2b, 0x29, 0x75, 0x37, 0xbb, 0xd8, 0x1c, 0xb5, 0x68, 0x4f,
0x4a, 0x12, 0x0b, 0x7d, 0x64, 0xdf, 0x42, 0xda, 0x20, 0x15, 0x9d, 0xd9, 0x1d, 0xc2, 0xf7, 0x5f,
0xf2, 0xd3, 0x83, 0xa1, 0x30, 0x72, 0xe4, 0x8d, 0xee, 0x44, 0xa3, 0x0d, 0x08, 0xd9, 0xf7, 0x42,
0xa1, 0x15, 0x8d, 0xc6, 0x97, 0xf6, 0x95, 0xf7, 0x46, 0x3b, 0xcd, 0xd8, 0x0f, 0x64, 0x80, 0xcb,
0xbe, 0xe7, 0x0a, 0x6d, 0x7a, 0xfc, 0x47, 0xd8, 0xe8, 0xae, 0xd3, 0x28, 0x10, 0x9c, 0x90, 0x4a,
0x19, 0xb0, 0xd6, 0x8b, 0xd3, 0xd3, 0xed, 0xa0, 0x02, 0xeb, 0x5a, 0x94, 0xae, 0xd5, 0xe8, 0xe1,
0xfd, 0x2f, 0x42, 0x57, 0x37, 0x73, 0x34, 0xbb, 0xa7, 0xf1, 0xa3, 0xec, 0xe0, 0x09, 0xcc, 0x00,
0xc6, 0x26, 0x24, 0x0b, 0xf3, 0xb8, 0x38, 0xe4, 0xbf, 0xaa, 0x78, 0x27, 0x8e, 0xe0, 0xf8, 0xed,
0xe2, 0x54, 0x95, 0x75, 0x8c, 0x8b, 0x90, 0x5d, 0xd1, 0xe8, 0x41, 0x5b, 0x67, 0x93, 0x60, 0x76,
0x38, 0xe2, 0xff, 0x3f, 0xc3, 0x7d, 0x24, 0x9f, 0xb9, 0x3b, 0x74, 0x66, 0xac, 0xa3, 0xb7, 0xe9,
0x4e, 0xd7, 0x94, 0x2e, 0x8f, 0x6c, 0x97, 0x86, 0xef, 0x30, 0x26, 0x24, 0x23, 0xf9, 0x4e, 0x3d,
0x9d, 0xec, 0x9c, 0x46, 0x83, 0xfc, 0xf8, 0x84, 0x24, 0xc8, 0x48, 0x1e, 0x17, 0xd9, 0x96, 0x7a,
0xd7, 0x7e, 0x91, 0xaa, 0xac, 0x3d, 0x7e, 0x19, 0x5c, 0x90, 0xf2, 0x84, 0xee, 0x35, 0xba, 0xdb,
0x50, 0xa7, 0x8c, 0x7d, 0x9f, 0x6a, 0x9a, 0x64, 0x1d, 0x2a, 0xb4, 0xcf, 0xab, 0x79, 0x9e, 0xb3,
0xef, 0x00, 0x00, 0x00, 0xff, 0xff, 0x17, 0x9a, 0x1d, 0xee, 0xaf, 0x01, 0x00, 0x00,
}

14
app/dns/config.proto Normal file
View File

@@ -0,0 +1,14 @@
syntax = "proto3";
package v2ray.core.app.dns;
option go_package = "dns";
option java_package = "com.v2ray.core.app.dns";
option java_outer_classname = "ConfigProto";
import "v2ray.com/core/common/net/address.proto";
import "v2ray.com/core/common/net/destination.proto";
message Config {
repeated v2ray.core.common.net.DestinationPB NameServers = 1;
map<string, v2ray.core.common.net.AddressPB> Hosts = 2;
}

View File

@@ -4,34 +4,30 @@ package dns
import (
"encoding/json"
"errors"
"net"
v2net "github.com/v2ray/v2ray-core/common/net"
v2net "v2ray.com/core/common/net"
)
func (this *Config) UnmarshalJSON(data []byte) error {
type JsonConfig struct {
Servers []v2net.AddressJson `json:"servers"`
Hosts map[string]v2net.AddressJson `json:"hosts"`
Servers []*v2net.AddressPB `json:"servers"`
Hosts map[string]*v2net.AddressPB `json:"hosts"`
}
jsonConfig := new(JsonConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
return err
}
this.NameServers = make([]v2net.Destination, len(jsonConfig.Servers))
this.NameServers = make([]*v2net.DestinationPB, len(jsonConfig.Servers))
for idx, server := range jsonConfig.Servers {
this.NameServers[idx] = v2net.UDPDestination(server.Address, v2net.Port(53))
this.NameServers[idx] = &v2net.DestinationPB{
Network: v2net.Network_UDP,
Address: server,
Port: 53,
}
}
if jsonConfig.Hosts != nil {
this.Hosts = make(map[string]net.IP)
for domain, ip := range jsonConfig.Hosts {
if ip.Address.Family().IsDomain() {
return errors.New(ip.Address.String() + " is not an IP.")
}
this.Hosts[domain] = ip.Address.IP()
}
this.Hosts = jsonConfig.Hosts
}
return nil

View File

@@ -6,9 +6,9 @@ import (
"encoding/json"
"testing"
. "github.com/v2ray/v2ray-core/app/dns"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/testing/assert"
. "v2ray.com/core/app/dns"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/testing/assert"
)
func TestConfigParsing(t *testing.T) {
@@ -22,7 +22,8 @@ func TestConfigParsing(t *testing.T) {
err := json.Unmarshal([]byte(rawJson), config)
assert.Error(err).IsNil()
assert.Int(len(config.NameServers)).Equals(1)
assert.Destination(config.NameServers[0]).IsUDP()
assert.Address(config.NameServers[0].Address()).Equals(v2net.IPAddress([]byte{8, 8, 8, 8}))
assert.Port(config.NameServers[0].Port()).Equals(v2net.Port(53))
dest := config.NameServers[0].AsDestination()
assert.Destination(dest).IsUDP()
assert.Address(dest.Address).Equals(v2net.IPAddress([]byte{8, 8, 8, 8}))
assert.Port(dest.Port).Equals(v2net.Port(53))
}

View File

@@ -3,7 +3,7 @@ package dns
import (
"net"
"github.com/v2ray/v2ray-core/app"
"v2ray.com/core/app"
)
const (

View File

@@ -5,13 +5,13 @@ import (
"sync"
"time"
"github.com/v2ray/v2ray-core/app/dispatcher"
"github.com/v2ray/v2ray-core/common/alloc"
"github.com/v2ray/v2ray-core/common/dice"
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/proxy"
"github.com/v2ray/v2ray-core/transport/internet/udp"
"v2ray.com/core/app/dispatcher"
"v2ray.com/core/common/alloc"
"v2ray.com/core/common/dice"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy"
"v2ray.com/core/transport/internet/udp"
"github.com/miekg/dns"
)
@@ -59,7 +59,7 @@ func NewUDPNameServer(address v2net.Destination, dispatcher dispatcher.PacketDis
return s
}
// @Private
// Private: Visible for testing.
func (this *UDPNameServer) Cleanup() {
expiredRequests := make([]uint16, 0, 16)
now := time.Now()
@@ -77,7 +77,7 @@ func (this *UDPNameServer) Cleanup() {
expiredRequests = nil
}
// @Private
// Private: Visible for testing.
func (this *UDPNameServer) AssignUnusedID(response chan<- *ARecord) uint16 {
var id uint16
this.Lock()
@@ -102,7 +102,7 @@ func (this *UDPNameServer) AssignUnusedID(response chan<- *ARecord) uint16 {
return id
}
// @Private
// Private: Visible for testing.
func (this *UDPNameServer) HandleResponse(dest v2net.Destination, payload *alloc.Buffer) {
msg := new(dns.Msg)
err := msg.Unpack(payload.Value)

View File

@@ -5,9 +5,10 @@ import (
"sync"
"time"
"github.com/v2ray/v2ray-core/app"
"github.com/v2ray/v2ray-core/app/dispatcher"
"github.com/v2ray/v2ray-core/common/log"
"v2ray.com/core/app"
"v2ray.com/core/app/dispatcher"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"github.com/miekg/dns"
)
@@ -32,7 +33,7 @@ func NewCacheServer(space app.Space, config *Config) *CacheServer {
server := &CacheServer{
records: make(map[string]*DomainRecord),
servers: make([]NameServer, len(config.NameServers)),
hosts: config.Hosts,
hosts: config.GetInternalHosts(),
}
space.InitializeApplication(func() error {
if !space.HasApp(dispatcher.APP_ID) {
@@ -41,11 +42,18 @@ func NewCacheServer(space app.Space, config *Config) *CacheServer {
}
dispatcher := space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher)
for idx, ns := range config.NameServers {
if ns.Address().Family().IsDomain() && ns.Address().Domain() == "localhost" {
for idx, destPB := range config.NameServers {
address := destPB.Address.AsAddress()
if address.Family().IsDomain() && address.Domain() == "localhost" {
server.servers[idx] = &LocalNameServer{}
} else {
server.servers[idx] = NewUDPNameServer(ns, dispatcher)
dest := destPB.AsDestination()
if dest.Network == v2net.Network_Unknown {
dest.Network = v2net.Network_UDP
}
if dest.Network == v2net.Network_UDP {
server.servers[idx] = NewUDPNameServer(dest, dispatcher)
}
}
}
if len(config.NameServers) == 0 {
@@ -60,7 +68,7 @@ func (this *CacheServer) Release() {
}
//@Private
// Private: Visible for testing.
func (this *CacheServer) GetCached(domain string) []net.IP {
this.RLock()
defer this.RUnlock()

View File

@@ -4,16 +4,16 @@ import (
"net"
"testing"
"github.com/v2ray/v2ray-core/app"
"github.com/v2ray/v2ray-core/app/dispatcher"
dispatchers "github.com/v2ray/v2ray-core/app/dispatcher/impl"
. "github.com/v2ray/v2ray-core/app/dns"
"github.com/v2ray/v2ray-core/app/proxyman"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/proxy"
"github.com/v2ray/v2ray-core/proxy/freedom"
"github.com/v2ray/v2ray-core/testing/assert"
"github.com/v2ray/v2ray-core/transport/internet"
"v2ray.com/core/app"
"v2ray.com/core/app/dispatcher"
dispatchers "v2ray.com/core/app/dispatcher/impl"
. "v2ray.com/core/app/dns"
"v2ray.com/core/app/proxyman"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy"
"v2ray.com/core/proxy/freedom"
"v2ray.com/core/testing/assert"
"v2ray.com/core/transport/internet"
)
func TestDnsAdd(t *testing.T) {
@@ -28,8 +28,8 @@ func TestDnsAdd(t *testing.T) {
space,
&proxy.OutboundHandlerMeta{
Address: v2net.AnyIP,
StreamSettings: &internet.StreamSettings{
Type: internet.StreamConnectionTypeRawTCP,
StreamSettings: &internet.StreamConfig{
Network: v2net.Network_RawTCP,
},
}))
space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, outboundHandlerManager)
@@ -37,9 +37,15 @@ func TestDnsAdd(t *testing.T) {
domain := "local.v2ray.com"
server := NewCacheServer(space, &Config{
NameServers: []v2net.Destination{
v2net.UDPDestination(v2net.IPAddress([]byte{8, 8, 8, 8}), v2net.Port(53)),
},
NameServers: []*v2net.DestinationPB{{
Network: v2net.Network_UDP,
Address: &v2net.AddressPB{
Address: &v2net.AddressPB_Ip{
Ip: []byte{8, 8, 8, 8},
},
},
Port: 53,
}},
})
space.BindApp(APP_ID, server)
space.Initialize()

View File

@@ -3,8 +3,8 @@ package proxyman
import (
"sync"
"github.com/v2ray/v2ray-core/app"
"github.com/v2ray/v2ray-core/proxy"
"v2ray.com/core/app"
"v2ray.com/core/proxy"
)
const (

View File

@@ -1,15 +1,13 @@
package router
import (
"errors"
"v2ray.com/core/common"
)
type ConfigObjectCreator func([]byte) (interface{}, error)
var (
configCache map[string]ConfigObjectCreator
ErrRouterNotFound = errors.New("Router not found.")
)
func RegisterRouterConfig(strategy string, creator ConfigObjectCreator) error {
@@ -21,7 +19,7 @@ func RegisterRouterConfig(strategy string, creator ConfigObjectCreator) error {
func CreateRouterConfig(strategy string, data []byte) (interface{}, error) {
creator, found := configCache[strategy]
if !found {
return nil, ErrRouterNotFound
return nil, common.ErrObjectNotFound
}
return creator(data)
}

View File

@@ -5,7 +5,7 @@ package router
import (
"encoding/json"
"github.com/v2ray/v2ray-core/common/log"
"v2ray.com/core/common/log"
)
func (this *Config) UnmarshalJSON(data []byte) error {

View File

@@ -1,9 +1,10 @@
package router
import (
"github.com/v2ray/v2ray-core/app"
"github.com/v2ray/v2ray-core/common"
v2net "github.com/v2ray/v2ray-core/common/net"
"v2ray.com/core/app"
"v2ray.com/core/common"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
)
const (
@@ -24,7 +25,9 @@ var (
)
func RegisterRouter(name string, factory RouterFactory) error {
// TODO: check name
if _, found := routerCache[name]; found {
return common.ErrDuplicatedName
}
routerCache[name] = factory
return nil
}
@@ -33,5 +36,6 @@ func CreateRouter(name string, rawConfig interface{}, space app.Space) (Router,
if factory, found := routerCache[name]; found {
return factory.Create(rawConfig, space)
}
return nil, ErrRouterNotFound
log.Error("Router: not found: ", name)
return nil, common.ErrObjectNotFound
}

View File

@@ -13,7 +13,7 @@ import (
"strconv"
"strings"
v2net "github.com/v2ray/v2ray-core/common/net"
v2net "v2ray.com/core/common/net"
)
const (
@@ -65,7 +65,7 @@ func main() {
fmt.Fprintln(file, "package rules")
fmt.Fprintln(file, "import (")
fmt.Fprintln(file, "v2net \"github.com/v2ray/v2ray-core/common/net\"")
fmt.Fprintln(file, "v2net \"v2ray.com/core/common/net\"")
fmt.Fprintln(file, ")")
fmt.Fprintln(file, "var (")

File diff suppressed because it is too large Load Diff

View File

@@ -5,7 +5,7 @@ package rules
import (
"encoding/json"
"github.com/v2ray/v2ray-core/common/log"
"v2ray.com/core/common/log"
)
func parseChinaIPRule(data []byte) (*Rule, error) {

View File

@@ -5,8 +5,8 @@ package rules_test
import (
"testing"
. "github.com/v2ray/v2ray-core/app/router/rules"
"github.com/v2ray/v2ray-core/testing/assert"
. "v2ray.com/core/app/router/rules"
"v2ray.com/core/testing/assert"
)
func TestChinaIPJson(t *testing.T) {

View File

@@ -4,9 +4,9 @@ import (
"net"
"testing"
. "github.com/v2ray/v2ray-core/app/router/rules"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/testing/assert"
. "v2ray.com/core/app/router/rules"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/testing/assert"
)
func makeDestination(ip string) v2net.Destination {

View File

@@ -4,7 +4,7 @@ package rules
import (
"encoding/json"
"github.com/v2ray/v2ray-core/common/log"
"v2ray.com/core/common/log"
)
func parseChinaSitesRule(data []byte) (*Rule, error) {

View File

@@ -5,8 +5,8 @@ package rules_test
import (
"testing"
. "github.com/v2ray/v2ray-core/app/router/rules"
"github.com/v2ray/v2ray-core/testing/assert"
. "v2ray.com/core/app/router/rules"
"v2ray.com/core/testing/assert"
)
func TestChinaSitesJson(t *testing.T) {

View File

@@ -3,9 +3,9 @@ package rules_test
import (
"testing"
. "github.com/v2ray/v2ray-core/app/router/rules"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/testing/assert"
. "v2ray.com/core/app/router/rules"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/testing/assert"
)
func makeDomainDestination(domain string) v2net.Destination {

View File

@@ -5,7 +5,7 @@ import (
"regexp"
"strings"
v2net "github.com/v2ray/v2ray-core/common/net"
v2net "v2ray.com/core/common/net"
)
type Condition interface {
@@ -73,10 +73,10 @@ func NewPlainDomainMatcher(pattern string) *PlainDomainMatcher {
}
func (this *PlainDomainMatcher) Apply(dest v2net.Destination) bool {
if !dest.Address().Family().IsDomain() {
if !dest.Address.Family().IsDomain() {
return false
}
domain := dest.Address().Domain()
domain := dest.Address.Domain()
return strings.Contains(domain, this.pattern)
}
@@ -95,10 +95,10 @@ func NewRegexpDomainMatcher(pattern string) (*RegexpDomainMatcher, error) {
}
func (this *RegexpDomainMatcher) Apply(dest v2net.Destination) bool {
if !dest.Address().Family().IsDomain() {
if !dest.Address.Family().IsDomain() {
return false
}
domain := dest.Address().Domain()
domain := dest.Address.Domain()
return this.pattern.MatchString(strings.ToLower(domain))
}
@@ -117,10 +117,10 @@ func NewCIDRMatcher(ipnet string) (*CIDRMatcher, error) {
}
func (this *CIDRMatcher) Apply(dest v2net.Destination) bool {
if !dest.Address().Family().Either(v2net.AddressFamilyIPv4, v2net.AddressFamilyIPv6) {
if !dest.Address.Family().Either(v2net.AddressFamilyIPv4, v2net.AddressFamilyIPv6) {
return false
}
return this.cidr.Contains(dest.Address().IP())
return this.cidr.Contains(dest.Address.IP())
}
type IPv4Matcher struct {
@@ -134,10 +134,10 @@ func NewIPv4Matcher(ipnet *v2net.IPNet) *IPv4Matcher {
}
func (this *IPv4Matcher) Apply(dest v2net.Destination) bool {
if !dest.Address().Family().Either(v2net.AddressFamilyIPv4) {
if !dest.Address.Family().Either(v2net.AddressFamilyIPv4) {
return false
}
return this.ipv4net.Contains(dest.Address().IP())
return this.ipv4net.Contains(dest.Address.IP())
}
type PortMatcher struct {
@@ -151,7 +151,7 @@ func NewPortMatcher(portRange v2net.PortRange) *PortMatcher {
}
func (this *PortMatcher) Apply(dest v2net.Destination) bool {
return this.port.Contains(dest.Port())
return this.port.Contains(dest.Port)
}
type NetworkMatcher struct {
@@ -165,5 +165,5 @@ func NewNetworkMatcher(network *v2net.NetworkList) *NetworkMatcher {
}
func (this *NetworkMatcher) Apply(dest v2net.Destination) bool {
return this.network.HasNetwork(dest.Network())
return this.network.HasNetwork(dest.Network)
}

View File

@@ -1,7 +1,7 @@
package rules
import (
v2net "github.com/v2ray/v2ray-core/common/net"
v2net "v2ray.com/core/common/net"
)
type Rule struct {

View File

@@ -7,10 +7,10 @@ import (
"errors"
"strings"
router "github.com/v2ray/v2ray-core/app/router"
"github.com/v2ray/v2ray-core/common/collect"
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
router "v2ray.com/core/app/router"
"v2ray.com/core/common/collect"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
)
type JsonRule struct {

View File

@@ -5,9 +5,9 @@ package rules_test
import (
"testing"
. "github.com/v2ray/v2ray-core/app/router/rules"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/testing/assert"
. "v2ray.com/core/app/router/rules"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/testing/assert"
)
func TestDomainRule(t *testing.T) {

View File

@@ -3,11 +3,11 @@ package rules
import (
"errors"
"github.com/v2ray/v2ray-core/app"
"github.com/v2ray/v2ray-core/app/dns"
"github.com/v2ray/v2ray-core/app/router"
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
"v2ray.com/core/app"
"v2ray.com/core/app/dns"
"v2ray.com/core/app/router"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
)
var (
@@ -41,18 +41,18 @@ func (this *Router) Release() {
}
// @Private
// Private: Visible for testing.
func (this *Router) ResolveIP(dest v2net.Destination) []v2net.Destination {
ips := this.dnsServer.Get(dest.Address().Domain())
ips := this.dnsServer.Get(dest.Address.Domain())
if len(ips) == 0 {
return nil
}
dests := make([]v2net.Destination, len(ips))
for idx, ip := range ips {
if dest.IsTCP() {
dests[idx] = v2net.TCPDestination(v2net.IPAddress(ip), dest.Port())
if dest.Network == v2net.Network_TCP {
dests[idx] = v2net.TCPDestination(v2net.IPAddress(ip), dest.Port)
} else {
dests[idx] = v2net.UDPDestination(v2net.IPAddress(ip), dest.Port())
dests[idx] = v2net.UDPDestination(v2net.IPAddress(ip), dest.Port)
}
}
return dests
@@ -64,7 +64,7 @@ func (this *Router) takeDetourWithoutCache(dest v2net.Destination) (string, erro
return rule.Tag, nil
}
}
if this.config.DomainStrategy == UseIPIfNonMatch && dest.Address().Family().IsDomain() {
if this.config.DomainStrategy == UseIPIfNonMatch && dest.Address.Family().IsDomain() {
log.Info("Router: Looking up IP for ", dest)
ipDests := this.ResolveIP(dest)
if ipDests != nil {

View File

@@ -3,15 +3,15 @@ package rules_test
import (
"testing"
"github.com/v2ray/v2ray-core/app"
"github.com/v2ray/v2ray-core/app/dispatcher"
dispatchers "github.com/v2ray/v2ray-core/app/dispatcher/impl"
"github.com/v2ray/v2ray-core/app/dns"
"github.com/v2ray/v2ray-core/app/proxyman"
"github.com/v2ray/v2ray-core/app/router"
. "github.com/v2ray/v2ray-core/app/router/rules"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/testing/assert"
"v2ray.com/core/app"
"v2ray.com/core/app/dispatcher"
dispatchers "v2ray.com/core/app/dispatcher/impl"
"v2ray.com/core/app/dns"
"v2ray.com/core/app/proxyman"
"v2ray.com/core/app/router"
. "v2ray.com/core/app/router/rules"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/testing/assert"
)
func TestSimpleRouter(t *testing.T) {
@@ -21,7 +21,7 @@ func TestSimpleRouter(t *testing.T) {
Rules: []*Rule{
{
Tag: "test",
Condition: NewNetworkMatcher(v2net.Network("tcp").AsList()),
Condition: NewNetworkMatcher(v2net.Network_TCP.AsList()),
},
},
}

View File

@@ -3,7 +3,7 @@ package app
import (
"errors"
"github.com/v2ray/v2ray-core/common"
"v2ray.com/core/common"
)
var (

View File

@@ -5,7 +5,7 @@ import (
"hash"
"io"
"github.com/v2ray/v2ray-core/common/serial"
"v2ray.com/core/common/serial"
)
const (
@@ -52,6 +52,7 @@ func (b *Buffer) Clear() *Buffer {
return b
}
// Reset resets this Buffer into its original state.
func (b *Buffer) Reset() *Buffer {
b.offset = defaultOffset
b.Value = b.head

View File

@@ -1,6 +1,8 @@
package alloc
import (
"os"
"strconv"
"sync"
)
@@ -14,14 +16,14 @@ type BufferPool struct {
allocator *sync.Pool
}
func NewBufferPool(bufferSize, poolSize int) *BufferPool {
func NewBufferPool(bufferSize, poolSize uint32) *BufferPool {
pool := &BufferPool{
chain: make(chan []byte, poolSize),
allocator: &sync.Pool{
New: func() interface{} { return make([]byte, bufferSize) },
},
}
for i := 0; i < poolSize; i++ {
for i := uint32(0); i < poolSize; i++ {
pool.chain <- make([]byte, bufferSize)
}
return pool
@@ -51,10 +53,32 @@ func (p *BufferPool) Free(buffer *Buffer) {
const (
SmallBufferSize = 1600 - defaultOffset
BufferSize = 8*1024 - defaultOffset
LargeBufferSize = 64*1024 - defaultOffset
mediumBufferByteSize = 8 * 1024
BufferSize = mediumBufferByteSize - defaultOffset
largeBufferByteSize = 64 * 1024
LargeBufferSize = largeBufferByteSize - defaultOffset
PoolSizeEnvKey = "v2ray.buffer.size"
)
var smallPool = NewBufferPool(1600, 256)
var mediumPool = NewBufferPool(8*1024, 1024)
var largePool = NewBufferPool(64*1024, 32)
var (
smallPool = NewBufferPool(1600, 256)
mediumPool *BufferPool
largePool *BufferPool
)
func init() {
var size uint32 = 20
sizeStr := os.Getenv(PoolSizeEnvKey)
if len(sizeStr) > 0 {
customSize, err := strconv.ParseUint(sizeStr, 10, 32)
if err == nil {
size = uint32(customSize)
}
}
totalByteSize := size * 1024 * 1024
mediumPool = NewBufferPool(mediumBufferByteSize, totalByteSize/4*3/mediumBufferByteSize)
largePool = NewBufferPool(largeBufferByteSize, totalByteSize/4/largeBufferByteSize)
}

View File

@@ -3,8 +3,8 @@ package alloc_test
import (
"testing"
. "github.com/v2ray/v2ray-core/common/alloc"
"github.com/v2ray/v2ray-core/testing/assert"
. "v2ray.com/core/common/alloc"
"v2ray.com/core/testing/assert"
)
func TestBufferClear(t *testing.T) {

View File

@@ -7,6 +7,6 @@ func NewStringList(raw []string) *StringList {
return &list
}
func (this *StringList) Len() int {
return len(*this)
func (this StringList) Len() int {
return len(this)
}

View File

@@ -6,8 +6,8 @@ import (
"encoding/json"
"testing"
. "github.com/v2ray/v2ray-core/common/collect"
"github.com/v2ray/v2ray-core/testing/assert"
. "v2ray.com/core/common/collect"
"v2ray.com/core/testing/assert"
)
func TestStringListUnmarshalError(t *testing.T) {
@@ -18,3 +18,13 @@ func TestStringListUnmarshalError(t *testing.T) {
err := json.Unmarshal([]byte(rawJson), list)
assert.Error(err).IsNotNil()
}
func TestStringListLen(t *testing.T) {
assert := assert.On(t)
rawJson := `"a, b, c, d"`
list := new(StringList)
err := json.Unmarshal([]byte(rawJson), list)
assert.Error(err).IsNil()
assert.Int(list.Len()).Equals(4)
}

View File

@@ -9,6 +9,8 @@ import (
var (
ErrObjectReleased = errors.New("Object already released.")
ErrBadConfiguration = errors.New("Bad configuration.")
ErrObjectNotFound = errors.New("Object not found.")
ErrDuplicatedName = errors.New("Duplicated name.")
)
// Releasable interface is for those types that can release its members.

View File

@@ -4,7 +4,7 @@ import (
"crypto/cipher"
"testing"
. "github.com/v2ray/v2ray-core/common/crypto"
. "v2ray.com/core/common/crypto"
)
const benchSize = 1024 * 1024
@@ -13,6 +13,7 @@ func benchmarkStream(b *testing.B, c cipher.Stream) {
b.SetBytes(benchSize)
input := make([]byte, benchSize)
output := make([]byte, benchSize)
b.ResetTimer()
for i := 0; i < b.N; i++ {
c.XORKeyStream(output, input)
}

View File

@@ -3,7 +3,7 @@ package crypto
import (
"crypto/cipher"
"github.com/v2ray/v2ray-core/common/crypto/internal"
"v2ray.com/core/common/crypto/internal"
)
// NewChaCha20Stream creates a new Chacha20 encryption/descryption stream based on give key and IV.

View File

@@ -5,8 +5,8 @@ import (
"encoding/hex"
"testing"
. "github.com/v2ray/v2ray-core/common/crypto"
"github.com/v2ray/v2ray-core/testing/assert"
. "v2ray.com/core/common/crypto"
"v2ray.com/core/testing/assert"
)
func mustDecodeHex(s string) []byte {

3
common/crypto/crypto.go Normal file
View File

@@ -0,0 +1,3 @@
// Provides common crypto libraries for V2Ray.
package crypto

View File

@@ -4,7 +4,7 @@ import (
"crypto/cipher"
"io"
"github.com/v2ray/v2ray-core/common"
"v2ray.com/core/common"
)
type CryptionReader struct {

View File

@@ -4,7 +4,7 @@ import (
"io"
"sync"
"github.com/v2ray/v2ray-core/common/alloc"
"v2ray.com/core/common/alloc"
)
type BufferedReader struct {

View File

@@ -3,9 +3,9 @@ package io_test
import (
"testing"
"github.com/v2ray/v2ray-core/common/alloc"
. "github.com/v2ray/v2ray-core/common/io"
"github.com/v2ray/v2ray-core/testing/assert"
"v2ray.com/core/common/alloc"
. "v2ray.com/core/common/io"
"v2ray.com/core/testing/assert"
)
func TestBufferedReader(t *testing.T) {

View File

@@ -4,7 +4,7 @@ import (
"io"
"sync"
"github.com/v2ray/v2ray-core/common/alloc"
"v2ray.com/core/common/alloc"
)
type BufferedWriter struct {

View File

@@ -3,9 +3,9 @@ package io_test
import (
"testing"
"github.com/v2ray/v2ray-core/common/alloc"
. "github.com/v2ray/v2ray-core/common/io"
"github.com/v2ray/v2ray-core/testing/assert"
"v2ray.com/core/common/alloc"
. "v2ray.com/core/common/io"
"v2ray.com/core/testing/assert"
)
func TestBufferedWriter(t *testing.T) {

View File

@@ -4,7 +4,7 @@ import (
"io"
"sync"
"github.com/v2ray/v2ray-core/common/alloc"
"v2ray.com/core/common/alloc"
)
type ChainWriter struct {

View File

@@ -4,7 +4,7 @@ import (
"io"
"sync"
"github.com/v2ray/v2ray-core/common/alloc"
"v2ray.com/core/common/alloc"
)
type ChanReader struct {
@@ -22,7 +22,7 @@ func NewChanReader(stream Reader) *ChanReader {
return this
}
// @Private
// Private: Visible for testing.
func (this *ChanReader) Fill() {
b, err := this.stream.Read()
this.current = b

View File

@@ -1,10 +1,10 @@
package io // import "github.com/v2ray/v2ray-core/common/io"
package io
import (
"io"
"github.com/v2ray/v2ray-core/common"
"github.com/v2ray/v2ray-core/common/alloc"
"v2ray.com/core/common"
"v2ray.com/core/common/alloc"
)
// Reader extends io.Reader with alloc.Buffer.

View File

@@ -4,9 +4,9 @@ import (
"bytes"
"testing"
"github.com/v2ray/v2ray-core/common/alloc"
. "github.com/v2ray/v2ray-core/common/io"
"github.com/v2ray/v2ray-core/testing/assert"
"v2ray.com/core/common/alloc"
. "v2ray.com/core/common/io"
"v2ray.com/core/testing/assert"
)
func TestAdaptiveReader(t *testing.T) {

View File

@@ -1,7 +1,7 @@
package io
import (
"github.com/v2ray/v2ray-core/common/log"
"v2ray.com/core/common/log"
)
func Pipe(reader Reader, writer Writer) error {

View File

@@ -3,8 +3,8 @@ package io
import (
"io"
"github.com/v2ray/v2ray-core/common"
"github.com/v2ray/v2ray-core/common/alloc"
"v2ray.com/core/common"
"v2ray.com/core/common/alloc"
)
// Writer extends io.Writer with alloc.Buffer.

View File

@@ -5,9 +5,9 @@ import (
"crypto/rand"
"testing"
"github.com/v2ray/v2ray-core/common/alloc"
. "github.com/v2ray/v2ray-core/common/io"
"github.com/v2ray/v2ray-core/testing/assert"
"v2ray.com/core/common/alloc"
. "v2ray.com/core/common/io"
"v2ray.com/core/testing/assert"
)
func TestAdaptiveWriter(t *testing.T) {

View File

@@ -5,25 +5,26 @@ package loader
import (
"encoding/json"
"github.com/v2ray/v2ray-core/common/log"
"v2ray.com/core/common"
"v2ray.com/core/common/log"
)
type JSONConfigLoader struct {
*BaseConfigLoader
cache ConfigCreatorCache
idKey string
configKey string
}
func NewJSONConfigLoader(idKey string, configKey string) *JSONConfigLoader {
func NewJSONConfigLoader(cache ConfigCreatorCache, idKey string, configKey string) *JSONConfigLoader {
return &JSONConfigLoader{
idKey: idKey,
configKey: configKey,
BaseConfigLoader: NewBaseConfigLoader(),
idKey: idKey,
configKey: configKey,
cache: cache,
}
}
func (this *JSONConfigLoader) LoadWithID(raw []byte, id string) (interface{}, error) {
config, err := this.CreateConfig(id)
config, err := this.cache.CreateConfig(id)
if err != nil {
return nil, err
}
@@ -42,7 +43,7 @@ func (this *JSONConfigLoader) Load(raw []byte) (interface{}, string, error) {
rawID, found := obj[this.idKey]
if !found {
log.Error(this.idKey, " not found in JSON content.")
return nil, "", ErrConfigIDKeyNotFound
return nil, "", common.ErrObjectNotFound
}
var id string
if err := json.Unmarshal(rawID, &id); err != nil {
@@ -53,7 +54,7 @@ func (this *JSONConfigLoader) Load(raw []byte) (interface{}, string, error) {
configValue, found := obj[this.configKey]
if !found {
log.Error(this.configKey, " not found in JSON content.")
return nil, "", ErrConfigIDKeyNotFound
return nil, "", common.ErrObjectNotFound
}
rawConfig = configValue
}

View File

@@ -0,0 +1,39 @@
// +build json
package loader_test
import (
"testing"
. "v2ray.com/core/common/loader"
"v2ray.com/core/testing/assert"
)
type TestConfigA struct {
V int
}
type TestConfigB struct {
S string
}
func TestCreatorCache(t *testing.T) {
assert := assert.On(t)
cache := ConfigCreatorCache{}
creator1 := func() interface{} { return &TestConfigA{} }
creator2 := func() interface{} { return &TestConfigB{} }
cache.RegisterCreator("1", creator1)
loader := NewJSONConfigLoader(cache, "test", "")
rawA, err := loader.LoadWithID([]byte(`{"V": 2}`), "1")
assert.Error(err).IsNil()
instA := rawA.(*TestConfigA)
assert.Int(instA.V).Equals(2)
cache.RegisterCreator("2", creator2)
rawB, err := loader.LoadWithID([]byte(`{"S": "a"}`), "2")
assert.Error(err).IsNil()
instB := rawB.(*TestConfigB)
assert.String(instB.S).Equals("a")
}

View File

@@ -2,46 +2,35 @@ package loader
import (
"errors"
"v2ray.com/core/common"
)
var (
ErrConfigIDKeyNotFound = errors.New("Config ID key is not found.")
ErrConfigIDExists = errors.New("Config ID already exists.")
ErrUnknownConfigID = errors.New("Unknown config ID.")
ErrUnknownConfigID = errors.New("Unknown config ID.")
)
type ConfigCreator func() interface{}
type ConfigLoader interface {
RegisterCreator(string, ConfigCreator) error
CreateConfig(string) (interface{}, error)
Load([]byte) (interface{}, string, error)
LoadWithID([]byte, string) (interface{}, error)
}
type ConfigCreatorCache map[string]ConfigCreator
type BaseConfigLoader struct {
creators map[string]ConfigCreator
}
func NewBaseConfigLoader() *BaseConfigLoader {
return &BaseConfigLoader{
creators: make(map[string]ConfigCreator),
}
}
func (this *BaseConfigLoader) RegisterCreator(id string, creator ConfigCreator) error {
if _, found := this.creators[id]; found {
return ErrConfigIDExists
func (this ConfigCreatorCache) RegisterCreator(id string, creator ConfigCreator) error {
if _, found := this[id]; found {
return common.ErrDuplicatedName
}
this.creators[id] = creator
this[id] = creator
return nil
}
func (this *BaseConfigLoader) CreateConfig(id string) (interface{}, error) {
creator, found := this.creators[id]
func (this ConfigCreatorCache) CreateConfig(id string) (interface{}, error) {
creator, found := this[id]
if !found {
return nil, ErrUnknownConfigID
}
return creator(), nil
}
type ConfigLoader interface {
Load([]byte) (interface{}, string, error)
LoadWithID([]byte, string) (interface{}, error)
}

View File

@@ -1,7 +1,7 @@
package log
import (
"github.com/v2ray/v2ray-core/common/log/internal"
"v2ray.com/core/common/log/internal"
)
// AccessStatus is the status of an access request from clients.

View File

@@ -4,8 +4,8 @@ import (
"fmt"
"strings"
"github.com/v2ray/v2ray-core/common"
"github.com/v2ray/v2ray-core/common/serial"
"v2ray.com/core/common"
"v2ray.com/core/common/serial"
)
func InterfaceToString(value interface{}) string {

View File

@@ -3,8 +3,8 @@ package internal_test
import (
"testing"
. "github.com/v2ray/v2ray-core/common/log/internal"
"github.com/v2ray/v2ray-core/testing/assert"
. "v2ray.com/core/common/log/internal"
"v2ray.com/core/testing/assert"
)
func TestAccessLog(t *testing.T) {

View File

@@ -5,8 +5,8 @@ import (
"os"
"time"
"github.com/v2ray/v2ray-core/common/platform"
"github.com/v2ray/v2ray-core/common/signal"
"v2ray.com/core/common/platform"
"v2ray.com/core/common/signal"
)
type LogWriter interface {

View File

@@ -1,7 +1,7 @@
package log
import (
"github.com/v2ray/v2ray-core/common/log/internal"
"v2ray.com/core/common/log/internal"
)
type LogLevel int

View File

@@ -3,8 +3,8 @@ package net
import (
"net"
"github.com/v2ray/v2ray-core/common/log"
"github.com/v2ray/v2ray-core/common/predicate"
"v2ray.com/core/common/log"
"v2ray.com/core/common/predicate"
)
var (
@@ -186,3 +186,16 @@ func (this *domainAddress) Equals(another Address) bool {
}
return this.Domain() == anotherDomain.Domain()
}
func (this *AddressPB) AsAddress() Address {
if this == nil {
return nil
}
switch addr := this.Address.(type) {
case *AddressPB_Ip:
return IPAddress(addr.Ip)
case *AddressPB_Domain:
return DomainAddress(addr.Domain)
}
panic("Common|Net: Invalid AddressPB.")
}

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

@@ -0,0 +1,168 @@
// Code generated by protoc-gen-go.
// source: v2ray.com/core/common/net/address.proto
// DO NOT EDIT!
/*
Package net is a generated protocol buffer package.
It is generated from these files:
v2ray.com/core/common/net/address.proto
v2ray.com/core/common/net/destination.proto
v2ray.com/core/common/net/network.proto
v2ray.com/core/common/net/port.proto
It has these top-level messages:
AddressPB
DestinationPB
NetworkList
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), "v2ray.core.common.net.AddressPB")
}
func init() { proto.RegisterFile("v2ray.com/core/common/net/address.proto", fileDescriptor0) }
var fileDescriptor0 = []byte{
// 159 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0x52, 0x2f, 0x33, 0x2a, 0x4a,
0xac, 0xd4, 0x4b, 0xce, 0xcf, 0xd5, 0x4f, 0xce, 0x2f, 0x4a, 0xd5, 0x4f, 0xce, 0xcf, 0xcd, 0xcd,
0xcf, 0xd3, 0xcf, 0x4b, 0x2d, 0xd1, 0x4f, 0x4c, 0x49, 0x29, 0x4a, 0x2d, 0x2e, 0xd6, 0x2b, 0x28,
0xca, 0x2f, 0xc9, 0x17, 0x12, 0x85, 0x29, 0x2c, 0x4a, 0xd5, 0x83, 0x28, 0xd2, 0xcb, 0x4b, 0x2d,
0x51, 0x72, 0xe2, 0xe2, 0x74, 0x84, 0xa8, 0x0b, 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, 0x16, 0x38, 0xe9, 0x71, 0x49, 0x26, 0xe7, 0xe7, 0xea, 0x61, 0xb5,
0xc0, 0x89, 0x07, 0x66, 0x3c, 0xc8, 0x15, 0x51, 0xcc, 0x79, 0xa9, 0x25, 0x49, 0x6c, 0x60, 0x17,
0x19, 0x03, 0x02, 0x00, 0x00, 0xff, 0xff, 0xa1, 0xd1, 0xe6, 0x14, 0xbc, 0x00, 0x00, 0x00,
}

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

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

View File

@@ -6,15 +6,22 @@ import (
"encoding/json"
)
type AddressJson struct {
Address Address
}
func (this *AddressJson) UnmarshalJSON(data []byte) error {
func (this *AddressPB) UnmarshalJSON(data []byte) error {
var rawStr string
if err := json.Unmarshal(data, &rawStr); err != nil {
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
}

View File

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

View File

@@ -4,8 +4,8 @@ import (
"net"
"testing"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/testing/assert"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/testing/assert"
)
func TestIPv4Address(t *testing.T) {

View File

@@ -5,16 +5,10 @@ import (
)
// Destination represents a network destination including address and protocol (tcp / udp).
type Destination interface {
Network() Network // Protocol of communication (tcp / udp)
Address() Address // Address of destination
Port() Port
String() string // String representation of the destination
NetAddr() string
Equals(Destination) bool
IsTCP() bool // True if destination is reachable via TCP
IsUDP() bool // True if destination is reachable via UDP
type Destination struct {
Network Network
Address Address
Port Port
}
func DestinationFromAddr(addr net.Addr) Destination {
@@ -30,102 +24,38 @@ func DestinationFromAddr(addr net.Addr) Destination {
// TCPDestination creates a TCP destination with given address
func TCPDestination(address Address, port Port) Destination {
return &tcpDestination{address: address, port: port}
return Destination{
Network: Network_TCP,
Address: address,
Port: port,
}
}
// UDPDestination creates a UDP destination with given address
func UDPDestination(address Address, port Port) Destination {
return &udpDestination{address: address, port: port}
}
type tcpDestination struct {
address Address
port Port
}
func (dest *tcpDestination) Network() Network {
return TCPNetwork
}
func (dest *tcpDestination) Address() Address {
return dest.address
}
func (dest *tcpDestination) NetAddr() string {
return dest.address.String() + ":" + dest.port.String()
}
func (dest *tcpDestination) String() string {
return "tcp:" + dest.NetAddr()
}
func (dest *tcpDestination) IsTCP() bool {
return true
}
func (dest *tcpDestination) IsUDP() bool {
return false
}
func (dest *tcpDestination) Port() Port {
return dest.port
}
func (dest *tcpDestination) Equals(another Destination) bool {
if dest == nil && another == nil {
return true
return Destination{
Network: Network_UDP,
Address: address,
Port: port,
}
if dest == nil || another == nil {
return false
}
func (this Destination) NetAddr() string {
return this.Address.String() + ":" + this.Port.String()
}
func (this Destination) String() string {
return this.Network.UrlPrefix() + ":" + this.NetAddr()
}
func (this Destination) Equals(another Destination) bool {
return this.Network == another.Network && this.Port == another.Port && this.Address.Equals(another.Address)
}
func (this *DestinationPB) AsDestination() Destination {
return Destination{
Network: this.Network,
Address: this.Address.AsAddress(),
Port: Port(this.Port),
}
if !another.IsTCP() {
return false
}
return dest.Port() == another.Port() && dest.Address().Equals(another.Address())
}
type udpDestination struct {
address Address
port Port
}
func (dest *udpDestination) Network() Network {
return UDPNetwork
}
func (dest *udpDestination) Address() Address {
return dest.address
}
func (dest *udpDestination) NetAddr() string {
return dest.address.String() + ":" + dest.port.String()
}
func (dest *udpDestination) String() string {
return "udp:" + dest.NetAddr()
}
func (dest *udpDestination) IsTCP() bool {
return false
}
func (dest *udpDestination) IsUDP() bool {
return true
}
func (dest *udpDestination) Port() Port {
return dest.port
}
func (dest *udpDestination) Equals(another Destination) bool {
if dest == nil && another == nil {
return true
}
if dest == nil || another == nil {
return false
}
if !another.IsUDP() {
return false
}
return dest.Port() == another.Port() && dest.Address().Equals(another.Address())
}

View File

@@ -0,0 +1,56 @@
// Code generated by protoc-gen-go.
// source: v2ray.com/core/common/net/destination.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
type DestinationPB struct {
Network Network `protobuf:"varint,1,opt,name=network,enum=v2ray.core.common.net.Network" json:"network,omitempty"`
Address *AddressPB `protobuf:"bytes,2,opt,name=address" json:"address,omitempty"`
Port uint32 `protobuf:"varint,3,opt,name=port" json:"port,omitempty"`
}
func (m *DestinationPB) Reset() { *m = DestinationPB{} }
func (m *DestinationPB) String() string { return proto.CompactTextString(m) }
func (*DestinationPB) ProtoMessage() {}
func (*DestinationPB) Descriptor() ([]byte, []int) { return fileDescriptor1, []int{0} }
func (m *DestinationPB) GetAddress() *AddressPB {
if m != nil {
return m.Address
}
return nil
}
func init() {
proto.RegisterType((*DestinationPB)(nil), "v2ray.core.common.net.DestinationPB")
}
func init() { proto.RegisterFile("v2ray.com/core/common/net/destination.proto", fileDescriptor1) }
var fileDescriptor1 = []byte{
// 209 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0xd2, 0x2e, 0x33, 0x2a, 0x4a,
0xac, 0xd4, 0x4b, 0xce, 0xcf, 0xd5, 0x4f, 0xce, 0x2f, 0x4a, 0xd5, 0x4f, 0xce, 0xcf, 0xcd, 0xcd,
0xcf, 0xd3, 0xcf, 0x4b, 0x2d, 0xd1, 0x4f, 0x49, 0x2d, 0x2e, 0xc9, 0xcc, 0x4b, 0x2c, 0xc9, 0xcc,
0xcf, 0xd3, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x12, 0x85, 0x29, 0x2e, 0x4a, 0xd5, 0x83, 0x28,
0xd4, 0xcb, 0x4b, 0x2d, 0x91, 0x52, 0xc7, 0x6d, 0x46, 0x5e, 0x6a, 0x49, 0x79, 0x7e, 0x51, 0x36,
0x44, 0x3f, 0x3e, 0x85, 0x89, 0x29, 0x29, 0x45, 0xa9, 0xc5, 0xc5, 0x10, 0x85, 0x4a, 0x33, 0x19,
0xb9, 0x78, 0x5d, 0x10, 0xd6, 0x07, 0x38, 0x09, 0x59, 0x70, 0xb1, 0x43, 0xcd, 0x92, 0x60, 0x54,
0x60, 0xd4, 0xe0, 0x33, 0x92, 0xd3, 0xc3, 0xea, 0x18, 0x3d, 0x3f, 0x88, 0xaa, 0x20, 0x98, 0x72,
0x21, 0x2b, 0x2e, 0x76, 0xa8, 0xe1, 0x12, 0x4c, 0x0a, 0x8c, 0x1a, 0xdc, 0x46, 0x0a, 0x38, 0x74,
0x3a, 0x42, 0x54, 0x05, 0x38, 0x05, 0xc1, 0x34, 0x08, 0x09, 0x71, 0xb1, 0x14, 0xe4, 0x17, 0x95,
0x48, 0x30, 0x2b, 0x30, 0x6a, 0xf0, 0x06, 0x81, 0xd9, 0x4e, 0x46, 0x5c, 0x92, 0xc9, 0xf9, 0xb9,
0xd8, 0xcd, 0x70, 0x12, 0x40, 0x76, 0x35, 0xc8, 0x2b, 0x51, 0xcc, 0x79, 0xa9, 0x25, 0x49, 0x6c,
0x60, 0x6f, 0x19, 0x03, 0x02, 0x00, 0x00, 0xff, 0xff, 0xe2, 0xd7, 0x78, 0x9a, 0x6e, 0x01, 0x00,
0x00,
}

View File

@@ -0,0 +1,15 @@
syntax = "proto3";
package v2ray.core.common.net;
option go_package = "net";
option java_package = "com.v2ray.core.common.net";
option java_outer_classname = "DestinationProto";
import "v2ray.com/core/common/net/network.proto";
import "v2ray.com/core/common/net/address.proto";
message DestinationPB {
Network network = 1;
AddressPB address = 2;
uint32 port = 3;
}

View File

@@ -3,8 +3,8 @@ package net_test
import (
"testing"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/testing/assert"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/testing/assert"
)
func TestTCPDestination(t *testing.T) {
@@ -29,7 +29,7 @@ func TestTCPDestinationEquals(t *testing.T) {
assert := assert.On(t)
dest := v2net.TCPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}), 80)
assert.Bool(dest.Equals(nil)).IsFalse()
assert.Bool(dest.Equals(v2net.Destination{})).IsFalse()
dest2 := v2net.TCPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}), 80)
assert.Bool(dest.Equals(dest2)).IsTrue()
@@ -45,7 +45,7 @@ func TestUDPDestinationEquals(t *testing.T) {
assert := assert.On(t)
dest := v2net.UDPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}), 80)
assert.Bool(dest.Equals(nil)).IsFalse()
assert.Bool(dest.Equals(v2net.Destination{})).IsFalse()
dest2 := v2net.UDPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}), 80)
assert.Bool(dest.Equals(dest2)).IsTrue()

View File

@@ -4,8 +4,8 @@ import (
"net"
"testing"
. "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/testing/assert"
. "v2ray.com/core/common/net"
"v2ray.com/core/testing/assert"
)
func parseCIDR(str string) *net.IPNet {

View File

@@ -1,2 +1,2 @@
// Package net contains common network utilities.
package net // import "github.com/v2ray/v2ray-core/common/net"
package net

View File

@@ -3,53 +3,80 @@ package net
import (
"strings"
"github.com/v2ray/v2ray-core/common/collect"
"v2ray.com/core/common/collect"
)
const (
// TCPNetwork represents the TCP network.
TCPNetwork = Network("tcp")
// UDPNetwork represents the UDP network.
UDPNetwork = Network("udp")
// KCPNetwork represents the KCP network.
KCPNetwork = Network("kcp")
// WSNetwork represents the Websocket over HTTP network.
WSNetwork = Network("ws")
)
// Network represents a communication network on internet.
type Network string
func ParseNetwork(nwStr string) Network {
if network, found := Network_value[nwStr]; found {
return Network(network)
}
switch strings.ToLower(nwStr) {
case "tcp":
return Network_TCP
case "udp":
return Network_UDP
case "kcp":
return Network_KCP
case "ws":
return Network_WebSocket
default:
return Network_Unknown
}
}
func (this Network) AsList() *NetworkList {
list := NetworkList([]Network{this})
return &list
return &NetworkList{
Network: []Network{this},
}
}
func (this Network) String() string {
return string(this)
func (this Network) SystemString() string {
switch this {
case Network_TCP, Network_RawTCP:
return "tcp"
case Network_UDP, Network_KCP:
return "udp"
default:
return "unknown"
}
}
// NetworkList is a list of Networks.
type NetworkList []Network
func (this Network) UrlPrefix() string {
switch this {
case Network_TCP, Network_RawTCP:
return "tcp"
case Network_UDP:
return "udp"
case Network_KCP:
return "kcp"
case Network_WebSocket:
return "ws"
default:
return "unknown"
}
}
// NewNetworkList construsts a NetWorklist from the given StringListeralList.
func NewNetworkList(networks collect.StringList) NetworkList {
list := NetworkList(make([]Network, networks.Len()))
func NewNetworkList(networks collect.StringList) *NetworkList {
list := &NetworkList{
Network: make([]Network, networks.Len()),
}
for idx, network := range networks {
list[idx] = Network(strings.ToLower(strings.TrimSpace(network)))
list.Network[idx] = ParseNetwork(network)
}
return list
}
// HashNetwork returns true if the given network is in this NetworkList.
func (this *NetworkList) HasNetwork(network Network) bool {
for _, value := range *this {
func (this NetworkList) HasNetwork(network Network) bool {
for _, value := range this.Network {
if string(value) == string(network) {
return true
}
}
return false
}
func (this NetworkList) Get(idx int) Network {
return this.Network[idx]
}

80
common/net/network.pb.go Normal file
View File

@@ -0,0 +1,80 @@
// Code generated by protoc-gen-go.
// source: v2ray.com/core/common/net/network.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
type Network int32
const (
Network_Unknown Network = 0
Network_RawTCP Network = 1
Network_TCP Network = 2
Network_UDP Network = 3
Network_KCP Network = 4
Network_WebSocket Network = 5
)
var Network_name = map[int32]string{
0: "Unknown",
1: "RawTCP",
2: "TCP",
3: "UDP",
4: "KCP",
5: "WebSocket",
}
var Network_value = map[string]int32{
"Unknown": 0,
"RawTCP": 1,
"TCP": 2,
"UDP": 3,
"KCP": 4,
"WebSocket": 5,
}
func (x Network) String() string {
return proto.EnumName(Network_name, int32(x))
}
func (Network) EnumDescriptor() ([]byte, []int) { return fileDescriptor2, []int{0} }
type NetworkList struct {
Network []Network `protobuf:"varint,1,rep,packed,name=network,enum=v2ray.core.common.net.Network" json:"network,omitempty"`
}
func (m *NetworkList) Reset() { *m = NetworkList{} }
func (m *NetworkList) String() string { return proto.CompactTextString(m) }
func (*NetworkList) ProtoMessage() {}
func (*NetworkList) Descriptor() ([]byte, []int) { return fileDescriptor2, []int{0} }
func init() {
proto.RegisterType((*NetworkList)(nil), "v2ray.core.common.net.NetworkList")
proto.RegisterEnum("v2ray.core.common.net.Network", Network_name, Network_value)
}
func init() { proto.RegisterFile("v2ray.com/core/common/net/network.proto", fileDescriptor2) }
var fileDescriptor2 = []byte{
// 203 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0x52, 0x2f, 0x33, 0x2a, 0x4a,
0xac, 0xd4, 0x4b, 0xce, 0xcf, 0xd5, 0x4f, 0xce, 0x2f, 0x4a, 0xd5, 0x4f, 0xce, 0xcf, 0xcd, 0xcd,
0xcf, 0xd3, 0xcf, 0x4b, 0x2d, 0x01, 0xe1, 0xf2, 0xfc, 0xa2, 0x6c, 0xbd, 0x82, 0xa2, 0xfc, 0x92,
0x7c, 0x21, 0x51, 0x98, 0xc2, 0xa2, 0x54, 0x3d, 0x88, 0x22, 0xbd, 0xbc, 0xd4, 0x12, 0x25, 0x77,
0x2e, 0x6e, 0x3f, 0x88, 0x3a, 0x9f, 0xcc, 0xe2, 0x12, 0x21, 0x0b, 0x2e, 0x76, 0xa8, 0x36, 0x09,
0x46, 0x05, 0x66, 0x0d, 0x3e, 0x23, 0x39, 0x3d, 0xac, 0xfa, 0xf4, 0xa0, 0x9a, 0x82, 0x60, 0xca,
0xb5, 0x7c, 0xb8, 0xd8, 0xa1, 0x62, 0x42, 0xdc, 0x5c, 0xec, 0xa1, 0x79, 0xd9, 0x79, 0xf9, 0xe5,
0x79, 0x02, 0x0c, 0x42, 0x5c, 0x5c, 0x6c, 0x41, 0x89, 0xe5, 0x21, 0xce, 0x01, 0x02, 0x8c, 0x42,
0xec, 0x5c, 0xcc, 0x20, 0x06, 0x13, 0x88, 0x11, 0xea, 0x12, 0x20, 0xc0, 0x0c, 0x62, 0x78, 0x3b,
0x07, 0x08, 0xb0, 0x08, 0xf1, 0x72, 0x71, 0x86, 0xa7, 0x26, 0x05, 0xe7, 0x27, 0x67, 0xa7, 0x96,
0x08, 0xb0, 0x3a, 0xe9, 0x71, 0x49, 0x26, 0xe7, 0xe7, 0x62, 0xb7, 0xdb, 0x89, 0x07, 0x6a, 0x51,
0x00, 0xc8, 0x63, 0x51, 0xcc, 0x79, 0xa9, 0x25, 0x49, 0x6c, 0x60, 0x4f, 0x1a, 0x03, 0x02, 0x00,
0x00, 0xff, 0xff, 0x6e, 0x52, 0xf1, 0x92, 0x0f, 0x01, 0x00, 0x00,
}

19
common/net/network.proto Normal file
View File

@@ -0,0 +1,19 @@
syntax = "proto3";
package v2ray.core.common.net;
option go_package = "net";
option java_package = "com.v2ray.core.common.net";
option java_outer_classname = "NetworkProto";
enum Network {
Unknown = 0;
RawTCP = 1;
TCP = 2;
UDP = 3;
KCP = 4;
WebSocket = 5;
}
message NetworkList {
repeated Network network = 1;
}

View File

@@ -5,14 +5,23 @@ package net
import (
"encoding/json"
"github.com/v2ray/v2ray-core/common/collect"
"v2ray.com/core/common/collect"
)
func (this *Network) UnmarshalJSON(data []byte) error {
var str string
if err := json.Unmarshal(data, &str); err != nil {
return err
}
*this = ParseNetwork(str)
return nil
}
func (this *NetworkList) UnmarshalJSON(data []byte) error {
var strlist collect.StringList
if err := json.Unmarshal(data, &strlist); err != nil {
return err
}
*this = NewNetworkList(strlist)
*this = *NewNetworkList(strlist)
return nil
}

View File

@@ -6,18 +6,27 @@ import (
"encoding/json"
"testing"
. "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/testing/assert"
. "v2ray.com/core/common/net"
"v2ray.com/core/testing/assert"
)
func TestStringNetwork(t *testing.T) {
assert := assert.On(t)
var network Network
err := json.Unmarshal([]byte(`"tcp"`), &network)
assert.Error(err).IsNil()
assert.Bool(network == Network_TCP).IsTrue()
}
func TestArrayNetworkList(t *testing.T) {
assert := assert.On(t)
var list NetworkList
err := json.Unmarshal([]byte("[\"Tcp\"]"), &list)
assert.Error(err).IsNil()
assert.Bool(list.HasNetwork(Network("tcp"))).IsTrue()
assert.Bool(list.HasNetwork(Network("udp"))).IsFalse()
assert.Bool(list.HasNetwork(ParseNetwork("tcp"))).IsTrue()
assert.Bool(list.HasNetwork(ParseNetwork("udp"))).IsFalse()
}
func TestStringNetworkList(t *testing.T) {
@@ -26,8 +35,8 @@ func TestStringNetworkList(t *testing.T) {
var list NetworkList
err := json.Unmarshal([]byte("\"TCP, ip\""), &list)
assert.Error(err).IsNil()
assert.Bool(list.HasNetwork(Network("tcp"))).IsTrue()
assert.Bool(list.HasNetwork(Network("udp"))).IsFalse()
assert.Bool(list.HasNetwork(ParseNetwork("tcp"))).IsTrue()
assert.Bool(list.HasNetwork(ParseNetwork("udp"))).IsFalse()
}
func TestInvalidNetworkJson(t *testing.T) {

View File

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

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

@@ -0,0 +1,45 @@
// Code generated by protoc-gen-go.
// source: v2ray.com/core/common/net/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 fileDescriptor3, []int{0} }
func init() {
proto.RegisterType((*PortRange)(nil), "v2ray.core.common.net.PortRange")
}
func init() { proto.RegisterFile("v2ray.com/core/common/net/port.proto", fileDescriptor3) }
var fileDescriptor3 = []byte{
// 148 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0x52, 0x29, 0x33, 0x2a, 0x4a,
0xac, 0xd4, 0x4b, 0xce, 0xcf, 0xd5, 0x4f, 0xce, 0x2f, 0x4a, 0xd5, 0x4f, 0xce, 0xcf, 0xcd, 0xcd,
0xcf, 0xd3, 0xcf, 0x4b, 0x2d, 0xd1, 0x2f, 0xc8, 0x2f, 0x2a, 0xd1, 0x2b, 0x28, 0xca, 0x2f, 0xc9,
0x17, 0x12, 0x85, 0xa9, 0x2a, 0x4a, 0xd5, 0x83, 0xa8, 0xd0, 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, 0x69, 0x73, 0x49, 0x26,
0xe7, 0xe7, 0xea, 0x61, 0x35, 0xcd, 0x09, 0x6c, 0x56, 0x00, 0xc8, 0xbe, 0x28, 0xe6, 0xbc, 0xd4,
0x92, 0x24, 0x36, 0xb0, 0xdd, 0xc6, 0x80, 0x00, 0x00, 0x00, 0xff, 0xff, 0x28, 0x7b, 0x9b, 0xb3,
0xa3, 0x00, 0x00, 0x00,
}

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

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

View File

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

View File

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

View File

@@ -3,16 +3,16 @@ package net_test
import (
"testing"
. "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/testing/assert"
. "v2ray.com/core/common/net"
"v2ray.com/core/testing/assert"
)
func TestPortRangeContains(t *testing.T) {
assert := assert.On(t)
portRange := &PortRange{
From: Port(53),
To: Port(53),
From: 53,
To: 53,
}
assert.Bool(portRange.Contains(Port(53))).IsTrue()
}

View File

@@ -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
@@ -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
@@ -56,7 +56,7 @@ func (reader *TimeOutReader) Release() {
}
type timedReaderWorker struct {
timeout int
timeout uint32
connection net.Conn
}

View File

@@ -3,17 +3,17 @@ package net_test
import (
"testing"
. "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/testing/assert"
. "v2ray.com/core/common/net"
"v2ray.com/core/testing/assert"
)
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)
}

View File

@@ -3,3 +3,9 @@ package protocol
type Account interface {
Equals(Account) bool
}
type AsAccount interface {
AsAccount() (Account, error)
}
type NewAccountFactory func() AsAccount

View File

@@ -1,8 +1,8 @@
package protocol
import (
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/common/uuid"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/uuid"
)
type RequestCommand byte
@@ -77,6 +77,6 @@ type CommandSwitchAccount struct {
Port v2net.Port
ID *uuid.UUID
AlterIds uint16
Level UserLevel
Level uint32
ValidMin byte
}

View File

@@ -3,8 +3,8 @@ package protocol_test
import (
"testing"
. "github.com/v2ray/v2ray-core/common/protocol"
"github.com/v2ray/v2ray-core/testing/assert"
. "v2ray.com/core/common/protocol"
"v2ray.com/core/testing/assert"
)
func TestRequestOptionSet(t *testing.T) {

View File

@@ -6,7 +6,7 @@ import (
"errors"
"hash"
"github.com/v2ray/v2ray-core/common/uuid"
"v2ray.com/core/common/uuid"
)
const (

View File

@@ -3,10 +3,10 @@ package protocol_test
import (
"testing"
"github.com/v2ray/v2ray-core/common/predicate"
. "github.com/v2ray/v2ray-core/common/protocol"
"github.com/v2ray/v2ray-core/common/uuid"
"github.com/v2ray/v2ray-core/testing/assert"
"v2ray.com/core/common/predicate"
. "v2ray.com/core/common/protocol"
"v2ray.com/core/common/uuid"
"v2ray.com/core/testing/assert"
)
func TestCmdKey(t *testing.T) {

View File

@@ -46,7 +46,7 @@ func (this *ServerList) GetServer(idx uint32) *ServerSpec {
}
}
// @Private
// Private: Visible for testing.
func (this *ServerList) RemoveServer(idx uint32) {
n := len(this.servers)
this.servers[idx] = this.servers[n-1]

View File

@@ -4,51 +4,51 @@ import (
"testing"
"time"
v2net "github.com/v2ray/v2ray-core/common/net"
. "github.com/v2ray/v2ray-core/common/protocol"
"github.com/v2ray/v2ray-core/testing/assert"
v2net "v2ray.com/core/common/net"
. "v2ray.com/core/common/protocol"
"v2ray.com/core/testing/assert"
)
func TestServerList(t *testing.T) {
assert := assert.On(t)
list := NewServerList()
list.AddServer(NewServerSpec(v2net.TCPDestination(v2net.LocalHostIP, v2net.Port(1)), AlwaysValid()))
list.AddServer(NewServerSpec(nil, v2net.TCPDestination(v2net.LocalHostIP, v2net.Port(1)), AlwaysValid()))
assert.Uint32(list.Size()).Equals(1)
list.AddServer(NewServerSpec(v2net.TCPDestination(v2net.LocalHostIP, v2net.Port(2)), BeforeTime(time.Now().Add(time.Second))))
list.AddServer(NewServerSpec(nil, v2net.TCPDestination(v2net.LocalHostIP, v2net.Port(2)), BeforeTime(time.Now().Add(time.Second))))
assert.Uint32(list.Size()).Equals(2)
server := list.GetServer(1)
assert.Port(server.Destination().Port()).Equals(2)
assert.Port(server.Destination().Port).Equals(2)
time.Sleep(2 * time.Second)
server = list.GetServer(1)
assert.Pointer(server).IsNil()
server = list.GetServer(0)
assert.Port(server.Destination().Port()).Equals(1)
assert.Port(server.Destination().Port).Equals(1)
}
func TestServerPicker(t *testing.T) {
assert := assert.On(t)
list := NewServerList()
list.AddServer(NewServerSpec(v2net.TCPDestination(v2net.LocalHostIP, v2net.Port(1)), AlwaysValid()))
list.AddServer(NewServerSpec(v2net.TCPDestination(v2net.LocalHostIP, v2net.Port(2)), BeforeTime(time.Now().Add(time.Second))))
list.AddServer(NewServerSpec(v2net.TCPDestination(v2net.LocalHostIP, v2net.Port(3)), BeforeTime(time.Now().Add(time.Second))))
list.AddServer(NewServerSpec(nil, v2net.TCPDestination(v2net.LocalHostIP, v2net.Port(1)), AlwaysValid()))
list.AddServer(NewServerSpec(nil, v2net.TCPDestination(v2net.LocalHostIP, v2net.Port(2)), BeforeTime(time.Now().Add(time.Second))))
list.AddServer(NewServerSpec(nil, v2net.TCPDestination(v2net.LocalHostIP, v2net.Port(3)), BeforeTime(time.Now().Add(time.Second))))
picker := NewRoundRobinServerPicker(list)
server := picker.PickServer()
assert.Port(server.Destination().Port()).Equals(1)
assert.Port(server.Destination().Port).Equals(1)
server = picker.PickServer()
assert.Port(server.Destination().Port()).Equals(2)
assert.Port(server.Destination().Port).Equals(2)
server = picker.PickServer()
assert.Port(server.Destination().Port()).Equals(3)
assert.Port(server.Destination().Port).Equals(3)
server = picker.PickServer()
assert.Port(server.Destination().Port()).Equals(1)
assert.Port(server.Destination().Port).Equals(1)
time.Sleep(2 * time.Second)
server = picker.PickServer()
assert.Port(server.Destination().Port()).Equals(1)
assert.Port(server.Destination().Port).Equals(1)
server = picker.PickServer()
assert.Port(server.Destination().Port()).Equals(1)
assert.Port(server.Destination().Port).Equals(1)
}

View File

@@ -4,8 +4,8 @@ import (
"sync"
"time"
"github.com/v2ray/v2ray-core/common/dice"
v2net "github.com/v2ray/v2ray-core/common/net"
"v2ray.com/core/common/dice"
v2net "v2ray.com/core/common/net"
)
type ValidationStrategy interface {
@@ -45,19 +45,26 @@ func (this *TimeoutValidStrategy) Invalidate() {
type ServerSpec struct {
sync.RWMutex
dest v2net.Destination
users []*User
valid ValidationStrategy
dest v2net.Destination
users []*User
valid ValidationStrategy
newAccount NewAccountFactory
}
func NewServerSpec(dest v2net.Destination, valid ValidationStrategy, users ...*User) *ServerSpec {
func NewServerSpec(newAccount NewAccountFactory, dest v2net.Destination, valid ValidationStrategy, users ...*User) *ServerSpec {
return &ServerSpec{
dest: dest,
users: users,
valid: valid,
dest: dest,
users: users,
valid: valid,
newAccount: newAccount,
}
}
func NewServerSpecFromPB(newAccount NewAccountFactory, spec ServerSpecPB) *ServerSpec {
dest := v2net.TCPDestination(spec.Address.AsAddress(), v2net.Port(spec.Port))
return NewServerSpec(newAccount, dest, AlwaysValid(), spec.User...)
}
func (this *ServerSpec) Destination() v2net.Destination {
return this.dest
}
@@ -66,9 +73,13 @@ func (this *ServerSpec) HasUser(user *User) bool {
this.RLock()
defer this.RUnlock()
account := user.Account
accountA, err := user.GetTypedAccount(this.newAccount())
if err != nil {
return false
}
for _, u := range this.users {
if u.Account.Equals(account) {
accountB, err := u.GetTypedAccount(this.newAccount())
if err == nil && accountA.Equals(accountB) {
return true
}
}

Some files were not shown because too many files have changed in this diff Show More