mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-09-16 08:58:22 -04:00
Massive refactoring for better code structure
This commit is contained in:
parent
5eee1b97aa
commit
075753c030
@ -1,10 +1,5 @@
|
|||||||
package core
|
package core
|
||||||
|
|
||||||
// User is the user account that is used for connection to a Point
|
|
||||||
type User struct {
|
|
||||||
Id ID `json:"id"` // The ID of this User.
|
|
||||||
}
|
|
||||||
|
|
||||||
type ConnectionConfig interface {
|
type ConnectionConfig interface {
|
||||||
Protocol() string
|
Protocol() string
|
||||||
Content() []byte
|
Content() []byte
|
||||||
|
2
point.go
2
point.go
@ -1,8 +1,8 @@
|
|||||||
package core
|
package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||||
"github.com/v2ray/v2ray-core/log"
|
"github.com/v2ray/v2ray-core/log"
|
||||||
v2net "github.com/v2ray/v2ray-core/net"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -4,8 +4,8 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/v2ray/v2ray-core"
|
"github.com/v2ray/v2ray-core"
|
||||||
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||||
"github.com/v2ray/v2ray-core/log"
|
"github.com/v2ray/v2ray-core/log"
|
||||||
v2net "github.com/v2ray/v2ray-core/net"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type FreedomConnection struct {
|
type FreedomConnection struct {
|
@ -2,7 +2,7 @@ package freedom
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/v2ray/v2ray-core"
|
"github.com/v2ray/v2ray-core"
|
||||||
v2net "github.com/v2ray/v2ray-core/net"
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||||
)
|
)
|
||||||
|
|
||||||
type FreedomFactory struct {
|
type FreedomFactory struct {
|
@ -1,5 +1,4 @@
|
|||||||
// Package socks contains protocol definition and io lib for SOCKS5 protocol
|
package protocol
|
||||||
package socks
|
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
@ -7,8 +6,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||||
"github.com/v2ray/v2ray-core/log"
|
"github.com/v2ray/v2ray-core/log"
|
||||||
v2net "github.com/v2ray/v2ray-core/net"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
@ -1,4 +1,4 @@
|
|||||||
package socks
|
package protocol
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
@ -1,9 +1,9 @@
|
|||||||
package socks
|
package protocol
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
v2net "github.com/v2ray/v2ray-core/net"
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Socks5UDPRequest struct {
|
type Socks5UDPRequest struct {
|
@ -8,9 +8,9 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/v2ray/v2ray-core"
|
"github.com/v2ray/v2ray-core"
|
||||||
socksio "github.com/v2ray/v2ray-core/io/socks"
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||||
"github.com/v2ray/v2ray-core/log"
|
"github.com/v2ray/v2ray-core/log"
|
||||||
v2net "github.com/v2ray/v2ray-core/net"
|
protocol "github.com/v2ray/v2ray-core/proxy/socks/protocol"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -64,8 +64,8 @@ func (server *SocksServer) HandleConnection(connection net.Conn) error {
|
|||||||
|
|
||||||
reader := connection.(io.Reader)
|
reader := connection.(io.Reader)
|
||||||
|
|
||||||
auth, auth4, err := socksio.ReadAuthentication(reader)
|
auth, auth4, err := protocol.ReadAuthentication(reader)
|
||||||
if err != nil && err != socksio.ErrorSocksVersion4 {
|
if err != nil && err != protocol.ErrorSocksVersion4 {
|
||||||
log.Error("Error on reading authentication: %v", err)
|
log.Error("Error on reading authentication: %v", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -73,28 +73,28 @@ func (server *SocksServer) HandleConnection(connection net.Conn) error {
|
|||||||
var dest v2net.Address
|
var dest v2net.Address
|
||||||
|
|
||||||
// TODO refactor this part
|
// TODO refactor this part
|
||||||
if err == socksio.ErrorSocksVersion4 {
|
if err == protocol.ErrorSocksVersion4 {
|
||||||
result := socksio.Socks4RequestGranted
|
result := protocol.Socks4RequestGranted
|
||||||
if auth4.Command == socksio.CmdBind {
|
if auth4.Command == protocol.CmdBind {
|
||||||
result = socksio.Socks4RequestRejected
|
result = protocol.Socks4RequestRejected
|
||||||
}
|
}
|
||||||
socks4Response := socksio.NewSocks4AuthenticationResponse(result, auth4.Port, auth4.IP[:])
|
socks4Response := protocol.NewSocks4AuthenticationResponse(result, auth4.Port, auth4.IP[:])
|
||||||
socksio.WriteSocks4AuthenticationResponse(connection, socks4Response)
|
protocol.WriteSocks4AuthenticationResponse(connection, socks4Response)
|
||||||
|
|
||||||
if result == socksio.Socks4RequestRejected {
|
if result == protocol.Socks4RequestRejected {
|
||||||
return ErrorCommandNotSupported
|
return ErrorCommandNotSupported
|
||||||
}
|
}
|
||||||
|
|
||||||
dest = v2net.IPAddress(auth4.IP[:], auth4.Port)
|
dest = v2net.IPAddress(auth4.IP[:], auth4.Port)
|
||||||
} else {
|
} else {
|
||||||
expectedAuthMethod := socksio.AuthNotRequired
|
expectedAuthMethod := protocol.AuthNotRequired
|
||||||
if server.config.AuthMethod == JsonAuthMethodUserPass {
|
if server.config.AuthMethod == JsonAuthMethodUserPass {
|
||||||
expectedAuthMethod = socksio.AuthUserPass
|
expectedAuthMethod = protocol.AuthUserPass
|
||||||
}
|
}
|
||||||
|
|
||||||
if !auth.HasAuthMethod(expectedAuthMethod) {
|
if !auth.HasAuthMethod(expectedAuthMethod) {
|
||||||
authResponse := socksio.NewAuthenticationResponse(socksio.AuthNoMatchingMethod)
|
authResponse := protocol.NewAuthenticationResponse(protocol.AuthNoMatchingMethod)
|
||||||
err = socksio.WriteAuthentication(connection, authResponse)
|
err = protocol.WriteAuthentication(connection, authResponse)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Error on socksio write authentication: %v", err)
|
log.Error("Error on socksio write authentication: %v", err)
|
||||||
return err
|
return err
|
||||||
@ -103,14 +103,14 @@ func (server *SocksServer) HandleConnection(connection net.Conn) error {
|
|||||||
return ErrorAuthenticationFailed
|
return ErrorAuthenticationFailed
|
||||||
}
|
}
|
||||||
|
|
||||||
authResponse := socksio.NewAuthenticationResponse(expectedAuthMethod)
|
authResponse := protocol.NewAuthenticationResponse(expectedAuthMethod)
|
||||||
err = socksio.WriteAuthentication(connection, authResponse)
|
err = protocol.WriteAuthentication(connection, authResponse)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Error on socksio write authentication: %v", err)
|
log.Error("Error on socksio write authentication: %v", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if server.config.AuthMethod == JsonAuthMethodUserPass {
|
if server.config.AuthMethod == JsonAuthMethodUserPass {
|
||||||
upRequest, err := socksio.ReadUserPassRequest(reader)
|
upRequest, err := protocol.ReadUserPassRequest(reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Failed to read username and password: %v", err)
|
log.Error("Failed to read username and password: %v", err)
|
||||||
return err
|
return err
|
||||||
@ -119,8 +119,8 @@ func (server *SocksServer) HandleConnection(connection net.Conn) error {
|
|||||||
if !upRequest.IsValid(server.config.Username, server.config.Password) {
|
if !upRequest.IsValid(server.config.Username, server.config.Password) {
|
||||||
status = byte(0xFF)
|
status = byte(0xFF)
|
||||||
}
|
}
|
||||||
upResponse := socksio.NewSocks5UserPassResponse(status)
|
upResponse := protocol.NewSocks5UserPassResponse(status)
|
||||||
err = socksio.WriteUserPassResponse(connection, upResponse)
|
err = protocol.WriteUserPassResponse(connection, upResponse)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Error on socksio write user pass response: %v", err)
|
log.Error("Error on socksio write user pass response: %v", err)
|
||||||
return err
|
return err
|
||||||
@ -130,18 +130,18 @@ func (server *SocksServer) HandleConnection(connection net.Conn) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
request, err := socksio.ReadRequest(reader)
|
request, err := protocol.ReadRequest(reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Error on reading socks request: %v", err)
|
log.Error("Error on reading socks request: %v", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
response := socksio.NewSocks5Response()
|
response := protocol.NewSocks5Response()
|
||||||
|
|
||||||
if request.Command == socksio.CmdBind || request.Command == socksio.CmdUdpAssociate {
|
if request.Command == protocol.CmdBind || request.Command == protocol.CmdUdpAssociate {
|
||||||
response := socksio.NewSocks5Response()
|
response := protocol.NewSocks5Response()
|
||||||
response.Error = socksio.ErrorCommandNotSupported
|
response.Error = protocol.ErrorCommandNotSupported
|
||||||
err = socksio.WriteResponse(connection, response)
|
err = protocol.WriteResponse(connection, response)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Error on socksio write response: %v", err)
|
log.Error("Error on socksio write response: %v", err)
|
||||||
return err
|
return err
|
||||||
@ -150,18 +150,18 @@ func (server *SocksServer) HandleConnection(connection net.Conn) error {
|
|||||||
return ErrorCommandNotSupported
|
return ErrorCommandNotSupported
|
||||||
}
|
}
|
||||||
|
|
||||||
response.Error = socksio.ErrorSuccess
|
response.Error = protocol.ErrorSuccess
|
||||||
response.Port = request.Port
|
response.Port = request.Port
|
||||||
response.AddrType = request.AddrType
|
response.AddrType = request.AddrType
|
||||||
switch response.AddrType {
|
switch response.AddrType {
|
||||||
case socksio.AddrTypeIPv4:
|
case protocol.AddrTypeIPv4:
|
||||||
copy(response.IPv4[:], request.IPv4[:])
|
copy(response.IPv4[:], request.IPv4[:])
|
||||||
case socksio.AddrTypeIPv6:
|
case protocol.AddrTypeIPv6:
|
||||||
copy(response.IPv6[:], request.IPv6[:])
|
copy(response.IPv6[:], request.IPv6[:])
|
||||||
case socksio.AddrTypeDomain:
|
case protocol.AddrTypeDomain:
|
||||||
response.Domain = request.Domain
|
response.Domain = request.Domain
|
||||||
}
|
}
|
||||||
err = socksio.WriteResponse(connection, response)
|
err = protocol.WriteResponse(connection, response)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Error on socksio write response: %v", err)
|
log.Error("Error on socksio write response: %v", err)
|
||||||
return err
|
return err
|
@ -4,9 +4,9 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/v2ray/v2ray-core"
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||||
"github.com/v2ray/v2ray-core/log"
|
"github.com/v2ray/v2ray-core/log"
|
||||||
v2net "github.com/v2ray/v2ray-core/net"
|
"github.com/v2ray/v2ray-core/proxy/vmess/protocol"
|
||||||
)
|
)
|
||||||
|
|
||||||
type VMessUser struct {
|
type VMessUser struct {
|
||||||
@ -14,9 +14,9 @@ type VMessUser struct {
|
|||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *VMessUser) ToUser() (core.User, error) {
|
func (u *VMessUser) ToUser() (protocol.User, error) {
|
||||||
id, err := core.NewID(u.Id)
|
id, err := protocol.NewID(u.Id)
|
||||||
return core.User{
|
return protocol.User{
|
||||||
Id: id,
|
Id: id,
|
||||||
}, err
|
}, err
|
||||||
}
|
}
|
||||||
@ -38,7 +38,7 @@ type VNextConfig struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (config VNextConfig) ToVNextServer() VNextServer {
|
func (config VNextConfig) ToVNextServer() VNextServer {
|
||||||
users := make([]core.User, 0, len(config.Users))
|
users := make([]protocol.User, 0, len(config.Users))
|
||||||
for _, user := range config.Users {
|
for _, user := range config.Users {
|
||||||
vuser, err := user.ToUser()
|
vuser, err := user.ToUser()
|
||||||
if err != nil {
|
if err != nil {
|
@ -1,4 +1,4 @@
|
|||||||
package core
|
package protocol
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/md5"
|
"crypto/md5"
|
@ -1,4 +1,4 @@
|
|||||||
package core
|
package protocol
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
@ -1,4 +1,4 @@
|
|||||||
package hash
|
package protocol
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/hmac"
|
"crypto/hmac"
|
@ -1,4 +1,4 @@
|
|||||||
package hash
|
package protocol
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/md5"
|
"crypto/md5"
|
@ -1,4 +1,4 @@
|
|||||||
package math
|
package protocol
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"math/rand"
|
"math/rand"
|
@ -1,4 +1,4 @@
|
|||||||
package math
|
package protocol
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
6
proxy/vmess/protocol/user.go
Normal file
6
proxy/vmess/protocol/user.go
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package protocol
|
||||||
|
|
||||||
|
// User is the user account that is used for connection to a Point
|
||||||
|
type User struct {
|
||||||
|
Id ID `json:"id"` // The ID of this User.
|
||||||
|
}
|
@ -1,10 +1,9 @@
|
|||||||
package core
|
package protocol
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"container/heap"
|
"container/heap"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
v2hash "github.com/v2ray/v2ray-core/hash"
|
|
||||||
"github.com/v2ray/v2ray-core/log"
|
"github.com/v2ray/v2ray-core/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -74,7 +73,7 @@ func NewTimedUserSet() UserSet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (us *TimedUserSet) generateNewHashes(lastSec, nowSec int64, idx int, id ID) {
|
func (us *TimedUserSet) generateNewHashes(lastSec, nowSec int64, idx int, id ID) {
|
||||||
idHash := v2hash.NewTimeHash(v2hash.HMACHash{})
|
idHash := NewTimeHash(HMACHash{})
|
||||||
for lastSec < nowSec+cacheDurationSec {
|
for lastSec < nowSec+cacheDurationSec {
|
||||||
|
|
||||||
idHash := idHash.Hash(id.Bytes, lastSec)
|
idHash := idHash.Hash(id.Bytes, lastSec)
|
@ -1,5 +1,5 @@
|
|||||||
// Package vmess contains protocol definition, io lib for VMess.
|
// Package vmess contains protocol definition, io lib for VMess.
|
||||||
package vmess
|
package protocol
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/aes"
|
"crypto/aes"
|
||||||
@ -12,12 +12,9 @@ import (
|
|||||||
mrand "math/rand"
|
mrand "math/rand"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/v2ray/v2ray-core"
|
v2io "github.com/v2ray/v2ray-core/common/io"
|
||||||
v2hash "github.com/v2ray/v2ray-core/hash"
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||||
v2io "github.com/v2ray/v2ray-core/io"
|
|
||||||
"github.com/v2ray/v2ray-core/log"
|
"github.com/v2ray/v2ray-core/log"
|
||||||
v2math "github.com/v2ray/v2ray-core/math"
|
|
||||||
v2net "github.com/v2ray/v2ray-core/net"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -41,7 +38,7 @@ var (
|
|||||||
|
|
||||||
type VMessRequest struct {
|
type VMessRequest struct {
|
||||||
Version byte
|
Version byte
|
||||||
UserId core.ID
|
UserId ID
|
||||||
RequestIV [16]byte
|
RequestIV [16]byte
|
||||||
RequestKey [16]byte
|
RequestKey [16]byte
|
||||||
ResponseHeader [4]byte
|
ResponseHeader [4]byte
|
||||||
@ -50,10 +47,10 @@ type VMessRequest struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type VMessRequestReader struct {
|
type VMessRequestReader struct {
|
||||||
vUserSet core.UserSet
|
vUserSet UserSet
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewVMessRequestReader(vUserSet core.UserSet) *VMessRequestReader {
|
func NewVMessRequestReader(vUserSet UserSet) *VMessRequestReader {
|
||||||
return &VMessRequestReader{
|
return &VMessRequestReader{
|
||||||
vUserSet: vUserSet,
|
vUserSet: vUserSet,
|
||||||
}
|
}
|
||||||
@ -62,7 +59,7 @@ func NewVMessRequestReader(vUserSet core.UserSet) *VMessRequestReader {
|
|||||||
func (r *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
|
func (r *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
|
||||||
buffer := make([]byte, 256)
|
buffer := make([]byte, 256)
|
||||||
|
|
||||||
nBytes, err := reader.Read(buffer[:core.IDBytesLen])
|
nBytes, err := reader.Read(buffer[:IDBytesLen])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -78,7 +75,7 @@ func (r *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
aesStream := cipher.NewCFBDecrypter(aesCipher, v2hash.Int64Hash(timeSec))
|
aesStream := cipher.NewCFBDecrypter(aesCipher, Int64Hash(timeSec))
|
||||||
decryptor := v2io.NewCryptionReader(aesStream, reader)
|
decryptor := v2io.NewCryptionReader(aesStream, reader)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -181,7 +178,7 @@ func (r *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
|
|||||||
return request, nil
|
return request, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (request *VMessRequest) ToBytes(idHash v2hash.CounterHash, randomRangeInt64 v2math.RandomInt64InRange) ([]byte, error) {
|
func (request *VMessRequest) ToBytes(idHash CounterHash, randomRangeInt64 RandomInt64InRange) ([]byte, error) {
|
||||||
buffer := make([]byte, 0, 300)
|
buffer := make([]byte, 0, 300)
|
||||||
|
|
||||||
counter := randomRangeInt64(time.Now().UTC().Unix(), 30)
|
counter := randomRangeInt64(time.Now().UTC().Unix(), 30)
|
||||||
@ -238,7 +235,7 @@ func (request *VMessRequest) ToBytes(idHash v2hash.CounterHash, randomRangeInt64
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
aesStream := cipher.NewCFBEncrypter(aesCipher, v2hash.Int64Hash(counter))
|
aesStream := cipher.NewCFBEncrypter(aesCipher, Int64Hash(counter))
|
||||||
aesStream.XORKeyStream(buffer[encryptionBegin:encryptionEnd], buffer[encryptionBegin:encryptionEnd])
|
aesStream.XORKeyStream(buffer[encryptionBegin:encryptionEnd], buffer[encryptionBegin:encryptionEnd])
|
||||||
|
|
||||||
return buffer, nil
|
return buffer, nil
|
@ -1,4 +1,4 @@
|
|||||||
package vmess
|
package protocol
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@ -6,9 +6,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/v2ray/v2ray-core"
|
"github.com/v2ray/v2ray-core"
|
||||||
v2hash "github.com/v2ray/v2ray-core/hash"
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||||
v2math "github.com/v2ray/v2ray-core/math"
|
|
||||||
v2net "github.com/v2ray/v2ray-core/net"
|
|
||||||
"github.com/v2ray/v2ray-core/testing/mocks"
|
"github.com/v2ray/v2ray-core/testing/mocks"
|
||||||
"github.com/v2ray/v2ray-core/testing/unit"
|
"github.com/v2ray/v2ray-core/testing/unit"
|
||||||
)
|
)
|
||||||
@ -47,7 +45,7 @@ func TestVMessSerialization(t *testing.T) {
|
|||||||
request.Address = v2net.DomainAddress("v2ray.com", 80)
|
request.Address = v2net.DomainAddress("v2ray.com", 80)
|
||||||
|
|
||||||
mockTime := int64(1823730)
|
mockTime := int64(1823730)
|
||||||
buffer, err := request.ToBytes(v2hash.NewTimeHash(v2hash.HMACHash{}), func(base int64, delta int) int64 { return mockTime })
|
buffer, err := request.ToBytes(NewTimeHash(HMACHash{}), func(base int64, delta int) int64 { return mockTime })
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -87,6 +85,6 @@ func BenchmarkVMessRequestWriting(b *testing.B) {
|
|||||||
request.Address = v2net.DomainAddress("v2ray.com", 80)
|
request.Address = v2net.DomainAddress("v2ray.com", 80)
|
||||||
|
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
request.ToBytes(v2hash.NewTimeHash(v2hash.HMACHash{}), v2math.GenerateRandomInt64InRange)
|
request.ToBytes(NewTimeHash(HMACHash{}), GenerateRandomInt64InRange)
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -5,7 +5,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/v2ray/v2ray-core"
|
"github.com/v2ray/v2ray-core"
|
||||||
v2net "github.com/v2ray/v2ray-core/net"
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||||
"github.com/v2ray/v2ray-core/testing/mocks"
|
"github.com/v2ray/v2ray-core/testing/mocks"
|
||||||
"github.com/v2ray/v2ray-core/testing/unit"
|
"github.com/v2ray/v2ray-core/testing/unit"
|
||||||
)
|
)
|
@ -7,19 +7,19 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/v2ray/v2ray-core"
|
"github.com/v2ray/v2ray-core"
|
||||||
v2io "github.com/v2ray/v2ray-core/io"
|
v2io "github.com/v2ray/v2ray-core/common/io"
|
||||||
vmessio "github.com/v2ray/v2ray-core/io/vmess"
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||||
"github.com/v2ray/v2ray-core/log"
|
"github.com/v2ray/v2ray-core/log"
|
||||||
v2net "github.com/v2ray/v2ray-core/net"
|
protocol "github.com/v2ray/v2ray-core/proxy/vmess/protocol"
|
||||||
)
|
)
|
||||||
|
|
||||||
type VMessInboundHandler struct {
|
type VMessInboundHandler struct {
|
||||||
vPoint *core.Point
|
vPoint *core.Point
|
||||||
clients core.UserSet
|
clients protocol.UserSet
|
||||||
accepting bool
|
accepting bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewVMessInboundHandler(vp *core.Point, clients core.UserSet) *VMessInboundHandler {
|
func NewVMessInboundHandler(vp *core.Point, clients protocol.UserSet) *VMessInboundHandler {
|
||||||
return &VMessInboundHandler{
|
return &VMessInboundHandler{
|
||||||
vPoint: vp,
|
vPoint: vp,
|
||||||
clients: clients,
|
clients: clients,
|
||||||
@ -51,7 +51,7 @@ func (handler *VMessInboundHandler) AcceptConnections(listener net.Listener) err
|
|||||||
func (handler *VMessInboundHandler) HandleConnection(connection net.Conn) error {
|
func (handler *VMessInboundHandler) HandleConnection(connection net.Conn) error {
|
||||||
defer connection.Close()
|
defer connection.Close()
|
||||||
|
|
||||||
reader := vmessio.NewVMessRequestReader(handler.clients)
|
reader := protocol.NewVMessRequestReader(handler.clients)
|
||||||
|
|
||||||
request, err := reader.Read(connection)
|
request, err := reader.Read(connection)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -72,7 +72,7 @@ func (handler *VMessInboundHandler) HandleConnection(connection net.Conn) error
|
|||||||
responseKey := md5.Sum(request.RequestKey[:])
|
responseKey := md5.Sum(request.RequestKey[:])
|
||||||
responseIV := md5.Sum(request.RequestIV[:])
|
responseIV := md5.Sum(request.RequestIV[:])
|
||||||
|
|
||||||
response := vmessio.NewVMessResponse(request)
|
response := protocol.NewVMessResponse(request)
|
||||||
responseWriter, err := v2io.NewAesEncryptWriter(responseKey[:], responseIV[:], connection)
|
responseWriter, err := v2io.NewAesEncryptWriter(responseKey[:], responseIV[:], connection)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return log.Error("VMessIn: Failed to create encrypt writer: %v", err)
|
return log.Error("VMessIn: Failed to create encrypt writer: %v", err)
|
||||||
@ -97,7 +97,7 @@ func (handler *VMessInboundHandler) HandleConnection(connection net.Conn) error
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleInput(request *vmessio.VMessRequest, reader io.Reader, input chan<- []byte, finish chan<- bool) {
|
func handleInput(request *protocol.VMessRequest, reader io.Reader, input chan<- []byte, finish chan<- bool) {
|
||||||
defer close(input)
|
defer close(input)
|
||||||
defer close(finish)
|
defer close(finish)
|
||||||
|
|
||||||
@ -110,7 +110,7 @@ func handleInput(request *vmessio.VMessRequest, reader io.Reader, input chan<- [
|
|||||||
v2net.ReaderToChan(input, requestReader)
|
v2net.ReaderToChan(input, requestReader)
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleOutput(request *vmessio.VMessRequest, writer io.Writer, output <-chan []byte, finish chan<- bool) {
|
func handleOutput(request *protocol.VMessRequest, writer io.Writer, output <-chan []byte, finish chan<- bool) {
|
||||||
v2net.ChanToWriter(writer, output)
|
v2net.ChanToWriter(writer, output)
|
||||||
close(finish)
|
close(finish)
|
||||||
}
|
}
|
||||||
@ -123,7 +123,7 @@ func (factory *VMessInboundHandlerFactory) Create(vp *core.Point, rawConfig []by
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(log.Error("VMessIn: Failed to load VMess inbound config: %v", err))
|
panic(log.Error("VMessIn: Failed to load VMess inbound config: %v", err))
|
||||||
}
|
}
|
||||||
allowedClients := core.NewTimedUserSet()
|
allowedClients := protocol.NewTimedUserSet()
|
||||||
for _, client := range config.AllowedClients {
|
for _, client := range config.AllowedClients {
|
||||||
user, err := client.ToUser()
|
user, err := client.ToUser()
|
||||||
if err != nil {
|
if err != nil {
|
@ -8,18 +8,16 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/v2ray/v2ray-core"
|
"github.com/v2ray/v2ray-core"
|
||||||
v2hash "github.com/v2ray/v2ray-core/hash"
|
v2io "github.com/v2ray/v2ray-core/common/io"
|
||||||
v2io "github.com/v2ray/v2ray-core/io"
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||||
vmessio "github.com/v2ray/v2ray-core/io/vmess"
|
|
||||||
"github.com/v2ray/v2ray-core/log"
|
"github.com/v2ray/v2ray-core/log"
|
||||||
v2math "github.com/v2ray/v2ray-core/math"
|
protocol "github.com/v2ray/v2ray-core/proxy/vmess/protocol"
|
||||||
v2net "github.com/v2ray/v2ray-core/net"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// VNext is the next Point server in the connection chain.
|
// VNext is the next Point server in the connection chain.
|
||||||
type VNextServer struct {
|
type VNextServer struct {
|
||||||
Address v2net.Address // Address of VNext server
|
Address v2net.Address // Address of VNext server
|
||||||
Users []core.User // User accounts for accessing VNext.
|
Users []protocol.User // User accounts for accessing VNext.
|
||||||
}
|
}
|
||||||
|
|
||||||
type VMessOutboundHandler struct {
|
type VMessOutboundHandler struct {
|
||||||
@ -36,7 +34,7 @@ func NewVMessOutboundHandler(vp *core.Point, vNextList []VNextServer, dest v2net
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler *VMessOutboundHandler) pickVNext() (v2net.Address, core.User) {
|
func (handler *VMessOutboundHandler) pickVNext() (v2net.Address, protocol.User) {
|
||||||
vNextLen := len(handler.vNextList)
|
vNextLen := len(handler.vNextList)
|
||||||
if vNextLen == 0 {
|
if vNextLen == 0 {
|
||||||
panic("VMessOut: Zero vNext is configured.")
|
panic("VMessOut: Zero vNext is configured.")
|
||||||
@ -55,8 +53,8 @@ func (handler *VMessOutboundHandler) pickVNext() (v2net.Address, core.User) {
|
|||||||
func (handler *VMessOutboundHandler) Start(ray core.OutboundRay) error {
|
func (handler *VMessOutboundHandler) Start(ray core.OutboundRay) error {
|
||||||
vNextAddress, vNextUser := handler.pickVNext()
|
vNextAddress, vNextUser := handler.pickVNext()
|
||||||
|
|
||||||
request := &vmessio.VMessRequest{
|
request := &protocol.VMessRequest{
|
||||||
Version: vmessio.Version,
|
Version: protocol.Version,
|
||||||
UserId: vNextUser.Id,
|
UserId: vNextUser.Id,
|
||||||
Command: byte(0x01),
|
Command: byte(0x01),
|
||||||
Address: handler.dest,
|
Address: handler.dest,
|
||||||
@ -69,7 +67,7 @@ func (handler *VMessOutboundHandler) Start(ray core.OutboundRay) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func startCommunicate(request *vmessio.VMessRequest, dest v2net.Address, ray core.OutboundRay) error {
|
func startCommunicate(request *protocol.VMessRequest, dest v2net.Address, ray core.OutboundRay) error {
|
||||||
input := ray.OutboundInput()
|
input := ray.OutboundInput()
|
||||||
output := ray.OutboundOutput()
|
output := ray.OutboundOutput()
|
||||||
|
|
||||||
@ -95,7 +93,7 @@ func startCommunicate(request *vmessio.VMessRequest, dest v2net.Address, ray cor
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleRequest(conn *net.TCPConn, request *vmessio.VMessRequest, input <-chan []byte, finish chan<- bool) {
|
func handleRequest(conn *net.TCPConn, request *protocol.VMessRequest, input <-chan []byte, finish chan<- bool) {
|
||||||
defer close(finish)
|
defer close(finish)
|
||||||
encryptRequestWriter, err := v2io.NewAesEncryptWriter(request.RequestKey[:], request.RequestIV[:], conn)
|
encryptRequestWriter, err := v2io.NewAesEncryptWriter(request.RequestKey[:], request.RequestIV[:], conn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -103,7 +101,7 @@ func handleRequest(conn *net.TCPConn, request *vmessio.VMessRequest, input <-cha
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer, err := request.ToBytes(v2hash.NewTimeHash(v2hash.HMACHash{}), v2math.GenerateRandomInt64InRange)
|
buffer, err := request.ToBytes(protocol.NewTimeHash(protocol.HMACHash{}), protocol.GenerateRandomInt64InRange)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("VMessOut: Failed to serialize VMess request: %v", err)
|
log.Error("VMessOut: Failed to serialize VMess request: %v", err)
|
||||||
return
|
return
|
||||||
@ -126,7 +124,7 @@ func handleRequest(conn *net.TCPConn, request *vmessio.VMessRequest, input <-cha
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleResponse(conn *net.TCPConn, request *vmessio.VMessRequest, output chan<- []byte, finish chan<- bool) {
|
func handleResponse(conn *net.TCPConn, request *protocol.VMessRequest, output chan<- []byte, finish chan<- bool) {
|
||||||
defer close(finish)
|
defer close(finish)
|
||||||
defer close(output)
|
defer close(output)
|
||||||
responseKey := md5.Sum(request.RequestKey[:])
|
responseKey := md5.Sum(request.RequestKey[:])
|
||||||
@ -138,7 +136,7 @@ func handleResponse(conn *net.TCPConn, request *vmessio.VMessRequest, output cha
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
response := vmessio.VMessResponse{}
|
response := protocol.VMessResponse{}
|
||||||
nBytes, err := decryptResponseReader.Read(response[:])
|
nBytes, err := decryptResponseReader.Read(response[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("VMessOut: Failed to read VMess response (%d bytes): %v", nBytes, err)
|
log.Error("VMessOut: Failed to read VMess response (%d bytes): %v", nBytes, err)
|
@ -5,13 +5,13 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/v2ray/v2ray-core"
|
"github.com/v2ray/v2ray-core"
|
||||||
jsonconf "github.com/v2ray/v2ray-core/io/config/json"
|
jsonconf "github.com/v2ray/v2ray-core/config/json"
|
||||||
"github.com/v2ray/v2ray-core/log"
|
"github.com/v2ray/v2ray-core/log"
|
||||||
|
|
||||||
// The following are neccesary as they register handlers in their init functions.
|
// The following are neccesary as they register handlers in their init functions.
|
||||||
_ "github.com/v2ray/v2ray-core/net/freedom"
|
_ "github.com/v2ray/v2ray-core/proxy/freedom"
|
||||||
_ "github.com/v2ray/v2ray-core/net/socks"
|
_ "github.com/v2ray/v2ray-core/proxy/socks"
|
||||||
_ "github.com/v2ray/v2ray-core/net/vmess"
|
_ "github.com/v2ray/v2ray-core/proxy/vmess"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -4,7 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
|
|
||||||
"github.com/v2ray/v2ray-core"
|
"github.com/v2ray/v2ray-core"
|
||||||
v2net "github.com/v2ray/v2ray-core/net"
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||||
)
|
)
|
||||||
|
|
||||||
type InboundConnectionHandler struct {
|
type InboundConnectionHandler struct {
|
||||||
|
@ -1,21 +1,21 @@
|
|||||||
package mocks
|
package mocks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/v2ray/v2ray-core"
|
"github.com/v2ray/v2ray-core/proxy/vmess/protocol"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MockUserSet struct {
|
type MockUserSet struct {
|
||||||
UserIds []core.ID
|
UserIds []protocol.ID
|
||||||
UserHashes map[string]int
|
UserHashes map[string]int
|
||||||
Timestamps map[string]int64
|
Timestamps map[string]int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (us *MockUserSet) AddUser(user core.User) error {
|
func (us *MockUserSet) AddUser(user protocol.User) error {
|
||||||
us.UserIds = append(us.UserIds, user.Id)
|
us.UserIds = append(us.UserIds, user.Id)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (us *MockUserSet) GetUser(userhash []byte) (*core.ID, int64, bool) {
|
func (us *MockUserSet) GetUser(userhash []byte) (*protocol.ID, int64, bool) {
|
||||||
idx, found := us.UserHashes[string(userhash)]
|
idx, found := us.UserHashes[string(userhash)]
|
||||||
if found {
|
if found {
|
||||||
return &us.UserIds[idx], us.Timestamps[string(userhash)], true
|
return &us.UserIds[idx], us.Timestamps[string(userhash)], true
|
||||||
|
@ -4,7 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
|
|
||||||
"github.com/v2ray/v2ray-core"
|
"github.com/v2ray/v2ray-core"
|
||||||
v2net "github.com/v2ray/v2ray-core/net"
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||||
)
|
)
|
||||||
|
|
||||||
type OutboundConnectionHandler struct {
|
type OutboundConnectionHandler struct {
|
||||||
|
Loading…
Reference in New Issue
Block a user