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:
parent
3765826602
commit
1c09b70931
@ -16,6 +16,11 @@ type LogConfig interface {
|
|||||||
AccessLog() string
|
AccessLog() string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type InboundDetour interface {
|
||||||
|
Protocol() string
|
||||||
|
Port()
|
||||||
|
}
|
||||||
|
|
||||||
type PointConfig interface {
|
type PointConfig interface {
|
||||||
Port() uint16
|
Port() uint16
|
||||||
LogConfig() LogConfig
|
LogConfig() LogConfig
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package net
|
package net
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -11,17 +11,17 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type TimeOutReader struct {
|
type TimeOutReader struct {
|
||||||
timeout int
|
timeout int
|
||||||
connection net.Conn
|
connection net.Conn
|
||||||
worker io.Reader
|
worker io.Reader
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTimeOutReader(timeout int, connection net.Conn) *TimeOutReader {
|
func NewTimeOutReader(timeout int, connection net.Conn) *TimeOutReader {
|
||||||
reader := &TimeOutReader{
|
reader := &TimeOutReader{
|
||||||
connection: connection,
|
connection: connection,
|
||||||
}
|
}
|
||||||
reader.SetTimeOut(timeout)
|
reader.SetTimeOut(timeout)
|
||||||
return reader
|
return reader
|
||||||
}
|
}
|
||||||
|
|
||||||
func (reader *TimeOutReader) Read(p []byte) (int, error) {
|
func (reader *TimeOutReader) Read(p []byte) (int, error) {
|
||||||
@ -33,36 +33,36 @@ func (reader *TimeOutReader) GetTimeOut() int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (reader *TimeOutReader) SetTimeOut(value int) {
|
func (reader *TimeOutReader) SetTimeOut(value int) {
|
||||||
reader.timeout = value
|
reader.timeout = value
|
||||||
if value > 0 {
|
if value > 0 {
|
||||||
reader.worker = &timedReaderWorker{
|
reader.worker = &timedReaderWorker{
|
||||||
timeout: value,
|
timeout: value,
|
||||||
connection: reader.connection,
|
connection: reader.connection,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
reader.worker = &noOpReaderWorker{
|
reader.worker = &noOpReaderWorker{
|
||||||
connection: reader.connection,
|
connection: reader.connection,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type timedReaderWorker struct {
|
type timedReaderWorker struct {
|
||||||
timeout int
|
timeout int
|
||||||
connection net.Conn
|
connection net.Conn
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *timedReaderWorker) Read(p []byte) (int, error) {
|
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))
|
this.connection.SetReadDeadline(time.Now().Add(deadline))
|
||||||
nBytes, err := this.connection.Read(p)
|
nBytes, err := this.connection.Read(p)
|
||||||
this.connection.SetReadDeadline(emptyTime)
|
this.connection.SetReadDeadline(emptyTime)
|
||||||
return nBytes, err
|
return nBytes, err
|
||||||
}
|
}
|
||||||
|
|
||||||
type noOpReaderWorker struct {
|
type noOpReaderWorker struct {
|
||||||
connection net.Conn
|
connection net.Conn
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *noOpReaderWorker) Read(p []byte) (int, error) {
|
func (this *noOpReaderWorker) Read(p []byte) (int, error) {
|
||||||
return this.connection.Read(p)
|
return this.connection.Read(p)
|
||||||
}
|
}
|
||||||
|
@ -8,16 +8,16 @@ import (
|
|||||||
|
|
||||||
// ConfigUser is an user account in VMess configuration.
|
// ConfigUser is an user account in VMess configuration.
|
||||||
type ConfigUser struct {
|
type ConfigUser struct {
|
||||||
Id *config.ID
|
Id *config.ID
|
||||||
Email string
|
Email string
|
||||||
LevelValue config.UserLevel
|
LevelValue config.UserLevel
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *ConfigUser) UnmarshalJSON(data []byte) error {
|
func (u *ConfigUser) UnmarshalJSON(data []byte) error {
|
||||||
type rawUser struct {
|
type rawUser struct {
|
||||||
IdString string `json:"id"`
|
IdString string `json:"id"`
|
||||||
EmailString string `json:"email"`
|
EmailString string `json:"email"`
|
||||||
LevelInt int `json:"level"`
|
LevelInt int `json:"level"`
|
||||||
}
|
}
|
||||||
var rawUserValue rawUser
|
var rawUserValue rawUser
|
||||||
if err := json.Unmarshal(data, &rawUserValue); err != nil {
|
if err := json.Unmarshal(data, &rawUserValue); err != nil {
|
||||||
@ -29,7 +29,7 @@ func (u *ConfigUser) UnmarshalJSON(data []byte) error {
|
|||||||
}
|
}
|
||||||
u.Id = id
|
u.Id = id
|
||||||
u.Email = rawUserValue.EmailString
|
u.Email = rawUserValue.EmailString
|
||||||
u.LevelValue = config.UserLevel(rawUserValue.LevelInt)
|
u.LevelValue = config.UserLevel(rawUserValue.LevelInt)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,5 +38,5 @@ func (u *ConfigUser) ID() *config.ID {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *ConfigUser) Level() config.UserLevel {
|
func (this *ConfigUser) Level() config.UserLevel {
|
||||||
return this.LevelValue
|
return this.LevelValue
|
||||||
}
|
}
|
||||||
|
@ -3,25 +3,25 @@ package config
|
|||||||
type UserLevel int
|
type UserLevel int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
UserLevelAdmin = UserLevel(999)
|
UserLevelAdmin = UserLevel(999)
|
||||||
UserLevelUntrusted = UserLevel(0)
|
UserLevelUntrusted = UserLevel(0)
|
||||||
)
|
)
|
||||||
|
|
||||||
type User interface {
|
type User interface {
|
||||||
ID() *ID
|
ID() *ID
|
||||||
Level() UserLevel
|
Level() UserLevel
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserSettings struct {
|
type UserSettings struct {
|
||||||
PayloadReadTimeout int
|
PayloadReadTimeout int
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetUserSettings(level UserLevel) UserSettings {
|
func GetUserSettings(level UserLevel) UserSettings {
|
||||||
settings := UserSettings {
|
settings := UserSettings{
|
||||||
PayloadReadTimeout: 120,
|
PayloadReadTimeout: 120,
|
||||||
}
|
}
|
||||||
if level > 0 {
|
if level > 0 {
|
||||||
settings.PayloadReadTimeout = 0
|
settings.PayloadReadTimeout = 0
|
||||||
}
|
}
|
||||||
return settings
|
return settings
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@ type UserSet interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type TimedUserSet struct {
|
type TimedUserSet struct {
|
||||||
validUsers []config.User
|
validUsers []config.User
|
||||||
userHash map[string]indexTimePair
|
userHash map[string]indexTimePair
|
||||||
userHashDeleteQueue *collect.TimedQueue
|
userHashDeleteQueue *collect.TimedQueue
|
||||||
access sync.RWMutex
|
access sync.RWMutex
|
||||||
@ -32,7 +32,7 @@ type indexTimePair struct {
|
|||||||
|
|
||||||
func NewTimedUserSet() UserSet {
|
func NewTimedUserSet() UserSet {
|
||||||
tus := &TimedUserSet{
|
tus := &TimedUserSet{
|
||||||
validUsers: make([]config.User, 0, 16),
|
validUsers: make([]config.User, 0, 16),
|
||||||
userHash: make(map[string]indexTimePair, 512),
|
userHash: make(map[string]indexTimePair, 512),
|
||||||
userHashDeleteQueue: collect.NewTimedQueue(updateIntervalSec),
|
userHashDeleteQueue: collect.NewTimedQueue(updateIntervalSec),
|
||||||
access: sync.RWMutex{},
|
access: sync.RWMutex{},
|
||||||
|
@ -37,7 +37,7 @@ const (
|
|||||||
// streaming.
|
// streaming.
|
||||||
type VMessRequest struct {
|
type VMessRequest struct {
|
||||||
Version byte
|
Version byte
|
||||||
User config.User
|
User config.User
|
||||||
RequestIV []byte
|
RequestIV []byte
|
||||||
RequestKey []byte
|
RequestKey []byte
|
||||||
ResponseHeader []byte
|
ResponseHeader []byte
|
||||||
@ -98,7 +98,7 @@ func (r *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
|
|||||||
bufferLen := nBytes
|
bufferLen := nBytes
|
||||||
|
|
||||||
request := &VMessRequest{
|
request := &VMessRequest{
|
||||||
User: userObj,
|
User: userObj,
|
||||||
Version: buffer.Value[0],
|
Version: buffer.Value[0],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,8 +13,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type TestUser struct {
|
type TestUser struct {
|
||||||
id *config.ID
|
id *config.ID
|
||||||
level config.UserLevel
|
level config.UserLevel
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *TestUser) ID() *config.ID {
|
func (u *TestUser) ID() *config.ID {
|
||||||
@ -22,7 +22,7 @@ func (u *TestUser) ID() *config.ID {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *TestUser) Level() config.UserLevel {
|
func (this *TestUser) Level() config.UserLevel {
|
||||||
return this.level
|
return this.level
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestVMessSerialization(t *testing.T) {
|
func TestVMessSerialization(t *testing.T) {
|
||||||
@ -32,10 +32,10 @@ func TestVMessSerialization(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
testUser := &TestUser {
|
testUser := &TestUser{
|
||||||
id: userId,
|
id: userId,
|
||||||
}
|
}
|
||||||
|
|
||||||
userSet := mocks.MockUserSet{[]config.User{}, make(map[string]int), make(map[string]int64)}
|
userSet := mocks.MockUserSet{[]config.User{}, make(map[string]int), make(map[string]int64)}
|
||||||
userSet.AddUser(testUser)
|
userSet.AddUser(testUser)
|
||||||
@ -82,10 +82,10 @@ func TestVMessSerialization(t *testing.T) {
|
|||||||
func BenchmarkVMessRequestWriting(b *testing.B) {
|
func BenchmarkVMessRequestWriting(b *testing.B) {
|
||||||
userId, _ := config.NewID("2b2966ac-16aa-4fbf-8d81-c5f172a3da51")
|
userId, _ := config.NewID("2b2966ac-16aa-4fbf-8d81-c5f172a3da51")
|
||||||
userSet := mocks.MockUserSet{[]config.User{}, make(map[string]int), make(map[string]int64)}
|
userSet := mocks.MockUserSet{[]config.User{}, make(map[string]int), make(map[string]int64)}
|
||||||
|
|
||||||
testUser := &TestUser{
|
testUser := &TestUser{
|
||||||
id: userId,
|
id: userId,
|
||||||
}
|
}
|
||||||
userSet.AddUser(testUser)
|
userSet.AddUser(testUser)
|
||||||
|
|
||||||
request := new(VMessRequest)
|
request := new(VMessRequest)
|
||||||
|
@ -91,7 +91,7 @@ func (handler *VMessInboundHandler) HandleConnection(connection *net.TCPConn) er
|
|||||||
readFinish.Lock()
|
readFinish.Lock()
|
||||||
writeFinish.Lock()
|
writeFinish.Lock()
|
||||||
|
|
||||||
userSettings := config.GetUserSettings(request.User.Level())
|
userSettings := config.GetUserSettings(request.User.Level())
|
||||||
connReader.SetTimeOut(userSettings.PayloadReadTimeout)
|
connReader.SetTimeOut(userSettings.PayloadReadTimeout)
|
||||||
go handleInput(request, connReader, input, &readFinish)
|
go handleInput(request, connReader, input, &readFinish)
|
||||||
|
|
||||||
|
@ -67,7 +67,7 @@ func (handler *VMessOutboundHandler) Dispatch(firstPacket v2net.Packet, ray ray.
|
|||||||
}
|
}
|
||||||
request := &protocol.VMessRequest{
|
request := &protocol.VMessRequest{
|
||||||
Version: protocol.Version,
|
Version: protocol.Version,
|
||||||
User: vNextUser,
|
User: vNextUser,
|
||||||
Command: command,
|
Command: command,
|
||||||
Address: firstPacket.Destination().Address(),
|
Address: firstPacket.Destination().Address(),
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type MockUserSet struct {
|
type MockUserSet struct {
|
||||||
Users []config.User
|
Users []config.User
|
||||||
UserHashes map[string]int
|
UserHashes map[string]int
|
||||||
Timestamps map[string]int64
|
Timestamps map[string]int64
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user