mirror of https://github.com/v2ray/v2ray-core
parent
823cbf1509
commit
cb16047cf0
|
@ -1,46 +1,66 @@
|
||||||
package net
|
package net
|
||||||
|
|
||||||
import (
|
type Destination interface {
|
||||||
"github.com/v2ray/v2ray-core/common/log"
|
Network() string
|
||||||
)
|
Address() Address
|
||||||
|
String() string
|
||||||
|
|
||||||
const (
|
IsTCP() bool
|
||||||
NetTCP = byte(0x01)
|
IsUDP() bool
|
||||||
NetUDP = byte(0x02)
|
}
|
||||||
)
|
|
||||||
|
|
||||||
type Destination struct {
|
func NewTCPDestination(address Address) Destination {
|
||||||
network byte
|
return TCPDestination{address: address}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewUDPDestination(address Address) Destination {
|
||||||
|
return UDPDestination{address: address}
|
||||||
|
}
|
||||||
|
|
||||||
|
type TCPDestination struct {
|
||||||
address Address
|
address Address
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDestination(network byte, address Address) *Destination {
|
func (dest TCPDestination) Network() string {
|
||||||
return &Destination{
|
|
||||||
network: network,
|
|
||||||
address: address,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (dest *Destination) Network() string {
|
|
||||||
switch dest.network {
|
|
||||||
case NetTCP:
|
|
||||||
return "tcp"
|
|
||||||
case NetUDP:
|
|
||||||
return "udp"
|
|
||||||
default:
|
|
||||||
log.Warning("Unknown network %d", dest.network)
|
|
||||||
return "tcp"
|
return "tcp"
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
func (dest *Destination) NetworkByte() byte {
|
func (dest TCPDestination) Address() Address {
|
||||||
return dest.network
|
|
||||||
}
|
|
||||||
|
|
||||||
func (dest *Destination) Address() Address {
|
|
||||||
return dest.address
|
return dest.address
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dest *Destination) String() string {
|
func (dest TCPDestination) String() string {
|
||||||
return dest.address.String() + " (" + dest.Network() + ")"
|
return "tcp:" + dest.address.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dest TCPDestination) IsTCP() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dest TCPDestination) IsUDP() bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
type UDPDestination struct {
|
||||||
|
address Address
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dest UDPDestination) Network() string {
|
||||||
|
return "udp"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dest UDPDestination) Address() Address {
|
||||||
|
return dest.address
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dest UDPDestination) String() string {
|
||||||
|
return "udp:" + dest.address.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dest UDPDestination) IsTCP() bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dest UDPDestination) IsUDP() bool {
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
4
point.go
4
point.go
|
@ -64,7 +64,7 @@ type InboundConnectionHandler interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
type OutboundConnectionHandlerFactory interface {
|
type OutboundConnectionHandlerFactory interface {
|
||||||
Create(VP *Point, config []byte, dest *v2net.Destination) (OutboundConnectionHandler, error)
|
Create(VP *Point, config []byte, dest v2net.Destination) (OutboundConnectionHandler, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type OutboundConnectionHandler interface {
|
type OutboundConnectionHandler interface {
|
||||||
|
@ -85,7 +85,7 @@ func (vp *Point) Start() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (vp *Point) NewInboundConnectionAccepted(destination *v2net.Destination) InboundRay {
|
func (vp *Point) NewInboundConnectionAccepted(destination v2net.Destination) InboundRay {
|
||||||
ray := NewRay()
|
ray := NewRay()
|
||||||
// TODO: handle error
|
// TODO: handle error
|
||||||
och, _ := vp.ochFactory.Create(vp, vp.ochConfig, destination)
|
och, _ := vp.ochFactory.Create(vp, vp.ochConfig, destination)
|
||||||
|
|
|
@ -9,10 +9,10 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type FreedomConnection struct {
|
type FreedomConnection struct {
|
||||||
dest *v2net.Destination
|
dest v2net.Destination
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFreedomConnection(dest *v2net.Destination) *FreedomConnection {
|
func NewFreedomConnection(dest v2net.Destination) *FreedomConnection {
|
||||||
return &FreedomConnection{
|
return &FreedomConnection{
|
||||||
dest: dest,
|
dest: dest,
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,7 @@ import (
|
||||||
type FreedomFactory struct {
|
type FreedomFactory struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (factory FreedomFactory) Create(vp *core.Point, config []byte, dest *v2net.Destination) (core.OutboundConnectionHandler, error) {
|
func (factory FreedomFactory) Create(vp *core.Point, config []byte, dest v2net.Destination) (core.OutboundConnectionHandler, error) {
|
||||||
return NewFreedomConnection(dest), nil
|
return NewFreedomConnection(dest), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -278,7 +278,7 @@ func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (request *Socks5Request) Destination() *v2net.Destination {
|
func (request *Socks5Request) Destination() v2net.Destination {
|
||||||
var address v2net.Address
|
var address v2net.Address
|
||||||
switch request.AddrType {
|
switch request.AddrType {
|
||||||
case AddrTypeIPv4:
|
case AddrTypeIPv4:
|
||||||
|
@ -290,7 +290,7 @@ func (request *Socks5Request) Destination() *v2net.Destination {
|
||||||
default:
|
default:
|
||||||
panic("Unknown address type")
|
panic("Unknown address type")
|
||||||
}
|
}
|
||||||
return v2net.NewDestination(v2net.NetTCP, address)
|
return v2net.NewTCPDestination(address)
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
|
@ -70,7 +70,7 @@ func (server *SocksServer) HandleConnection(connection net.Conn) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var dest *v2net.Destination
|
var dest v2net.Destination
|
||||||
|
|
||||||
// TODO refactor this part
|
// TODO refactor this part
|
||||||
if err == protocol.ErrorSocksVersion4 {
|
if err == protocol.ErrorSocksVersion4 {
|
||||||
|
@ -85,7 +85,7 @@ func (server *SocksServer) HandleConnection(connection net.Conn) error {
|
||||||
return ErrorCommandNotSupported
|
return ErrorCommandNotSupported
|
||||||
}
|
}
|
||||||
|
|
||||||
dest = v2net.NewDestination(v2net.NetTCP, v2net.IPAddress(auth4.IP[:], auth4.Port))
|
dest = v2net.NewTCPDestination(v2net.IPAddress(auth4.IP[:], auth4.Port))
|
||||||
} else {
|
} else {
|
||||||
expectedAuthMethod := protocol.AuthNotRequired
|
expectedAuthMethod := protocol.AuthNotRequired
|
||||||
if server.config.AuthMethod == JsonAuthMethodUserPass {
|
if server.config.AuthMethod == JsonAuthMethodUserPass {
|
||||||
|
|
|
@ -51,12 +51,13 @@ func (config VNextConfig) ToVNextServer() VNextServer {
|
||||||
if ip == nil {
|
if ip == nil {
|
||||||
panic(log.Error("Unable to parse VNext IP: %s", config.Address))
|
panic(log.Error("Unable to parse VNext IP: %s", config.Address))
|
||||||
}
|
}
|
||||||
network := v2net.NetTCP
|
address := v2net.IPAddress(ip, config.Port)
|
||||||
|
dest := v2net.NewTCPDestination(address)
|
||||||
if config.Network == "udp" {
|
if config.Network == "udp" {
|
||||||
network = v2net.NetUDP
|
dest = v2net.NewUDPDestination(address)
|
||||||
}
|
}
|
||||||
return VNextServer{
|
return VNextServer{
|
||||||
Destination: v2net.NewDestination(network, v2net.IPAddress(ip, config.Port)),
|
Destination: dest,
|
||||||
Users: users,
|
Users: users,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,8 +50,12 @@ type VMessRequest struct {
|
||||||
Address v2net.Address
|
Address v2net.Address
|
||||||
}
|
}
|
||||||
|
|
||||||
func (request *VMessRequest) Destination() *v2net.Destination {
|
func (request *VMessRequest) Destination() v2net.Destination {
|
||||||
return v2net.NewDestination(request.Command, request.Address)
|
if request.Command == CmdTCP {
|
||||||
|
return v2net.NewTCPDestination(request.Address)
|
||||||
|
} else {
|
||||||
|
return v2net.NewUDPDestination(request.Address)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type VMessRequestReader struct {
|
type VMessRequestReader struct {
|
||||||
|
|
|
@ -68,7 +68,7 @@ func TestVMessInAndOut(t *testing.T) {
|
||||||
err = pointB.Start()
|
err = pointB.Start()
|
||||||
assert.Error(err).IsNil()
|
assert.Error(err).IsNil()
|
||||||
|
|
||||||
dest := v2net.NewDestination(v2net.NetTCP, v2net.IPAddress([]byte{1, 2, 3, 4}, 80))
|
dest := v2net.NewTCPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}, 80))
|
||||||
ich.Communicate(dest)
|
ich.Communicate(dest)
|
||||||
assert.Bytes([]byte(data2Send)).Equals(och.Data2Send.Bytes())
|
assert.Bytes([]byte(data2Send)).Equals(och.Data2Send.Bytes())
|
||||||
assert.Bytes(ich.DataReturned.Bytes()).Equals(och.Data2Return)
|
assert.Bytes(ich.DataReturned.Bytes()).Equals(och.Data2Return)
|
||||||
|
|
|
@ -17,17 +17,17 @@ import (
|
||||||
|
|
||||||
// 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 {
|
||||||
Destination *v2net.Destination // Address of VNext server
|
Destination v2net.Destination // Address of VNext server
|
||||||
Users []user.User // User accounts for accessing VNext.
|
Users []user.User // User accounts for accessing VNext.
|
||||||
}
|
}
|
||||||
|
|
||||||
type VMessOutboundHandler struct {
|
type VMessOutboundHandler struct {
|
||||||
vPoint *core.Point
|
vPoint *core.Point
|
||||||
dest *v2net.Destination
|
dest v2net.Destination
|
||||||
vNextList []VNextServer
|
vNextList []VNextServer
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewVMessOutboundHandler(vp *core.Point, vNextList []VNextServer, dest *v2net.Destination) *VMessOutboundHandler {
|
func NewVMessOutboundHandler(vp *core.Point, vNextList []VNextServer, dest v2net.Destination) *VMessOutboundHandler {
|
||||||
return &VMessOutboundHandler{
|
return &VMessOutboundHandler{
|
||||||
vPoint: vp,
|
vPoint: vp,
|
||||||
dest: dest,
|
dest: dest,
|
||||||
|
@ -35,7 +35,7 @@ func NewVMessOutboundHandler(vp *core.Point, vNextList []VNextServer, dest *v2ne
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler *VMessOutboundHandler) pickVNext() (*v2net.Destination, user.User) {
|
func (handler *VMessOutboundHandler) pickVNext() (v2net.Destination, user.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.")
|
||||||
|
@ -54,10 +54,14 @@ func (handler *VMessOutboundHandler) pickVNext() (*v2net.Destination, user.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()
|
||||||
|
|
||||||
|
command := protocol.CmdTCP
|
||||||
|
if handler.dest.IsUDP() {
|
||||||
|
command = protocol.CmdUDP
|
||||||
|
}
|
||||||
request := &protocol.VMessRequest{
|
request := &protocol.VMessRequest{
|
||||||
Version: protocol.Version,
|
Version: protocol.Version,
|
||||||
UserId: vNextUser.Id,
|
UserId: vNextUser.Id,
|
||||||
Command: handler.dest.NetworkByte(),
|
Command: command,
|
||||||
Address: handler.dest.Address(),
|
Address: handler.dest.Address(),
|
||||||
}
|
}
|
||||||
rand.Read(request.RequestIV[:])
|
rand.Read(request.RequestIV[:])
|
||||||
|
@ -68,7 +72,7 @@ func (handler *VMessOutboundHandler) Start(ray core.OutboundRay) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func startCommunicate(request *protocol.VMessRequest, dest *v2net.Destination, ray core.OutboundRay) error {
|
func startCommunicate(request *protocol.VMessRequest, dest v2net.Destination, ray core.OutboundRay) error {
|
||||||
input := ray.OutboundInput()
|
input := ray.OutboundInput()
|
||||||
output := ray.OutboundOutput()
|
output := ray.OutboundOutput()
|
||||||
|
|
||||||
|
@ -155,7 +159,7 @@ func handleResponse(conn *net.TCPConn, request *protocol.VMessRequest, output ch
|
||||||
type VMessOutboundHandlerFactory struct {
|
type VMessOutboundHandlerFactory struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (factory *VMessOutboundHandlerFactory) Create(vp *core.Point, rawConfig []byte, destination *v2net.Destination) (core.OutboundConnectionHandler, error) {
|
func (factory *VMessOutboundHandlerFactory) Create(vp *core.Point, rawConfig []byte, destination v2net.Destination) (core.OutboundConnectionHandler, error) {
|
||||||
config, err := loadOutboundConfig(rawConfig)
|
config, err := loadOutboundConfig(rawConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(log.Error("Failed to load VMess outbound config: %v", err))
|
panic(log.Error("Failed to load VMess outbound config: %v", err))
|
||||||
|
|
|
@ -19,7 +19,7 @@ func (handler *InboundConnectionHandler) Listen(port uint16) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler *InboundConnectionHandler) Communicate(dest *v2net.Destination) error {
|
func (handler *InboundConnectionHandler) Communicate(dest v2net.Destination) error {
|
||||||
ray := handler.Server.NewInboundConnectionAccepted(dest)
|
ray := handler.Server.NewInboundConnectionAccepted(dest)
|
||||||
|
|
||||||
input := ray.InboundInput()
|
input := ray.InboundInput()
|
||||||
|
|
|
@ -10,7 +10,7 @@ import (
|
||||||
type OutboundConnectionHandler struct {
|
type OutboundConnectionHandler struct {
|
||||||
Data2Send *bytes.Buffer
|
Data2Send *bytes.Buffer
|
||||||
Data2Return []byte
|
Data2Return []byte
|
||||||
Destination *v2net.Destination
|
Destination v2net.Destination
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler *OutboundConnectionHandler) Start(ray core.OutboundRay) error {
|
func (handler *OutboundConnectionHandler) Start(ray core.OutboundRay) error {
|
||||||
|
@ -32,7 +32,7 @@ func (handler *OutboundConnectionHandler) Start(ray core.OutboundRay) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler *OutboundConnectionHandler) Create(point *core.Point, config []byte, dest *v2net.Destination) (core.OutboundConnectionHandler, error) {
|
func (handler *OutboundConnectionHandler) Create(point *core.Point, config []byte, dest v2net.Destination) (core.OutboundConnectionHandler, error) {
|
||||||
handler.Destination = dest
|
handler.Destination = dest
|
||||||
return handler, nil
|
return handler, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue