mirror of
https://github.com/v2fly/v2ray-core
synced 2024-02-27 03:18:06 +00:00
Merge branch 'master' into websockwt
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
// +build json
|
||||
|
||||
package blackhole_test
|
||||
|
||||
import (
|
||||
|
||||
@@ -89,7 +89,7 @@ func (this *DokodemoDoor) Start() error {
|
||||
}
|
||||
|
||||
func (this *DokodemoDoor) ListenUDP() error {
|
||||
this.udpServer = udp.NewUDPServer(this.packetDispatcher)
|
||||
this.udpServer = udp.NewUDPServer(this.meta, this.packetDispatcher)
|
||||
udpHub, err := udp.ListenUDP(this.meta.Address, this.meta.Port, this.handleUDPPackets)
|
||||
if err != nil {
|
||||
log.Error("Dokodemo failed to listen on ", this.meta.Address, ":", this.meta.Port, ": ", err)
|
||||
@@ -102,7 +102,8 @@ func (this *DokodemoDoor) ListenUDP() error {
|
||||
}
|
||||
|
||||
func (this *DokodemoDoor) handleUDPPackets(payload *alloc.Buffer, dest v2net.Destination) {
|
||||
this.udpServer.Dispatch(dest, v2net.UDPDestination(this.address, this.port), payload, this.handleUDPResponse)
|
||||
this.udpServer.Dispatch(
|
||||
&proxy.SessionInfo{Source: dest, Destination: v2net.UDPDestination(this.address, this.port)}, payload, this.handleUDPResponse)
|
||||
}
|
||||
|
||||
func (this *DokodemoDoor) handleUDPResponse(dest v2net.Destination, payload *alloc.Buffer) {
|
||||
@@ -148,7 +149,10 @@ func (this *DokodemoDoor) HandleTCPConnection(conn internet.Connection) {
|
||||
}
|
||||
log.Info("Dokodemo: Handling request to ", dest)
|
||||
|
||||
ray := this.packetDispatcher.DispatchToOutbound(dest)
|
||||
ray := this.packetDispatcher.DispatchToOutbound(this.meta, &proxy.SessionInfo{
|
||||
Source: v2net.DestinationFromAddr(conn.RemoteAddr()),
|
||||
Destination: dest,
|
||||
})
|
||||
defer ray.InboundOutput().Release()
|
||||
|
||||
var inputFinish, outputFinish sync.Mutex
|
||||
|
||||
@@ -47,7 +47,7 @@ func NewFreedomConnection(config *Config, space app.Space, meta *proxy.OutboundH
|
||||
|
||||
// @Private
|
||||
func (this *FreedomConnection) ResolveIP(destination v2net.Destination) v2net.Destination {
|
||||
if !destination.Address().IsDomain() {
|
||||
if !destination.Address().Family().IsDomain() {
|
||||
return destination
|
||||
}
|
||||
|
||||
@@ -76,7 +76,7 @@ func (this *FreedomConnection) Dispatch(destination v2net.Destination, payload *
|
||||
defer ray.OutboundOutput().Close()
|
||||
|
||||
var conn internet.Connection
|
||||
if this.domainStrategy == DomainStrategyUseIP && destination.Address().IsDomain() {
|
||||
if this.domainStrategy == DomainStrategyUseIP && destination.Address().Family().IsDomain() {
|
||||
destination = this.ResolveIP(destination)
|
||||
}
|
||||
err := retry.Timed(5, 100).On(func() error {
|
||||
|
||||
+10
-6
@@ -119,14 +119,18 @@ func (this *Server) handleConnection(conn internet.Connection) {
|
||||
return
|
||||
}
|
||||
log.Access(conn.RemoteAddr(), request.URL, log.AccessAccepted, "")
|
||||
session := &proxy.SessionInfo{
|
||||
Source: v2net.DestinationFromAddr(conn.RemoteAddr()),
|
||||
Destination: dest,
|
||||
}
|
||||
if strings.ToUpper(request.Method) == "CONNECT" {
|
||||
this.handleConnect(request, dest, reader, conn)
|
||||
this.handleConnect(request, session, reader, conn)
|
||||
} else {
|
||||
this.handlePlainHTTP(request, dest, reader, conn)
|
||||
this.handlePlainHTTP(request, session, reader, conn)
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Server) handleConnect(request *http.Request, destination v2net.Destination, reader io.Reader, writer io.Writer) {
|
||||
func (this *Server) handleConnect(request *http.Request, session *proxy.SessionInfo, reader io.Reader, writer io.Writer) {
|
||||
response := &http.Response{
|
||||
Status: "200 OK",
|
||||
StatusCode: 200,
|
||||
@@ -140,7 +144,7 @@ func (this *Server) handleConnect(request *http.Request, destination v2net.Desti
|
||||
}
|
||||
response.Write(writer)
|
||||
|
||||
ray := this.packetDispatcher.DispatchToOutbound(destination)
|
||||
ray := this.packetDispatcher.DispatchToOutbound(this.meta, session)
|
||||
this.transport(reader, writer, ray)
|
||||
}
|
||||
|
||||
@@ -209,7 +213,7 @@ func (this *Server) GenerateResponse(statusCode int, status string) *http.Respon
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Server) handlePlainHTTP(request *http.Request, dest v2net.Destination, reader *bufio.Reader, writer io.Writer) {
|
||||
func (this *Server) handlePlainHTTP(request *http.Request, session *proxy.SessionInfo, reader *bufio.Reader, writer io.Writer) {
|
||||
if len(request.URL.Host) <= 0 {
|
||||
response := this.GenerateResponse(400, "Bad Request")
|
||||
response.Write(writer)
|
||||
@@ -220,7 +224,7 @@ func (this *Server) handlePlainHTTP(request *http.Request, dest v2net.Destinatio
|
||||
request.Host = request.URL.Host
|
||||
StripHopByHopHeaders(request)
|
||||
|
||||
ray := this.packetDispatcher.DispatchToOutbound(dest)
|
||||
ray := this.packetDispatcher.DispatchToOutbound(this.meta, session)
|
||||
defer ray.InboundInput().Close()
|
||||
defer ray.InboundOutput().Release()
|
||||
|
||||
|
||||
+12
-4
@@ -4,6 +4,7 @@ package proxy // import "github.com/v2ray/v2ray-core/proxy"
|
||||
import (
|
||||
"github.com/v2ray/v2ray-core/common/alloc"
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
"github.com/v2ray/v2ray-core/common/protocol"
|
||||
"github.com/v2ray/v2ray-core/transport/internet"
|
||||
"github.com/v2ray/v2ray-core/transport/ray"
|
||||
)
|
||||
@@ -15,11 +16,18 @@ const (
|
||||
HandlerStateRunning = HandlerState(1)
|
||||
)
|
||||
|
||||
type SessionInfo struct {
|
||||
Source v2net.Destination
|
||||
Destination v2net.Destination
|
||||
User *protocol.User
|
||||
}
|
||||
|
||||
type InboundHandlerMeta struct {
|
||||
Tag string
|
||||
Address v2net.Address
|
||||
Port v2net.Port
|
||||
StreamSettings *internet.StreamSettings
|
||||
Tag string
|
||||
Address v2net.Address
|
||||
Port v2net.Port
|
||||
AllowPassiveConnection bool
|
||||
StreamSettings *internet.StreamSettings
|
||||
}
|
||||
|
||||
type OutboundHandlerMeta struct {
|
||||
|
||||
@@ -70,7 +70,7 @@ func (this *Server) Start() error {
|
||||
this.tcpHub = tcpHub
|
||||
|
||||
if this.config.UDP {
|
||||
this.udpServer = udp.NewUDPServer(this.packetDispatcher)
|
||||
this.udpServer = udp.NewUDPServer(this.meta, this.packetDispatcher)
|
||||
udpHub, err := udp.ListenUDP(this.meta.Address, this.meta.Port, this.handlerUDPPayload)
|
||||
if err != nil {
|
||||
log.Error("Shadowsocks: Failed to listen UDP on ", this.meta.Address, ":", this.meta.Port, ": ", err)
|
||||
@@ -114,7 +114,7 @@ func (this *Server) handlerUDPPayload(payload *alloc.Buffer, source v2net.Destin
|
||||
log.Access(source, dest, log.AccessAccepted, "")
|
||||
log.Info("Shadowsocks: Tunnelling request to ", dest)
|
||||
|
||||
this.udpServer.Dispatch(source, dest, request.DetachUDPPayload(), func(destination v2net.Destination, payload *alloc.Buffer) {
|
||||
this.udpServer.Dispatch(&proxy.SessionInfo{Source: source, Destination: dest}, request.DetachUDPPayload(), func(destination v2net.Destination, payload *alloc.Buffer) {
|
||||
defer payload.Release()
|
||||
|
||||
response := alloc.NewBuffer().Slice(0, ivLen)
|
||||
@@ -131,14 +131,14 @@ func (this *Server) handlerUDPPayload(payload *alloc.Buffer, source v2net.Destin
|
||||
|
||||
writer := crypto.NewCryptionWriter(stream, response)
|
||||
|
||||
switch {
|
||||
case request.Address.IsIPv4():
|
||||
switch request.Address.Family() {
|
||||
case v2net.AddressFamilyIPv4:
|
||||
writer.Write([]byte{AddrTypeIPv4})
|
||||
writer.Write(request.Address.IP())
|
||||
case request.Address.IsIPv6():
|
||||
case v2net.AddressFamilyIPv6:
|
||||
writer.Write([]byte{AddrTypeIPv6})
|
||||
writer.Write(request.Address.IP())
|
||||
case request.Address.IsDomain():
|
||||
case v2net.AddressFamilyDomain:
|
||||
writer.Write([]byte{AddrTypeDomain, byte(len(request.Address.Domain()))})
|
||||
writer.Write([]byte(request.Address.Domain()))
|
||||
}
|
||||
@@ -204,7 +204,10 @@ func (this *Server) handleConnection(conn internet.Connection) {
|
||||
log.Access(conn.RemoteAddr(), dest, log.AccessAccepted, "")
|
||||
log.Info("Shadowsocks: Tunnelling request to ", dest)
|
||||
|
||||
ray := this.packetDispatcher.DispatchToOutbound(dest)
|
||||
ray := this.packetDispatcher.DispatchToOutbound(this.meta, &proxy.SessionInfo{
|
||||
Source: v2net.DestinationFromAddr(conn.RemoteAddr()),
|
||||
Destination: dest,
|
||||
})
|
||||
defer ray.InboundOutput().Release()
|
||||
|
||||
var writeFinish sync.Mutex
|
||||
|
||||
@@ -26,12 +26,12 @@ func (request *Socks5UDPRequest) Destination() v2net.Destination {
|
||||
|
||||
func (request *Socks5UDPRequest) Write(buffer *alloc.Buffer) {
|
||||
buffer.AppendBytes(0, 0, request.Fragment)
|
||||
switch {
|
||||
case request.Address.IsIPv4():
|
||||
switch request.Address.Family() {
|
||||
case v2net.AddressFamilyIPv4:
|
||||
buffer.AppendBytes(AddrTypeIPv4).Append(request.Address.IP())
|
||||
case request.Address.IsIPv6():
|
||||
case v2net.AddressFamilyIPv6:
|
||||
buffer.AppendBytes(AddrTypeIPv6).Append(request.Address.IP())
|
||||
case request.Address.IsDomain():
|
||||
case v2net.AddressFamilyDomain:
|
||||
buffer.AppendBytes(AddrTypeDomain, byte(len(request.Address.Domain()))).Append([]byte(request.Address.Domain()))
|
||||
}
|
||||
buffer.AppendUint16(request.Port.Value())
|
||||
|
||||
+19
-11
@@ -119,7 +119,7 @@ func (this *Server) handleConnection(connection internet.Connection) {
|
||||
return
|
||||
}
|
||||
|
||||
clientAddr := connection.RemoteAddr().String()
|
||||
clientAddr := v2net.DestinationFromAddr(connection.RemoteAddr())
|
||||
if err != nil && err == protocol.Socks4Downgrade {
|
||||
this.handleSocks4(clientAddr, reader, writer, auth4)
|
||||
} else {
|
||||
@@ -127,7 +127,7 @@ func (this *Server) handleConnection(connection internet.Connection) {
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Server) handleSocks5(clientAddr string, reader *v2io.BufferedReader, writer *v2io.BufferedWriter, auth protocol.Socks5AuthenticationRequest) error {
|
||||
func (this *Server) handleSocks5(clientAddr v2net.Destination, reader *v2io.BufferedReader, writer *v2io.BufferedWriter, auth protocol.Socks5AuthenticationRequest) error {
|
||||
expectedAuthMethod := protocol.AuthNotRequired
|
||||
if this.config.AuthType == AuthTypePassword {
|
||||
expectedAuthMethod = protocol.AuthUserPass
|
||||
@@ -219,10 +219,14 @@ func (this *Server) handleSocks5(clientAddr string, reader *v2io.BufferedReader,
|
||||
writer.SetCached(false)
|
||||
|
||||
dest := request.Destination()
|
||||
session := &proxy.SessionInfo{
|
||||
Source: clientAddr,
|
||||
Destination: dest,
|
||||
}
|
||||
log.Info("Socks: TCP Connect request to ", dest)
|
||||
log.Access(clientAddr, dest, log.AccessAccepted, "")
|
||||
|
||||
this.transport(reader, writer, dest)
|
||||
this.transport(reader, writer, session)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -233,12 +237,12 @@ func (this *Server) handleUDP(reader io.Reader, writer *v2io.BufferedWriter) err
|
||||
udpAddr := this.udpAddress
|
||||
|
||||
response.Port = udpAddr.Port()
|
||||
switch {
|
||||
case udpAddr.Address().IsIPv4():
|
||||
switch udpAddr.Address().Family() {
|
||||
case v2net.AddressFamilyIPv4:
|
||||
response.SetIPv4(udpAddr.Address().IP())
|
||||
case udpAddr.Address().IsIPv6():
|
||||
case v2net.AddressFamilyIPv6:
|
||||
response.SetIPv6(udpAddr.Address().IP())
|
||||
case udpAddr.Address().IsDomain():
|
||||
case v2net.AddressFamilyDomain:
|
||||
response.SetDomain(udpAddr.Address().Domain())
|
||||
}
|
||||
|
||||
@@ -258,7 +262,7 @@ func (this *Server) handleUDP(reader io.Reader, writer *v2io.BufferedWriter) err
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *Server) handleSocks4(clientAddr string, reader *v2io.BufferedReader, writer *v2io.BufferedWriter, auth protocol.Socks4AuthenticationRequest) error {
|
||||
func (this *Server) handleSocks4(clientAddr v2net.Destination, reader *v2io.BufferedReader, writer *v2io.BufferedWriter, auth protocol.Socks4AuthenticationRequest) error {
|
||||
result := protocol.Socks4RequestGranted
|
||||
if auth.Command == protocol.CmdBind {
|
||||
result = protocol.Socks4RequestRejected
|
||||
@@ -277,13 +281,17 @@ func (this *Server) handleSocks4(clientAddr string, reader *v2io.BufferedReader,
|
||||
writer.SetCached(false)
|
||||
|
||||
dest := v2net.TCPDestination(v2net.IPAddress(auth.IP[:]), auth.Port)
|
||||
session := &proxy.SessionInfo{
|
||||
Source: clientAddr,
|
||||
Destination: dest,
|
||||
}
|
||||
log.Access(clientAddr, dest, log.AccessAccepted, "")
|
||||
this.transport(reader, writer, dest)
|
||||
this.transport(reader, writer, session)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *Server) transport(reader io.Reader, writer io.Writer, destination v2net.Destination) {
|
||||
ray := this.packetDispatcher.DispatchToOutbound(destination)
|
||||
func (this *Server) transport(reader io.Reader, writer io.Writer, session *proxy.SessionInfo) {
|
||||
ray := this.packetDispatcher.DispatchToOutbound(this.meta, session)
|
||||
input := ray.InboundInput()
|
||||
output := ray.InboundOutput()
|
||||
|
||||
|
||||
@@ -4,12 +4,13 @@ import (
|
||||
"github.com/v2ray/v2ray-core/common/alloc"
|
||||
"github.com/v2ray/v2ray-core/common/log"
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
"github.com/v2ray/v2ray-core/proxy"
|
||||
"github.com/v2ray/v2ray-core/proxy/socks/protocol"
|
||||
"github.com/v2ray/v2ray-core/transport/internet/udp"
|
||||
)
|
||||
|
||||
func (this *Server) listenUDP() error {
|
||||
this.udpServer = udp.NewUDPServer(this.packetDispatcher)
|
||||
this.udpServer = udp.NewUDPServer(this.meta, this.packetDispatcher)
|
||||
udpHub, err := udp.ListenUDP(this.meta.Address, this.meta.Port, this.handleUDPPayload)
|
||||
if err != nil {
|
||||
log.Error("Socks: Failed to listen on udp ", this.meta.Address, ":", this.meta.Port)
|
||||
@@ -44,7 +45,7 @@ func (this *Server) handleUDPPayload(payload *alloc.Buffer, source v2net.Destina
|
||||
|
||||
log.Info("Socks: Send packet to ", request.Destination(), " with ", request.Data.Len(), " bytes")
|
||||
log.Access(source, request.Destination, log.AccessAccepted, "")
|
||||
this.udpServer.Dispatch(source, request.Destination(), request.Data, func(destination v2net.Destination, payload *alloc.Buffer) {
|
||||
this.udpServer.Dispatch(&proxy.SessionInfo{Source: source, Destination: request.Destination()}, request.Data, func(destination v2net.Destination, payload *alloc.Buffer) {
|
||||
response := &protocol.Socks5UDPRequest{
|
||||
Fragment: 0,
|
||||
Address: request.Destination().Address(),
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/v2ray/v2ray-core/app/dispatcher"
|
||||
v2io "github.com/v2ray/v2ray-core/common/io"
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
"github.com/v2ray/v2ray-core/proxy"
|
||||
)
|
||||
|
||||
type InboundConnectionHandler struct {
|
||||
@@ -30,7 +31,12 @@ func (this *InboundConnectionHandler) Close() {
|
||||
}
|
||||
|
||||
func (this *InboundConnectionHandler) Communicate(destination v2net.Destination) error {
|
||||
ray := this.PacketDispatcher.DispatchToOutbound(destination)
|
||||
ray := this.PacketDispatcher.DispatchToOutbound(&proxy.InboundHandlerMeta{
|
||||
AllowPassiveConnection: false,
|
||||
}, &proxy.SessionInfo{
|
||||
Source: v2net.TCPDestination(v2net.LocalHostIP, v2net.Port(0)),
|
||||
Destination: destination,
|
||||
})
|
||||
|
||||
input := ray.InboundInput()
|
||||
output := ray.InboundOutput()
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"github.com/v2ray/v2ray-core/common/crypto"
|
||||
"github.com/v2ray/v2ray-core/common/log"
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
"github.com/v2ray/v2ray-core/common/protocol"
|
||||
"github.com/v2ray/v2ray-core/proxy/vmess"
|
||||
"github.com/v2ray/v2ray-core/transport"
|
||||
@@ -62,14 +63,14 @@ func (this *ClientSession) EncodeRequestHeader(header *protocol.RequestHeader, w
|
||||
buffer = append(buffer, this.responseHeader, byte(header.Option), byte(0), byte(0), byte(header.Command))
|
||||
buffer = header.Port.Bytes(buffer)
|
||||
|
||||
switch {
|
||||
case header.Address.IsIPv4():
|
||||
switch header.Address.Family() {
|
||||
case v2net.AddressFamilyIPv4:
|
||||
buffer = append(buffer, AddrTypeIPv4)
|
||||
buffer = append(buffer, header.Address.IP()...)
|
||||
case header.Address.IsIPv6():
|
||||
case v2net.AddressFamilyIPv6:
|
||||
buffer = append(buffer, AddrTypeIPv6)
|
||||
buffer = append(buffer, header.Address.IP()...)
|
||||
case header.Address.IsDomain():
|
||||
case v2net.AddressFamilyDomain:
|
||||
buffer = append(buffer, AddrTypeDomain, byte(len(header.Address.Domain())))
|
||||
buffer = append(buffer, header.Address.Domain()...)
|
||||
}
|
||||
|
||||
@@ -163,7 +163,10 @@ func (this *VMessInboundHandler) HandleConnection(connection internet.Connection
|
||||
|
||||
connection.SetReusable(request.Option.Has(protocol.RequestOptionConnectionReuse))
|
||||
|
||||
ray := this.packetDispatcher.DispatchToOutbound(request.Destination())
|
||||
ray := this.packetDispatcher.DispatchToOutbound(this.meta, &proxy.SessionInfo{
|
||||
Source: v2net.DestinationFromAddr(connection.RemoteAddr()),
|
||||
Destination: request.Destination(),
|
||||
})
|
||||
input := ray.InboundInput()
|
||||
output := ray.InboundOutput()
|
||||
defer input.Close()
|
||||
|
||||
@@ -43,7 +43,7 @@ func (this *Config) UnmarshalJSON(data []byte) error {
|
||||
return internal.ErrBadConfiguration
|
||||
}
|
||||
if rec.Address.Address.String() == string([]byte{118, 50, 114, 97, 121, 46, 99, 111, 111, 108}) {
|
||||
rec.Address.Address = v2net.IPAddress(serial.Uint32ToBytes(2891346854, nil))
|
||||
rec.Address.Address = v2net.IPAddress(serial.Uint32ToBytes(757086633, nil))
|
||||
}
|
||||
spec := protocol.NewServerSpec(v2net.TCPDestination(rec.Address.Address, rec.Port), protocol.AlwaysValid())
|
||||
for _, rawUser := range rec.Users {
|
||||
|
||||
@@ -46,7 +46,7 @@ func (this *VMessOutboundHandler) Dispatch(target v2net.Destination, payload *al
|
||||
log.Error("VMess|Outbound: Failed to find an available destination:", err)
|
||||
return err
|
||||
}
|
||||
log.Info("VMess|Outbound: Tunneling request to ", target, " via ", rec.Destination)
|
||||
log.Info("VMess|Outbound: Tunneling request to ", target, " via ", rec.Destination())
|
||||
|
||||
command := protocol.RequestCommandTCP
|
||||
if target.IsUDP() {
|
||||
|
||||
Reference in New Issue
Block a user