1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 10:08:15 -05:00

Merged latest master

This commit is contained in:
Shelikhoo 2016-06-12 12:58:20 +08:00
commit a9ef8d27bd
No known key found for this signature in database
GPG Key ID: 7791BDB0709ABD21
17 changed files with 115 additions and 30 deletions

View File

@ -109,12 +109,12 @@ func (this *ClientSession) DecodeResponseHeader(reader io.Reader) (*protocol.Res
_, err := io.ReadFull(this.responseReader, buffer.Value[:4]) _, err := io.ReadFull(this.responseReader, buffer.Value[:4])
if err != nil { if err != nil {
log.Error("Raw: Failed to read response header: ", err) log.Info("Raw: Failed to read response header: ", err)
return nil, err return nil, err
} }
if buffer.Value[0] != this.responseHeader { if buffer.Value[0] != this.responseHeader {
log.Warning("Raw: Unexpected response header. Expecting %d, but actually %d", this.responseHeader, buffer.Value[0]) log.Info("Raw: Unexpected response header. Expecting ", this.responseHeader, " but actually ", buffer.Value[0])
return nil, transport.ErrorCorruptedPacket return nil, transport.ErrorCorruptedPacket
} }
@ -127,7 +127,7 @@ func (this *ClientSession) DecodeResponseHeader(reader io.Reader) (*protocol.Res
dataLen := int(buffer.Value[3]) dataLen := int(buffer.Value[3])
_, err := io.ReadFull(this.responseReader, buffer.Value[:dataLen]) _, err := io.ReadFull(this.responseReader, buffer.Value[:dataLen])
if err != nil { if err != nil {
log.Error("Raw: Failed to read response command: ", err) log.Info("Raw: Failed to read response command: ", err)
return nil, err return nil, err
} }
data := buffer.Value[:dataLen] data := buffer.Value[:dataLen]

View File

@ -4,6 +4,7 @@ package blackhole
import ( import (
"encoding/json" "encoding/json"
"errors"
"github.com/v2ray/v2ray-core/common/loader" "github.com/v2ray/v2ray-core/common/loader"
"github.com/v2ray/v2ray-core/proxy/internal" "github.com/v2ray/v2ray-core/proxy/internal"
@ -15,7 +16,7 @@ func (this *Config) UnmarshalJSON(data []byte) error {
} }
jsonConfig := new(JSONConfig) jsonConfig := new(JSONConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil { if err := json.Unmarshal(data, jsonConfig); err != nil {
return err return errors.New("Blackhole: Failed to parse config: " + err.Error())
} }
this.Response = new(NoneResponse) this.Response = new(NoneResponse)
@ -25,7 +26,7 @@ func (this *Config) UnmarshalJSON(data []byte) error {
loader.RegisterCreator("http", func() interface{} { return new(HTTPResponse) }) loader.RegisterCreator("http", func() interface{} { return new(HTTPResponse) })
response, err := loader.Load(jsonConfig.Response) response, err := loader.Load(jsonConfig.Response)
if err != nil { if err != nil {
return err return errors.New("Blackhole: Failed to parse response config: " + err.Error())
} }
this.Response = response.(Response) this.Response = response.(Response)
} }

View File

@ -5,8 +5,9 @@ import (
) )
type Config struct { type Config struct {
Address v2net.Address FollowRedirect bool
Port v2net.Port Address v2net.Address
Network *v2net.NetworkList Port v2net.Port
Timeout int Network *v2net.NetworkList
Timeout int
} }

View File

@ -4,6 +4,7 @@ package dokodemo
import ( import (
"encoding/json" "encoding/json"
"errors"
v2net "github.com/v2ray/v2ray-core/common/net" v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/proxy/internal" "github.com/v2ray/v2ray-core/proxy/internal"
@ -15,15 +16,17 @@ func (this *Config) UnmarshalJSON(data []byte) error {
PortValue v2net.Port `json:"port"` PortValue v2net.Port `json:"port"`
NetworkList *v2net.NetworkList `json:"network"` NetworkList *v2net.NetworkList `json:"network"`
TimeoutValue int `json:"timeout"` TimeoutValue int `json:"timeout"`
Redirect bool `json:"followRedirect"`
} }
rawConfig := new(DokodemoConfig) rawConfig := new(DokodemoConfig)
if err := json.Unmarshal(data, rawConfig); err != nil { if err := json.Unmarshal(data, rawConfig); err != nil {
return err return errors.New("Dokodemo: Failed to parse config: " + err.Error())
} }
this.Address = rawConfig.Host.Address this.Address = rawConfig.Host.Address
this.Port = rawConfig.PortValue this.Port = rawConfig.PortValue
this.Network = rawConfig.NetworkList this.Network = rawConfig.NetworkList
this.Timeout = rawConfig.TimeoutValue this.Timeout = rawConfig.TimeoutValue
this.FollowRedirect = rawConfig.Redirect
return nil return nil
} }

View File

@ -129,7 +129,16 @@ func (this *DokodemoDoor) ListenTCP() error {
func (this *DokodemoDoor) HandleTCPConnection(conn *hub.Connection) { func (this *DokodemoDoor) HandleTCPConnection(conn *hub.Connection) {
defer conn.Close() defer conn.Close()
ray := this.packetDispatcher.DispatchToOutbound(v2net.TCPDestination(this.address, this.port)) dest := v2net.TCPDestination(this.address, this.port)
if this.config.FollowRedirect {
originalDest := GetOriginalDestination(conn)
if originalDest != nil {
log.Info("Dokodemo: Following redirect to: ", originalDest)
dest = originalDest
}
}
ray := this.packetDispatcher.DispatchToOutbound(dest)
defer ray.InboundOutput().Release() defer ray.InboundOutput().Release()
var inputFinish, outputFinish sync.Mutex var inputFinish, outputFinish sync.Mutex

View File

@ -0,0 +1,30 @@
// +build linux
package dokodemo
import (
"syscall"
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/transport/hub"
)
const SO_ORIGINAL_DST = 80
func GetOriginalDestination(conn *hub.Connection) v2net.Destination {
fd, err := conn.SysFd()
if err != nil {
log.Info("Dokodemo: Failed to get original destination: ", err)
return nil
}
addr, err := syscall.GetsockoptIPv6Mreq(fd, syscall.IPPROTO_IP, SO_ORIGINAL_DST)
if err != nil {
log.Info("Dokodemo: Failed to call getsockopt: ", err)
return nil
}
ip := v2net.IPAddress(addr.Multiaddr[4:8])
port := uint16(addr.Multiaddr[2])<<8 + uint16(addr.Multiaddr[3])
return v2net.TCPDestination(ip, v2net.Port(port))
}

View File

@ -0,0 +1,12 @@
// +build !linux
package dokodemo
import (
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/transport/hub"
)
func GetOriginalDestination(conn *hub.Connection) v2net.Destination {
return nil
}

View File

@ -4,6 +4,7 @@ package freedom
import ( import (
"encoding/json" "encoding/json"
"errors"
"strings" "strings"
"github.com/v2ray/v2ray-core/proxy/internal" "github.com/v2ray/v2ray-core/proxy/internal"
@ -16,7 +17,7 @@ func (this *Config) UnmarshalJSON(data []byte) error {
} }
jsonConfig := new(JsonConfig) jsonConfig := new(JsonConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil { if err := json.Unmarshal(data, jsonConfig); err != nil {
return err return errors.New("Freedom: Failed to parse config: " + err.Error())
} }
this.DomainStrategy = DomainStrategyAsIs this.DomainStrategy = DomainStrategyAsIs
domainStrategy := strings.ToLower(jsonConfig.DomainStrategy) domainStrategy := strings.ToLower(jsonConfig.DomainStrategy)

View File

@ -5,6 +5,7 @@ package http
import ( import (
"crypto/tls" "crypto/tls"
"encoding/json" "encoding/json"
"errors"
"github.com/v2ray/v2ray-core/proxy/internal" "github.com/v2ray/v2ray-core/proxy/internal"
) )
@ -18,7 +19,7 @@ func (this *CertificateConfig) UnmarshalJSON(data []byte) error {
} }
jsonConfig := new(JsonConfig) jsonConfig := new(JsonConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil { if err := json.Unmarshal(data, jsonConfig); err != nil {
return err return errors.New("HTTP: Failed to parse certificate config: " + err.Error())
} }
cert, err := tls.LoadX509KeyPair(jsonConfig.CertFile, jsonConfig.KeyFile) cert, err := tls.LoadX509KeyPair(jsonConfig.CertFile, jsonConfig.KeyFile)
@ -38,7 +39,7 @@ func (this *TLSConfig) UnmarshalJSON(data []byte) error {
} }
jsonConfig := new(JsonConfig) jsonConfig := new(JsonConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil { if err := json.Unmarshal(data, jsonConfig); err != nil {
return err return errors.New("HTTP: Failed to parse TLS config: " + err.Error())
} }
this.Enabled = jsonConfig.Enabled this.Enabled = jsonConfig.Enabled
@ -53,7 +54,7 @@ func (this *Config) UnmarshalJSON(data []byte) error {
} }
jsonConfig := new(JsonConfig) jsonConfig := new(JsonConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil { if err := json.Unmarshal(data, jsonConfig); err != nil {
return err return errors.New("HTTP: Failed to parse config: " + err.Error())
} }
this.TLSConfig = jsonConfig.Tls this.TLSConfig = jsonConfig.Tls

View File

@ -4,6 +4,7 @@ package shadowsocks
import ( import (
"encoding/json" "encoding/json"
"errors"
"strings" "strings"
"github.com/v2ray/v2ray-core/common/log" "github.com/v2ray/v2ray-core/common/log"
@ -21,7 +22,7 @@ func (this *Config) UnmarshalJSON(data []byte) error {
} }
jsonConfig := new(JsonConfig) jsonConfig := new(JsonConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil { if err := json.Unmarshal(data, jsonConfig); err != nil {
return err return errors.New("Shadowsocks: Failed to parse config: " + err.Error())
} }
this.UDP = jsonConfig.UDP this.UDP = jsonConfig.UDP

View File

@ -4,6 +4,7 @@ package socks
import ( import (
"encoding/json" "encoding/json"
"errors"
"github.com/v2ray/v2ray-core/common/log" "github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net" v2net "github.com/v2ray/v2ray-core/common/net"
@ -30,7 +31,7 @@ func (this *Config) UnmarshalJSON(data []byte) error {
rawConfig := new(SocksConfig) rawConfig := new(SocksConfig)
if err := json.Unmarshal(data, rawConfig); err != nil { if err := json.Unmarshal(data, rawConfig); err != nil {
return err return errors.New("Socks: Failed to parse config: " + err.Error())
} }
if rawConfig.AuthMethod == AuthMethodNoAuth { if rawConfig.AuthMethod == AuthMethodNoAuth {
this.AuthType = AuthTypeNoAuth this.AuthType = AuthTypeNoAuth

View File

@ -4,6 +4,7 @@ package inbound
import ( import (
"encoding/json" "encoding/json"
"errors"
"github.com/v2ray/v2ray-core/common/protocol" "github.com/v2ray/v2ray-core/common/protocol"
"github.com/v2ray/v2ray-core/proxy/internal" "github.com/v2ray/v2ray-core/proxy/internal"
@ -15,7 +16,7 @@ func (this *DetourConfig) UnmarshalJSON(data []byte) error {
} }
jsonConfig := new(JsonDetourConfig) jsonConfig := new(JsonDetourConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil { if err := json.Unmarshal(data, jsonConfig); err != nil {
return err return errors.New("VMessIn: Failed to parse detour config: " + err.Error())
} }
this.ToTag = jsonConfig.ToTag this.ToTag = jsonConfig.ToTag
return nil return nil
@ -27,7 +28,7 @@ func (this *FeaturesConfig) UnmarshalJSON(data []byte) error {
} }
jsonConfig := new(JsonFeaturesConfig) jsonConfig := new(JsonFeaturesConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil { if err := json.Unmarshal(data, jsonConfig); err != nil {
return err return errors.New("VMessIn: Failed to parse features config: " + err.Error())
} }
this.Detour = jsonConfig.Detour this.Detour = jsonConfig.Detour
return nil return nil
@ -40,7 +41,7 @@ func (this *DefaultConfig) UnmarshalJSON(data []byte) error {
} }
jsonConfig := new(JsonDefaultConfig) jsonConfig := new(JsonDefaultConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil { if err := json.Unmarshal(data, jsonConfig); err != nil {
return err return errors.New("VMessIn: Failed to parse default config: " + err.Error())
} }
this.AlterIDs = jsonConfig.AlterIDs this.AlterIDs = jsonConfig.AlterIDs
if this.AlterIDs == 0 { if this.AlterIDs == 0 {
@ -59,7 +60,7 @@ func (this *Config) UnmarshalJSON(data []byte) error {
} }
jsonConfig := new(JsonConfig) jsonConfig := new(JsonConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil { if err := json.Unmarshal(data, jsonConfig); err != nil {
return err return errors.New("VMessIn: Failed to parse config: " + err.Error())
} }
this.AllowedUsers = jsonConfig.Users this.AllowedUsers = jsonConfig.Users
this.Features = jsonConfig.Features // Backward compatibility this.Features = jsonConfig.Features // Backward compatibility

View File

@ -4,6 +4,7 @@ package outbound
import ( import (
"encoding/json" "encoding/json"
"errors"
"github.com/v2ray/v2ray-core/common/log" "github.com/v2ray/v2ray-core/common/log"
"github.com/v2ray/v2ray-core/proxy/internal" "github.com/v2ray/v2ray-core/proxy/internal"
@ -16,10 +17,10 @@ func (this *Config) UnmarshalJSON(data []byte) error {
rawOutbound := &RawOutbound{} rawOutbound := &RawOutbound{}
err := json.Unmarshal(data, rawOutbound) err := json.Unmarshal(data, rawOutbound)
if err != nil { if err != nil {
return err return errors.New("VMessOut: Failed to parse config: " + err.Error())
} }
if len(rawOutbound.Receivers) == 0 { if len(rawOutbound.Receivers) == 0 {
log.Error("VMess: 0 VMess receiver configured.") log.Error("VMessOut: 0 VMess receiver configured.")
return internal.ErrorBadConfiguration return internal.ErrorBadConfiguration
} }
this.Receivers = rawOutbound.Receivers this.Receivers = rawOutbound.Receivers

View File

@ -34,7 +34,7 @@ func (this *Config) UnmarshalJSON(data []byte) error {
} }
jsonConfig := new(JsonConfig) jsonConfig := new(JsonConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil { if err := json.Unmarshal(data, jsonConfig); err != nil {
return err return errors.New("Point: Failed to parse config: " + err.Error())
} }
this.Port = jsonConfig.Port this.Port = jsonConfig.Port
this.LogConfig = jsonConfig.LogConfig this.LogConfig = jsonConfig.LogConfig
@ -65,7 +65,7 @@ func (this *InboundConnectionConfig) UnmarshalJSON(data []byte) error {
jsonConfig := new(JsonConfig) jsonConfig := new(JsonConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil { if err := json.Unmarshal(data, jsonConfig); err != nil {
return err return errors.New("Point: Failed to parse inbound config: " + err.Error())
} }
this.Port = v2net.Port(jsonConfig.Port) this.Port = v2net.Port(jsonConfig.Port)
this.ListenOn = v2net.AnyIP this.ListenOn = v2net.AnyIP
@ -89,7 +89,7 @@ func (this *OutboundConnectionConfig) UnmarshalJSON(data []byte) error {
} }
jsonConfig := new(JsonConnectionConfig) jsonConfig := new(JsonConnectionConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil { if err := json.Unmarshal(data, jsonConfig); err != nil {
return err return errors.New("Point: Failed to parse outbound config: " + err.Error())
} }
this.Protocol = jsonConfig.Protocol this.Protocol = jsonConfig.Protocol
this.Settings = jsonConfig.Settings this.Settings = jsonConfig.Settings
@ -112,7 +112,7 @@ func (this *LogConfig) UnmarshalJSON(data []byte) error {
} }
jsonConfig := new(JsonLogConfig) jsonConfig := new(JsonLogConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil { if err := json.Unmarshal(data, jsonConfig); err != nil {
return err return errors.New("Point: Failed to parse log config: " + err.Error())
} }
this.AccessLog = jsonConfig.AccessLog this.AccessLog = jsonConfig.AccessLog
this.ErrorLog = jsonConfig.ErrorLog this.ErrorLog = jsonConfig.ErrorLog
@ -141,7 +141,7 @@ func (this *InboundDetourAllocationConfig) UnmarshalJSON(data []byte) error {
} }
jsonConfig := new(JsonInboundDetourAllocationConfig) jsonConfig := new(JsonInboundDetourAllocationConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil { if err := json.Unmarshal(data, jsonConfig); err != nil {
return err return errors.New("Point: Failed to parse inbound detour allocation config: " + err.Error())
} }
this.Strategy = jsonConfig.Strategy this.Strategy = jsonConfig.Strategy
this.Concurrency = jsonConfig.Concurrency this.Concurrency = jsonConfig.Concurrency
@ -171,7 +171,7 @@ func (this *InboundDetourConfig) UnmarshalJSON(data []byte) error {
} }
jsonConfig := new(JsonInboundDetourConfig) jsonConfig := new(JsonInboundDetourConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil { if err := json.Unmarshal(data, jsonConfig); err != nil {
return err return errors.New("Point: Failed to parse inbound detour config: " + err.Error())
} }
if jsonConfig.PortRange == nil { if jsonConfig.PortRange == nil {
log.Error("Point: Port range not specified in InboundDetour.") log.Error("Point: Port range not specified in InboundDetour.")
@ -207,7 +207,7 @@ func (this *OutboundDetourConfig) UnmarshalJSON(data []byte) error {
} }
jsonConfig := new(JsonOutboundDetourConfig) jsonConfig := new(JsonOutboundDetourConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil { if err := json.Unmarshal(data, jsonConfig); err != nil {
return err return errors.New("Point: Failed to parse outbound detour config: " + err.Error())
} }
this.Protocol = jsonConfig.Protocol this.Protocol = jsonConfig.Protocol
this.Tag = jsonConfig.Tag this.Tag = jsonConfig.Tag

View File

@ -2,12 +2,14 @@ package transport
import "github.com/v2ray/v2ray-core/transport/hub/kcpv" import "github.com/v2ray/v2ray-core/transport/hub/kcpv"
// Config for V2Ray transport layer.
type Config struct { type Config struct {
ConnectionReuse bool ConnectionReuse bool
enableKcp bool enableKcp bool
kcpConfig *kcpv.Config kcpConfig *kcpv.Config
} }
// Apply applies this Config.
func (this *Config) Apply() error { func (this *Config) Apply() error {
if this.ConnectionReuse { if this.ConnectionReuse {
connectionReuse = true connectionReuse = true

View File

@ -1,12 +1,18 @@
package hub package hub
import ( import (
"errors"
"net" "net"
"reflect"
"time" "time"
"github.com/v2ray/v2ray-core/transport" "github.com/v2ray/v2ray-core/transport"
) )
var (
ErrInvalidConn = errors.New("Invalid Connection.")
)
type ConnectionHandler func(*Connection) type ConnectionHandler func(*Connection)
type ConnectionManager interface { type ConnectionManager interface {
@ -73,3 +79,17 @@ func (this *Connection) SetReusable(reusable bool) {
func (this *Connection) Reusable() bool { func (this *Connection) Reusable() bool {
return this.reusable return this.reusable
} }
func (this *Connection) SysFd() (int, error) {
cv := reflect.ValueOf(this.conn)
switch ce := cv.Elem(); ce.Kind() {
case reflect.Struct:
netfd := ce.FieldByName("conn").FieldByName("fd")
switch fe := netfd.Elem(); fe.Kind() {
case reflect.Struct:
fd := fe.FieldByName("sysfd")
return int(fd.Int()), nil
}
}
return 0, ErrInvalidConn
}

View File

@ -8,6 +8,7 @@ var (
KcpConfig *kcpv.Config KcpConfig *kcpv.Config
) )
// IsConnectionReusable returns true if V2Ray is trying to reuse TCP connections.
func IsConnectionReusable() bool { func IsConnectionReusable() bool {
return connectionReuse return connectionReuse
} }