1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-30 05:56:54 -05:00

format code

This commit is contained in:
V2Ray 2015-10-31 14:08:13 +01:00
parent 3765826602
commit 1c09b70931
10 changed files with 65 additions and 60 deletions

View File

@ -16,6 +16,11 @@ type LogConfig interface {
AccessLog() string
}
type InboundDetour interface {
Protocol() string
Port()
}
type PointConfig interface {
Port() uint16
LogConfig() LogConfig

View File

@ -1,7 +1,7 @@
package net
import (
"io"
"io"
"net"
"time"
)
@ -11,17 +11,17 @@ var (
)
type TimeOutReader struct {
timeout int
timeout int
connection net.Conn
worker io.Reader
worker io.Reader
}
func NewTimeOutReader(timeout int, connection net.Conn) *TimeOutReader {
reader := &TimeOutReader{
connection: connection,
}
reader.SetTimeOut(timeout)
return reader
reader.SetTimeOut(timeout)
return reader
}
func (reader *TimeOutReader) Read(p []byte) (int, error) {
@ -33,36 +33,36 @@ func (reader *TimeOutReader) GetTimeOut() int {
}
func (reader *TimeOutReader) SetTimeOut(value int) {
reader.timeout = value
if value > 0 {
reader.worker = &timedReaderWorker{
timeout: value,
connection: reader.connection,
}
} else {
reader.worker = &noOpReaderWorker{
connection: reader.connection,
}
}
reader.timeout = value
if value > 0 {
reader.worker = &timedReaderWorker{
timeout: value,
connection: reader.connection,
}
} else {
reader.worker = &noOpReaderWorker{
connection: reader.connection,
}
}
}
type timedReaderWorker struct {
timeout int
connection net.Conn
timeout int
connection net.Conn
}
func (this *timedReaderWorker) Read(p []byte) (int, error) {
deadline := time.Duration(this.timeout) * time.Second
deadline := time.Duration(this.timeout) * time.Second
this.connection.SetReadDeadline(time.Now().Add(deadline))
nBytes, err := this.connection.Read(p)
this.connection.SetReadDeadline(emptyTime)
return nBytes, err
nBytes, err := this.connection.Read(p)
this.connection.SetReadDeadline(emptyTime)
return nBytes, err
}
type noOpReaderWorker struct {
connection net.Conn
connection net.Conn
}
func (this *noOpReaderWorker) Read(p []byte) (int, error) {
return this.connection.Read(p)
return this.connection.Read(p)
}

View File

@ -8,16 +8,16 @@ import (
// ConfigUser is an user account in VMess configuration.
type ConfigUser struct {
Id *config.ID
Email string
LevelValue config.UserLevel
Id *config.ID
Email string
LevelValue config.UserLevel
}
func (u *ConfigUser) UnmarshalJSON(data []byte) error {
type rawUser struct {
IdString string `json:"id"`
EmailString string `json:"email"`
LevelInt int `json:"level"`
LevelInt int `json:"level"`
}
var rawUserValue rawUser
if err := json.Unmarshal(data, &rawUserValue); err != nil {
@ -29,7 +29,7 @@ func (u *ConfigUser) UnmarshalJSON(data []byte) error {
}
u.Id = id
u.Email = rawUserValue.EmailString
u.LevelValue = config.UserLevel(rawUserValue.LevelInt)
u.LevelValue = config.UserLevel(rawUserValue.LevelInt)
return nil
}
@ -38,5 +38,5 @@ func (u *ConfigUser) ID() *config.ID {
}
func (this *ConfigUser) Level() config.UserLevel {
return this.LevelValue
return this.LevelValue
}

View File

@ -3,25 +3,25 @@ package config
type UserLevel int
const (
UserLevelAdmin = UserLevel(999)
UserLevelUntrusted = UserLevel(0)
UserLevelAdmin = UserLevel(999)
UserLevelUntrusted = UserLevel(0)
)
type User interface {
ID() *ID
Level() UserLevel
Level() UserLevel
}
type UserSettings struct {
PayloadReadTimeout int
PayloadReadTimeout int
}
func GetUserSettings(level UserLevel) UserSettings {
settings := UserSettings {
PayloadReadTimeout: 120,
}
if level > 0 {
settings.PayloadReadTimeout = 0
}
return settings
settings := UserSettings{
PayloadReadTimeout: 120,
}
if level > 0 {
settings.PayloadReadTimeout = 0
}
return settings
}

View File

@ -19,7 +19,7 @@ type UserSet interface {
}
type TimedUserSet struct {
validUsers []config.User
validUsers []config.User
userHash map[string]indexTimePair
userHashDeleteQueue *collect.TimedQueue
access sync.RWMutex
@ -32,7 +32,7 @@ type indexTimePair struct {
func NewTimedUserSet() UserSet {
tus := &TimedUserSet{
validUsers: make([]config.User, 0, 16),
validUsers: make([]config.User, 0, 16),
userHash: make(map[string]indexTimePair, 512),
userHashDeleteQueue: collect.NewTimedQueue(updateIntervalSec),
access: sync.RWMutex{},

View File

@ -37,7 +37,7 @@ const (
// streaming.
type VMessRequest struct {
Version byte
User config.User
User config.User
RequestIV []byte
RequestKey []byte
ResponseHeader []byte
@ -98,7 +98,7 @@ func (r *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
bufferLen := nBytes
request := &VMessRequest{
User: userObj,
User: userObj,
Version: buffer.Value[0],
}

View File

@ -13,8 +13,8 @@ import (
)
type TestUser struct {
id *config.ID
level config.UserLevel
id *config.ID
level config.UserLevel
}
func (u *TestUser) ID() *config.ID {
@ -22,7 +22,7 @@ func (u *TestUser) ID() *config.ID {
}
func (this *TestUser) Level() config.UserLevel {
return this.level
return this.level
}
func TestVMessSerialization(t *testing.T) {
@ -33,9 +33,9 @@ func TestVMessSerialization(t *testing.T) {
t.Fatal(err)
}
testUser := &TestUser {
id: userId,
}
testUser := &TestUser{
id: userId,
}
userSet := mocks.MockUserSet{[]config.User{}, make(map[string]int), make(map[string]int64)}
userSet.AddUser(testUser)
@ -83,9 +83,9 @@ func BenchmarkVMessRequestWriting(b *testing.B) {
userId, _ := config.NewID("2b2966ac-16aa-4fbf-8d81-c5f172a3da51")
userSet := mocks.MockUserSet{[]config.User{}, make(map[string]int), make(map[string]int64)}
testUser := &TestUser{
id: userId,
}
testUser := &TestUser{
id: userId,
}
userSet.AddUser(testUser)
request := new(VMessRequest)

View File

@ -91,7 +91,7 @@ func (handler *VMessInboundHandler) HandleConnection(connection *net.TCPConn) er
readFinish.Lock()
writeFinish.Lock()
userSettings := config.GetUserSettings(request.User.Level())
userSettings := config.GetUserSettings(request.User.Level())
connReader.SetTimeOut(userSettings.PayloadReadTimeout)
go handleInput(request, connReader, input, &readFinish)

View File

@ -67,7 +67,7 @@ func (handler *VMessOutboundHandler) Dispatch(firstPacket v2net.Packet, ray ray.
}
request := &protocol.VMessRequest{
Version: protocol.Version,
User: vNextUser,
User: vNextUser,
Command: command,
Address: firstPacket.Destination().Address(),
}

View File

@ -5,7 +5,7 @@ import (
)
type MockUserSet struct {
Users []config.User
Users []config.User
UserHashes map[string]int
Timestamps map[string]int64
}