1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-26 23:24:35 -04:00

Massive refactoring for better code structure

This commit is contained in:
V2Ray 2015-09-19 23:54:36 +02:00
parent 5eee1b97aa
commit 075753c030
34 changed files with 107 additions and 115 deletions

View File

@ -1,10 +1,5 @@
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 {
Protocol() string
Content() []byte

View File

@ -1,8 +1,8 @@
package core
import (
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/log"
v2net "github.com/v2ray/v2ray-core/net"
)
var (

View File

@ -4,8 +4,8 @@ import (
"net"
"github.com/v2ray/v2ray-core"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/log"
v2net "github.com/v2ray/v2ray-core/net"
)
type FreedomConnection struct {

View File

@ -2,7 +2,7 @@ package freedom
import (
"github.com/v2ray/v2ray-core"
v2net "github.com/v2ray/v2ray-core/net"
v2net "github.com/v2ray/v2ray-core/common/net"
)
type FreedomFactory struct {

View File

@ -1,5 +1,4 @@
// Package socks contains protocol definition and io lib for SOCKS5 protocol
package socks
package protocol
import (
"encoding/binary"
@ -7,8 +6,8 @@ import (
"fmt"
"io"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/log"
v2net "github.com/v2ray/v2ray-core/net"
)
const (

View File

@ -1,4 +1,4 @@
package socks
package protocol
import (
"bytes"

View File

@ -1,9 +1,9 @@
package socks
package protocol
import (
"io"
v2net "github.com/v2ray/v2ray-core/net"
v2net "github.com/v2ray/v2ray-core/common/net"
)
type Socks5UDPRequest struct {

View File

@ -8,9 +8,9 @@ import (
"strconv"
"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"
v2net "github.com/v2ray/v2ray-core/net"
protocol "github.com/v2ray/v2ray-core/proxy/socks/protocol"
)
var (
@ -64,8 +64,8 @@ func (server *SocksServer) HandleConnection(connection net.Conn) error {
reader := connection.(io.Reader)
auth, auth4, err := socksio.ReadAuthentication(reader)
if err != nil && err != socksio.ErrorSocksVersion4 {
auth, auth4, err := protocol.ReadAuthentication(reader)
if err != nil && err != protocol.ErrorSocksVersion4 {
log.Error("Error on reading authentication: %v", err)
return err
}
@ -73,28 +73,28 @@ func (server *SocksServer) HandleConnection(connection net.Conn) error {
var dest v2net.Address
// TODO refactor this part
if err == socksio.ErrorSocksVersion4 {
result := socksio.Socks4RequestGranted
if auth4.Command == socksio.CmdBind {
result = socksio.Socks4RequestRejected
if err == protocol.ErrorSocksVersion4 {
result := protocol.Socks4RequestGranted
if auth4.Command == protocol.CmdBind {
result = protocol.Socks4RequestRejected
}
socks4Response := socksio.NewSocks4AuthenticationResponse(result, auth4.Port, auth4.IP[:])
socksio.WriteSocks4AuthenticationResponse(connection, socks4Response)
socks4Response := protocol.NewSocks4AuthenticationResponse(result, auth4.Port, auth4.IP[:])
protocol.WriteSocks4AuthenticationResponse(connection, socks4Response)
if result == socksio.Socks4RequestRejected {
if result == protocol.Socks4RequestRejected {
return ErrorCommandNotSupported
}
dest = v2net.IPAddress(auth4.IP[:], auth4.Port)
} else {
expectedAuthMethod := socksio.AuthNotRequired
expectedAuthMethod := protocol.AuthNotRequired
if server.config.AuthMethod == JsonAuthMethodUserPass {
expectedAuthMethod = socksio.AuthUserPass
expectedAuthMethod = protocol.AuthUserPass
}
if !auth.HasAuthMethod(expectedAuthMethod) {
authResponse := socksio.NewAuthenticationResponse(socksio.AuthNoMatchingMethod)
err = socksio.WriteAuthentication(connection, authResponse)
authResponse := protocol.NewAuthenticationResponse(protocol.AuthNoMatchingMethod)
err = protocol.WriteAuthentication(connection, authResponse)
if err != nil {
log.Error("Error on socksio write authentication: %v", err)
return err
@ -103,14 +103,14 @@ func (server *SocksServer) HandleConnection(connection net.Conn) error {
return ErrorAuthenticationFailed
}
authResponse := socksio.NewAuthenticationResponse(expectedAuthMethod)
err = socksio.WriteAuthentication(connection, authResponse)
authResponse := protocol.NewAuthenticationResponse(expectedAuthMethod)
err = protocol.WriteAuthentication(connection, authResponse)
if err != nil {
log.Error("Error on socksio write authentication: %v", err)
return err
}
if server.config.AuthMethod == JsonAuthMethodUserPass {
upRequest, err := socksio.ReadUserPassRequest(reader)
upRequest, err := protocol.ReadUserPassRequest(reader)
if err != nil {
log.Error("Failed to read username and password: %v", err)
return err
@ -119,8 +119,8 @@ func (server *SocksServer) HandleConnection(connection net.Conn) error {
if !upRequest.IsValid(server.config.Username, server.config.Password) {
status = byte(0xFF)
}
upResponse := socksio.NewSocks5UserPassResponse(status)
err = socksio.WriteUserPassResponse(connection, upResponse)
upResponse := protocol.NewSocks5UserPassResponse(status)
err = protocol.WriteUserPassResponse(connection, upResponse)
if err != nil {
log.Error("Error on socksio write user pass response: %v", 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 {
log.Error("Error on reading socks request: %v", err)
return err
}
response := socksio.NewSocks5Response()
response := protocol.NewSocks5Response()
if request.Command == socksio.CmdBind || request.Command == socksio.CmdUdpAssociate {
response := socksio.NewSocks5Response()
response.Error = socksio.ErrorCommandNotSupported
err = socksio.WriteResponse(connection, response)
if request.Command == protocol.CmdBind || request.Command == protocol.CmdUdpAssociate {
response := protocol.NewSocks5Response()
response.Error = protocol.ErrorCommandNotSupported
err = protocol.WriteResponse(connection, response)
if err != nil {
log.Error("Error on socksio write response: %v", err)
return err
@ -150,18 +150,18 @@ func (server *SocksServer) HandleConnection(connection net.Conn) error {
return ErrorCommandNotSupported
}
response.Error = socksio.ErrorSuccess
response.Error = protocol.ErrorSuccess
response.Port = request.Port
response.AddrType = request.AddrType
switch response.AddrType {
case socksio.AddrTypeIPv4:
case protocol.AddrTypeIPv4:
copy(response.IPv4[:], request.IPv4[:])
case socksio.AddrTypeIPv6:
case protocol.AddrTypeIPv6:
copy(response.IPv6[:], request.IPv6[:])
case socksio.AddrTypeDomain:
case protocol.AddrTypeDomain:
response.Domain = request.Domain
}
err = socksio.WriteResponse(connection, response)
err = protocol.WriteResponse(connection, response)
if err != nil {
log.Error("Error on socksio write response: %v", err)
return err

View File

@ -4,9 +4,9 @@ import (
"encoding/json"
"net"
"github.com/v2ray/v2ray-core"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/log"
v2net "github.com/v2ray/v2ray-core/net"
"github.com/v2ray/v2ray-core/proxy/vmess/protocol"
)
type VMessUser struct {
@ -14,9 +14,9 @@ type VMessUser struct {
Email string `json:"email"`
}
func (u *VMessUser) ToUser() (core.User, error) {
id, err := core.NewID(u.Id)
return core.User{
func (u *VMessUser) ToUser() (protocol.User, error) {
id, err := protocol.NewID(u.Id)
return protocol.User{
Id: id,
}, err
}
@ -38,7 +38,7 @@ type VNextConfig struct {
}
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 {
vuser, err := user.ToUser()
if err != nil {

View File

@ -1,4 +1,4 @@
package core
package protocol
import (
"crypto/md5"

View File

@ -1,4 +1,4 @@
package core
package protocol
import (
"testing"

View File

@ -1,4 +1,4 @@
package hash
package protocol
import (
"crypto/hmac"

View File

@ -1,4 +1,4 @@
package hash
package protocol
import (
"crypto/md5"

View File

@ -1,4 +1,4 @@
package math
package protocol
import (
"math/rand"

View File

@ -1,4 +1,4 @@
package math
package protocol
import (
"testing"

View 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.
}

View File

@ -1,10 +1,9 @@
package core
package protocol
import (
"container/heap"
"time"
v2hash "github.com/v2ray/v2ray-core/hash"
"github.com/v2ray/v2ray-core/log"
)
@ -74,7 +73,7 @@ func NewTimedUserSet() UserSet {
}
func (us *TimedUserSet) generateNewHashes(lastSec, nowSec int64, idx int, id ID) {
idHash := v2hash.NewTimeHash(v2hash.HMACHash{})
idHash := NewTimeHash(HMACHash{})
for lastSec < nowSec+cacheDurationSec {
idHash := idHash.Hash(id.Bytes, lastSec)

View File

@ -1,5 +1,5 @@
// Package vmess contains protocol definition, io lib for VMess.
package vmess
package protocol
import (
"crypto/aes"
@ -12,12 +12,9 @@ import (
mrand "math/rand"
"time"
"github.com/v2ray/v2ray-core"
v2hash "github.com/v2ray/v2ray-core/hash"
v2io "github.com/v2ray/v2ray-core/io"
v2io "github.com/v2ray/v2ray-core/common/io"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/log"
v2math "github.com/v2ray/v2ray-core/math"
v2net "github.com/v2ray/v2ray-core/net"
)
const (
@ -41,7 +38,7 @@ var (
type VMessRequest struct {
Version byte
UserId core.ID
UserId ID
RequestIV [16]byte
RequestKey [16]byte
ResponseHeader [4]byte
@ -50,10 +47,10 @@ type VMessRequest struct {
}
type VMessRequestReader struct {
vUserSet core.UserSet
vUserSet UserSet
}
func NewVMessRequestReader(vUserSet core.UserSet) *VMessRequestReader {
func NewVMessRequestReader(vUserSet UserSet) *VMessRequestReader {
return &VMessRequestReader{
vUserSet: vUserSet,
}
@ -62,7 +59,7 @@ func NewVMessRequestReader(vUserSet core.UserSet) *VMessRequestReader {
func (r *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
buffer := make([]byte, 256)
nBytes, err := reader.Read(buffer[:core.IDBytesLen])
nBytes, err := reader.Read(buffer[:IDBytesLen])
if err != nil {
return nil, err
}
@ -78,7 +75,7 @@ func (r *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
if err != nil {
return nil, err
}
aesStream := cipher.NewCFBDecrypter(aesCipher, v2hash.Int64Hash(timeSec))
aesStream := cipher.NewCFBDecrypter(aesCipher, Int64Hash(timeSec))
decryptor := v2io.NewCryptionReader(aesStream, reader)
if err != nil {
@ -181,7 +178,7 @@ func (r *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
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)
counter := randomRangeInt64(time.Now().UTC().Unix(), 30)
@ -238,7 +235,7 @@ func (request *VMessRequest) ToBytes(idHash v2hash.CounterHash, randomRangeInt64
if err != nil {
return nil, err
}
aesStream := cipher.NewCFBEncrypter(aesCipher, v2hash.Int64Hash(counter))
aesStream := cipher.NewCFBEncrypter(aesCipher, Int64Hash(counter))
aesStream.XORKeyStream(buffer[encryptionBegin:encryptionEnd], buffer[encryptionBegin:encryptionEnd])
return buffer, nil

View File

@ -1,4 +1,4 @@
package vmess
package protocol
import (
"bytes"
@ -6,9 +6,7 @@ import (
"testing"
"github.com/v2ray/v2ray-core"
v2hash "github.com/v2ray/v2ray-core/hash"
v2math "github.com/v2ray/v2ray-core/math"
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/unit"
)
@ -47,7 +45,7 @@ func TestVMessSerialization(t *testing.T) {
request.Address = v2net.DomainAddress("v2ray.com", 80)
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 {
t.Fatal(err)
}
@ -87,6 +85,6 @@ func BenchmarkVMessRequestWriting(b *testing.B) {
request.Address = v2net.DomainAddress("v2ray.com", 80)
for i := 0; i < b.N; i++ {
request.ToBytes(v2hash.NewTimeHash(v2hash.HMACHash{}), v2math.GenerateRandomInt64InRange)
request.ToBytes(NewTimeHash(HMACHash{}), GenerateRandomInt64InRange)
}
}

View File

@ -5,7 +5,7 @@ import (
"testing"
"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/unit"
)

View File

@ -7,19 +7,19 @@ import (
"strconv"
"github.com/v2ray/v2ray-core"
v2io "github.com/v2ray/v2ray-core/io"
vmessio "github.com/v2ray/v2ray-core/io/vmess"
v2io "github.com/v2ray/v2ray-core/common/io"
v2net "github.com/v2ray/v2ray-core/common/net"
"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 {
vPoint *core.Point
clients core.UserSet
clients protocol.UserSet
accepting bool
}
func NewVMessInboundHandler(vp *core.Point, clients core.UserSet) *VMessInboundHandler {
func NewVMessInboundHandler(vp *core.Point, clients protocol.UserSet) *VMessInboundHandler {
return &VMessInboundHandler{
vPoint: vp,
clients: clients,
@ -51,7 +51,7 @@ func (handler *VMessInboundHandler) AcceptConnections(listener net.Listener) err
func (handler *VMessInboundHandler) HandleConnection(connection net.Conn) error {
defer connection.Close()
reader := vmessio.NewVMessRequestReader(handler.clients)
reader := protocol.NewVMessRequestReader(handler.clients)
request, err := reader.Read(connection)
if err != nil {
@ -72,7 +72,7 @@ func (handler *VMessInboundHandler) HandleConnection(connection net.Conn) error
responseKey := md5.Sum(request.RequestKey[:])
responseIV := md5.Sum(request.RequestIV[:])
response := vmessio.NewVMessResponse(request)
response := protocol.NewVMessResponse(request)
responseWriter, err := v2io.NewAesEncryptWriter(responseKey[:], responseIV[:], connection)
if err != nil {
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
}
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(finish)
@ -110,7 +110,7 @@ func handleInput(request *vmessio.VMessRequest, reader io.Reader, input chan<- [
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)
close(finish)
}
@ -123,7 +123,7 @@ func (factory *VMessInboundHandlerFactory) Create(vp *core.Point, rawConfig []by
if err != nil {
panic(log.Error("VMessIn: Failed to load VMess inbound config: %v", err))
}
allowedClients := core.NewTimedUserSet()
allowedClients := protocol.NewTimedUserSet()
for _, client := range config.AllowedClients {
user, err := client.ToUser()
if err != nil {

View File

@ -8,18 +8,16 @@ import (
"net"
"github.com/v2ray/v2ray-core"
v2hash "github.com/v2ray/v2ray-core/hash"
v2io "github.com/v2ray/v2ray-core/io"
vmessio "github.com/v2ray/v2ray-core/io/vmess"
v2io "github.com/v2ray/v2ray-core/common/io"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/log"
v2math "github.com/v2ray/v2ray-core/math"
v2net "github.com/v2ray/v2ray-core/net"
protocol "github.com/v2ray/v2ray-core/proxy/vmess/protocol"
)
// VNext is the next Point server in the connection chain.
type VNextServer struct {
Address v2net.Address // Address of VNext server
Users []core.User // User accounts for accessing VNext.
Address v2net.Address // Address of VNext server
Users []protocol.User // User accounts for accessing VNext.
}
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)
if vNextLen == 0 {
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 {
vNextAddress, vNextUser := handler.pickVNext()
request := &vmessio.VMessRequest{
Version: vmessio.Version,
request := &protocol.VMessRequest{
Version: protocol.Version,
UserId: vNextUser.Id,
Command: byte(0x01),
Address: handler.dest,
@ -69,7 +67,7 @@ func (handler *VMessOutboundHandler) Start(ray core.OutboundRay) error {
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()
output := ray.OutboundOutput()
@ -95,7 +93,7 @@ func startCommunicate(request *vmessio.VMessRequest, dest v2net.Address, ray cor
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)
encryptRequestWriter, err := v2io.NewAesEncryptWriter(request.RequestKey[:], request.RequestIV[:], conn)
if err != nil {
@ -103,7 +101,7 @@ func handleRequest(conn *net.TCPConn, request *vmessio.VMessRequest, input <-cha
return
}
buffer, err := request.ToBytes(v2hash.NewTimeHash(v2hash.HMACHash{}), v2math.GenerateRandomInt64InRange)
buffer, err := request.ToBytes(protocol.NewTimeHash(protocol.HMACHash{}), protocol.GenerateRandomInt64InRange)
if err != nil {
log.Error("VMessOut: Failed to serialize VMess request: %v", err)
return
@ -126,7 +124,7 @@ func handleRequest(conn *net.TCPConn, request *vmessio.VMessRequest, input <-cha
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(output)
responseKey := md5.Sum(request.RequestKey[:])
@ -138,7 +136,7 @@ func handleResponse(conn *net.TCPConn, request *vmessio.VMessRequest, output cha
return
}
response := vmessio.VMessResponse{}
response := protocol.VMessResponse{}
nBytes, err := decryptResponseReader.Read(response[:])
if err != nil {
log.Error("VMessOut: Failed to read VMess response (%d bytes): %v", nBytes, err)

View File

@ -5,13 +5,13 @@ import (
"fmt"
"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"
// The following are neccesary as they register handlers in their init functions.
_ "github.com/v2ray/v2ray-core/net/freedom"
_ "github.com/v2ray/v2ray-core/net/socks"
_ "github.com/v2ray/v2ray-core/net/vmess"
_ "github.com/v2ray/v2ray-core/proxy/freedom"
_ "github.com/v2ray/v2ray-core/proxy/socks"
_ "github.com/v2ray/v2ray-core/proxy/vmess"
)
var (

View File

@ -4,7 +4,7 @@ import (
"bytes"
"github.com/v2ray/v2ray-core"
v2net "github.com/v2ray/v2ray-core/net"
v2net "github.com/v2ray/v2ray-core/common/net"
)
type InboundConnectionHandler struct {

View File

@ -1,21 +1,21 @@
package mocks
import (
"github.com/v2ray/v2ray-core"
"github.com/v2ray/v2ray-core/proxy/vmess/protocol"
)
type MockUserSet struct {
UserIds []core.ID
UserIds []protocol.ID
UserHashes map[string]int
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)
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)]
if found {
return &us.UserIds[idx], us.Timestamps[string(userhash)], true

View File

@ -4,7 +4,7 @@ import (
"bytes"
"github.com/v2ray/v2ray-core"
v2net "github.com/v2ray/v2ray-core/net"
v2net "github.com/v2ray/v2ray-core/common/net"
)
type OutboundConnectionHandler struct {