mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-13 04:46:40 -05:00
update context functions
This commit is contained in:
parent
d270a99d4f
commit
d04d92c187
@ -53,7 +53,7 @@ func (v *DefaultDispatcher) Dispatch(ctx context.Context, destination net.Destin
|
|||||||
panic("Dispatcher: Invalid destination.")
|
panic("Dispatcher: Invalid destination.")
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx = proxy.ContextWithDestination(ctx, destination)
|
ctx = proxy.ContextWithTarget(ctx, destination)
|
||||||
|
|
||||||
if v.router != nil {
|
if v.router != nil {
|
||||||
if tag, err := v.router.TakeDetour(ctx); err == nil {
|
if tag, err := v.router.TakeDetour(ctx); err == nil {
|
||||||
|
@ -44,13 +44,13 @@ func (w *tcpWorker) callback(conn internet.Connection) {
|
|||||||
if w.recvOrigDest {
|
if w.recvOrigDest {
|
||||||
dest := tcp.GetOriginalDestination(conn)
|
dest := tcp.GetOriginalDestination(conn)
|
||||||
if dest.IsValid() {
|
if dest.IsValid() {
|
||||||
ctx = proxy.ContextWithOriginalDestination(ctx, dest)
|
ctx = proxy.ContextWithOriginalTarget(ctx, dest)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(w.tag) > 0 {
|
if len(w.tag) > 0 {
|
||||||
ctx = proxy.ContextWithInboundTag(ctx, w.tag)
|
ctx = proxy.ContextWithInboundTag(ctx, w.tag)
|
||||||
}
|
}
|
||||||
ctx = proxy.ContextWithInboundDestination(ctx, v2net.TCPDestination(w.address, w.port))
|
ctx = proxy.ContextWithInboundEntryPoint(ctx, v2net.TCPDestination(w.address, w.port))
|
||||||
ctx = proxy.ContextWithSource(ctx, v2net.DestinationFromAddr(conn.RemoteAddr()))
|
ctx = proxy.ContextWithSource(ctx, v2net.DestinationFromAddr(conn.RemoteAddr()))
|
||||||
if err := w.proxy.Process(ctx, v2net.Network_TCP, conn, w.dispatcher); err != nil {
|
if err := w.proxy.Process(ctx, v2net.Network_TCP, conn, w.dispatcher); err != nil {
|
||||||
log.Info("Proxyman|TCPWorker: Connection ends with ", err)
|
log.Info("Proxyman|TCPWorker: Connection ends with ", err)
|
||||||
@ -205,13 +205,13 @@ func (w *udpWorker) callback(b *buf.Buffer, source v2net.Destination, originalDe
|
|||||||
ctx, cancel := context.WithCancel(ctx)
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
conn.cancel = cancel
|
conn.cancel = cancel
|
||||||
if originalDest.IsValid() {
|
if originalDest.IsValid() {
|
||||||
ctx = proxy.ContextWithOriginalDestination(ctx, originalDest)
|
ctx = proxy.ContextWithOriginalTarget(ctx, originalDest)
|
||||||
}
|
}
|
||||||
if len(w.tag) > 0 {
|
if len(w.tag) > 0 {
|
||||||
ctx = proxy.ContextWithInboundTag(ctx, w.tag)
|
ctx = proxy.ContextWithInboundTag(ctx, w.tag)
|
||||||
}
|
}
|
||||||
ctx = proxy.ContextWithSource(ctx, source)
|
ctx = proxy.ContextWithSource(ctx, source)
|
||||||
ctx = proxy.ContextWithInboundDestination(ctx, v2net.UDPDestination(w.address, w.port))
|
ctx = proxy.ContextWithInboundEntryPoint(ctx, v2net.UDPDestination(w.address, w.port))
|
||||||
if err := w.proxy.Process(ctx, v2net.Network_UDP, conn, w.dispatcher); err != nil {
|
if err := w.proxy.Process(ctx, v2net.Network_UDP, conn, w.dispatcher); err != nil {
|
||||||
log.Info("Proxyman|UDPWorker: Connection ends with ", err)
|
log.Info("Proxyman|UDPWorker: Connection ends with ", err)
|
||||||
}
|
}
|
||||||
|
@ -81,7 +81,7 @@ func (h *Handler) Dial(ctx context.Context, dest v2net.Destination) (internet.Co
|
|||||||
handler := h.outboundManager.GetHandler(tag)
|
handler := h.outboundManager.GetHandler(tag)
|
||||||
if handler != nil {
|
if handler != nil {
|
||||||
log.Info("Proxyman|OutboundHandler: Proxying to ", tag)
|
log.Info("Proxyman|OutboundHandler: Proxying to ", tag)
|
||||||
ctx = proxy.ContextWithDestination(ctx, dest)
|
ctx = proxy.ContextWithTarget(ctx, dest)
|
||||||
stream := ray.NewRay(ctx)
|
stream := ray.NewRay(ctx)
|
||||||
go handler.Dispatch(ctx, stream)
|
go handler.Dispatch(ctx, stream)
|
||||||
return NewConnection(stream), nil
|
return NewConnection(stream), nil
|
||||||
|
@ -76,7 +76,11 @@ func NewPlainDomainMatcher(pattern string) *PlainDomainMatcher {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (v *PlainDomainMatcher) Apply(ctx context.Context) bool {
|
func (v *PlainDomainMatcher) Apply(ctx context.Context) bool {
|
||||||
dest := proxy.DestinationFromContext(ctx)
|
dest, ok := proxy.TargetFromContext(ctx)
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
if !dest.Address.Family().IsDomain() {
|
if !dest.Address.Family().IsDomain() {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@ -99,7 +103,10 @@ func NewRegexpDomainMatcher(pattern string) (*RegexpDomainMatcher, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (v *RegexpDomainMatcher) Apply(ctx context.Context) bool {
|
func (v *RegexpDomainMatcher) Apply(ctx context.Context) bool {
|
||||||
dest := proxy.DestinationFromContext(ctx)
|
dest, ok := proxy.TargetFromContext(ctx)
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
if !dest.Address.Family().IsDomain() {
|
if !dest.Address.Family().IsDomain() {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@ -135,13 +142,14 @@ func (v *CIDRMatcher) Apply(ctx context.Context) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var dest v2net.Destination
|
var dest v2net.Destination
|
||||||
|
var ok bool
|
||||||
if v.onSource {
|
if v.onSource {
|
||||||
dest = proxy.SourceFromContext(ctx)
|
dest, ok = proxy.SourceFromContext(ctx)
|
||||||
} else {
|
} else {
|
||||||
dest = proxy.DestinationFromContext(ctx)
|
dest, ok = proxy.TargetFromContext(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
if dest.IsValid() && dest.Address.Family().IsIPv6() {
|
if ok && dest.Address.Family().IsIPv6() {
|
||||||
ips = append(ips, dest.Address.IP())
|
ips = append(ips, dest.Address.IP())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -177,13 +185,14 @@ func (v *IPv4Matcher) Apply(ctx context.Context) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var dest v2net.Destination
|
var dest v2net.Destination
|
||||||
|
var ok bool
|
||||||
if v.onSource {
|
if v.onSource {
|
||||||
dest = proxy.SourceFromContext(ctx)
|
dest, ok = proxy.SourceFromContext(ctx)
|
||||||
} else {
|
} else {
|
||||||
dest = proxy.DestinationFromContext(ctx)
|
dest, ok = proxy.TargetFromContext(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
if dest.IsValid() && dest.Address.Family().IsIPv4() {
|
if ok && dest.Address.Family().IsIPv4() {
|
||||||
ips = append(ips, dest.Address.IP())
|
ips = append(ips, dest.Address.IP())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -206,7 +215,10 @@ func NewPortMatcher(portRange v2net.PortRange) *PortMatcher {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (v *PortMatcher) Apply(ctx context.Context) bool {
|
func (v *PortMatcher) Apply(ctx context.Context) bool {
|
||||||
dest := proxy.DestinationFromContext(ctx)
|
dest, ok := proxy.TargetFromContext(ctx)
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return v.port.Contains(dest.Port)
|
return v.port.Contains(dest.Port)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -221,7 +233,10 @@ func NewNetworkMatcher(network *v2net.NetworkList) *NetworkMatcher {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (v *NetworkMatcher) Apply(ctx context.Context) bool {
|
func (v *NetworkMatcher) Apply(ctx context.Context) bool {
|
||||||
dest := proxy.DestinationFromContext(ctx)
|
dest, ok := proxy.TargetFromContext(ctx)
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return v.network.HasNetwork(dest.Network)
|
return v.network.HasNetwork(dest.Network)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -259,8 +274,8 @@ func NewInboundTagMatcher(tags []string) *InboundTagMatcher {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (v *InboundTagMatcher) Apply(ctx context.Context) bool {
|
func (v *InboundTagMatcher) Apply(ctx context.Context) bool {
|
||||||
tag := proxy.InboundTagFromContext(ctx)
|
tag, ok := proxy.InboundTagFromContext(ctx)
|
||||||
if len(tag) == 0 {
|
if !ok {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,7 +70,11 @@ func (v *Router) TakeDetour(ctx context.Context) (string, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dest := proxy.DestinationFromContext(ctx)
|
dest, ok := proxy.TargetFromContext(ctx)
|
||||||
|
if !ok {
|
||||||
|
return "", ErrNoRuleApplicable
|
||||||
|
}
|
||||||
|
|
||||||
if v.domainStrategy == Config_IpIfNonMatch && dest.Address.Family().IsDomain() {
|
if v.domainStrategy == Config_IpIfNonMatch && dest.Address.Family().IsDomain() {
|
||||||
log.Info("Router: Looking up IP for ", dest)
|
log.Info("Router: Looking up IP for ", dest)
|
||||||
ipDests := v.resolveIP(dest)
|
ipDests := v.resolveIP(dest)
|
||||||
|
@ -41,7 +41,7 @@ func TestSimpleRouter(t *testing.T) {
|
|||||||
|
|
||||||
r := FromSpace(space)
|
r := FromSpace(space)
|
||||||
|
|
||||||
ctx = proxy.ContextWithDestination(ctx, net.TCPDestination(net.DomainAddress("v2ray.com"), 80))
|
ctx = proxy.ContextWithTarget(ctx, net.TCPDestination(net.DomainAddress("v2ray.com"), 80))
|
||||||
tag, err := r.TakeDetour(ctx)
|
tag, err := r.TakeDetour(ctx)
|
||||||
assert.Error(err).IsNil()
|
assert.Error(err).IsNil()
|
||||||
assert.String(tag).Equals("test")
|
assert.String(tag).Equals("test")
|
||||||
|
@ -10,9 +10,9 @@ type key int
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
sourceKey key = iota
|
sourceKey key = iota
|
||||||
destinationKey
|
targetKey
|
||||||
originalDestinationKey
|
originalTargetKey
|
||||||
inboundDestinationKey
|
inboundEntryPointKey
|
||||||
inboundTagKey
|
inboundTagKey
|
||||||
resolvedIPsKey
|
resolvedIPsKey
|
||||||
)
|
)
|
||||||
@ -21,60 +21,45 @@ func ContextWithSource(ctx context.Context, src net.Destination) context.Context
|
|||||||
return context.WithValue(ctx, sourceKey, src)
|
return context.WithValue(ctx, sourceKey, src)
|
||||||
}
|
}
|
||||||
|
|
||||||
func SourceFromContext(ctx context.Context) net.Destination {
|
func SourceFromContext(ctx context.Context) (net.Destination, bool) {
|
||||||
v := ctx.Value(sourceKey)
|
v, ok := ctx.Value(sourceKey).(net.Destination)
|
||||||
if v == nil {
|
return v, ok
|
||||||
return net.Destination{}
|
|
||||||
}
|
|
||||||
return v.(net.Destination)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ContextWithOriginalDestination(ctx context.Context, dest net.Destination) context.Context {
|
func ContextWithOriginalTarget(ctx context.Context, dest net.Destination) context.Context {
|
||||||
return context.WithValue(ctx, originalDestinationKey, dest)
|
return context.WithValue(ctx, originalTargetKey, dest)
|
||||||
}
|
}
|
||||||
|
|
||||||
func OriginalDestinationFromContext(ctx context.Context) net.Destination {
|
func OriginalTargetFromContext(ctx context.Context) (net.Destination, bool) {
|
||||||
v := ctx.Value(originalDestinationKey)
|
v, ok := ctx.Value(originalTargetKey).(net.Destination)
|
||||||
if v == nil {
|
return v, ok
|
||||||
return net.Destination{}
|
|
||||||
}
|
|
||||||
return v.(net.Destination)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ContextWithDestination(ctx context.Context, dest net.Destination) context.Context {
|
func ContextWithTarget(ctx context.Context, dest net.Destination) context.Context {
|
||||||
return context.WithValue(ctx, destinationKey, dest)
|
return context.WithValue(ctx, targetKey, dest)
|
||||||
}
|
}
|
||||||
|
|
||||||
func DestinationFromContext(ctx context.Context) net.Destination {
|
func TargetFromContext(ctx context.Context) (net.Destination, bool) {
|
||||||
v := ctx.Value(destinationKey)
|
v, ok := ctx.Value(targetKey).(net.Destination)
|
||||||
if v == nil {
|
return v, ok
|
||||||
return net.Destination{}
|
|
||||||
}
|
|
||||||
return v.(net.Destination)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ContextWithInboundDestination(ctx context.Context, dest net.Destination) context.Context {
|
func ContextWithInboundEntryPoint(ctx context.Context, dest net.Destination) context.Context {
|
||||||
return context.WithValue(ctx, inboundDestinationKey, dest)
|
return context.WithValue(ctx, inboundEntryPointKey, dest)
|
||||||
}
|
}
|
||||||
|
|
||||||
func InboundDestinationFromContext(ctx context.Context) net.Destination {
|
func InboundEntryPointFromContext(ctx context.Context) (net.Destination, bool) {
|
||||||
v := ctx.Value(inboundDestinationKey)
|
v, ok := ctx.Value(inboundEntryPointKey).(net.Destination)
|
||||||
if v == nil {
|
return v, ok
|
||||||
return net.Destination{}
|
|
||||||
}
|
|
||||||
return v.(net.Destination)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ContextWithInboundTag(ctx context.Context, tag string) context.Context {
|
func ContextWithInboundTag(ctx context.Context, tag string) context.Context {
|
||||||
return context.WithValue(ctx, inboundTagKey, tag)
|
return context.WithValue(ctx, inboundTagKey, tag)
|
||||||
}
|
}
|
||||||
|
|
||||||
func InboundTagFromContext(ctx context.Context) string {
|
func InboundTagFromContext(ctx context.Context) (string, bool) {
|
||||||
v := ctx.Value(inboundTagKey)
|
v, ok := ctx.Value(inboundTagKey).(string)
|
||||||
if v == nil {
|
return v, ok
|
||||||
return ""
|
|
||||||
}
|
|
||||||
return v.(string)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ContextWithResolveIPs(ctx context.Context, ips []net.Address) context.Context {
|
func ContextWithResolveIPs(ctx context.Context, ips []net.Address) context.Context {
|
||||||
|
@ -52,7 +52,7 @@ func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn in
|
|||||||
Port: d.port,
|
Port: d.port,
|
||||||
}
|
}
|
||||||
if d.config.FollowRedirect {
|
if d.config.FollowRedirect {
|
||||||
if origDest := proxy.OriginalDestinationFromContext(ctx); origDest.IsValid() {
|
if origDest, ok := proxy.OriginalTargetFromContext(ctx); ok {
|
||||||
dest = origDest
|
dest = origDest
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -74,7 +74,7 @@ func (v *Handler) ResolveIP(destination net.Destination) net.Destination {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (v *Handler) Process(ctx context.Context, outboundRay ray.OutboundRay, dialer proxy.Dialer) error {
|
func (v *Handler) Process(ctx context.Context, outboundRay ray.OutboundRay, dialer proxy.Dialer) error {
|
||||||
destination := proxy.DestinationFromContext(ctx)
|
destination, _ := proxy.TargetFromContext(ctx)
|
||||||
if v.destOverride != nil {
|
if v.destOverride != nil {
|
||||||
server := v.destOverride.Server
|
server := v.destOverride.Server
|
||||||
destination = net.Destination{
|
destination = net.Destination{
|
||||||
|
@ -2,10 +2,9 @@ package shadowsocks
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"time"
|
|
||||||
|
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"time"
|
||||||
|
|
||||||
"v2ray.com/core/app/log"
|
"v2ray.com/core/app/log"
|
||||||
"v2ray.com/core/common"
|
"v2ray.com/core/common"
|
||||||
@ -40,7 +39,10 @@ func NewClient(ctx context.Context, config *ClientConfig) (*Client, error) {
|
|||||||
|
|
||||||
// Process implements OutboundHandler.Process().
|
// Process implements OutboundHandler.Process().
|
||||||
func (v *Client) Process(ctx context.Context, outboundRay ray.OutboundRay, dialer proxy.Dialer) error {
|
func (v *Client) Process(ctx context.Context, outboundRay ray.OutboundRay, dialer proxy.Dialer) error {
|
||||||
destination := proxy.DestinationFromContext(ctx)
|
destination, ok := proxy.TargetFromContext(ctx)
|
||||||
|
if !ok {
|
||||||
|
return errors.New("Shadowsocks|Client: Target not specified.")
|
||||||
|
}
|
||||||
network := destination.Network
|
network := destination.Network
|
||||||
|
|
||||||
var server *protocol.ServerSpec
|
var server *protocol.ServerSpec
|
||||||
|
@ -75,7 +75,6 @@ func (s *Server) Process(ctx context.Context, network net.Network, conn internet
|
|||||||
|
|
||||||
func (v *Server) handlerUDPPayload(ctx context.Context, conn internet.Connection, dispatcher dispatcher.Interface) error {
|
func (v *Server) handlerUDPPayload(ctx context.Context, conn internet.Connection, dispatcher dispatcher.Interface) error {
|
||||||
udpServer := udp.NewDispatcher(dispatcher)
|
udpServer := udp.NewDispatcher(dispatcher)
|
||||||
source := proxy.SourceFromContext(ctx)
|
|
||||||
|
|
||||||
reader := buf.NewReader(conn)
|
reader := buf.NewReader(conn)
|
||||||
for {
|
for {
|
||||||
@ -86,8 +85,10 @@ func (v *Server) handlerUDPPayload(ctx context.Context, conn internet.Connection
|
|||||||
|
|
||||||
request, data, err := DecodeUDPPacket(v.user, payload)
|
request, data, err := DecodeUDPPacket(v.user, payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Info("Shadowsocks|Server: Skipping invalid UDP packet from: ", source, ": ", err)
|
if source, ok := proxy.SourceFromContext(ctx); ok {
|
||||||
log.Access(source, "", log.AccessRejected, err)
|
log.Info("Shadowsocks|Server: Skipping invalid UDP packet from: ", source, ": ", err)
|
||||||
|
log.Access(source, "", log.AccessRejected, err)
|
||||||
|
}
|
||||||
payload.Release()
|
payload.Release()
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -105,7 +106,9 @@ func (v *Server) handlerUDPPayload(ctx context.Context, conn internet.Connection
|
|||||||
}
|
}
|
||||||
|
|
||||||
dest := request.Destination()
|
dest := request.Destination()
|
||||||
log.Access(source, dest, log.AccessAccepted, "")
|
if source, ok := proxy.SourceFromContext(ctx); ok {
|
||||||
|
log.Access(source, dest, log.AccessAccepted, "")
|
||||||
|
}
|
||||||
log.Info("Shadowsocks|Server: Tunnelling request to ", dest)
|
log.Info("Shadowsocks|Server: Tunnelling request to ", dest)
|
||||||
|
|
||||||
ctx = protocol.ContextWithUser(ctx, request.User)
|
ctx = protocol.ContextWithUser(ctx, request.User)
|
||||||
|
@ -2,7 +2,7 @@ package socks
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"runtime"
|
"runtime"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -35,7 +35,10 @@ func NewClient(ctx context.Context, config *ClientConfig) (*Client, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Process(ctx context.Context, ray ray.OutboundRay, dialer proxy.Dialer) error {
|
func (c *Client) Process(ctx context.Context, ray ray.OutboundRay, dialer proxy.Dialer) error {
|
||||||
destination := proxy.DestinationFromContext(ctx)
|
destination, ok := proxy.TargetFromContext(ctx)
|
||||||
|
if !ok {
|
||||||
|
return errors.New("Socks|Client: Target not specified.")
|
||||||
|
}
|
||||||
|
|
||||||
var server *protocol.ServerSpec
|
var server *protocol.ServerSpec
|
||||||
var conn internet.Connection
|
var conn internet.Connection
|
||||||
|
@ -65,16 +65,20 @@ func (s *Server) processTCP(ctx context.Context, conn internet.Connection, dispa
|
|||||||
conn.SetReadDeadline(time.Now().Add(time.Second * 8))
|
conn.SetReadDeadline(time.Now().Add(time.Second * 8))
|
||||||
reader := bufio.NewReader(conn)
|
reader := bufio.NewReader(conn)
|
||||||
|
|
||||||
inboundDest := proxy.InboundDestinationFromContext(ctx)
|
inboundDest, ok := proxy.InboundEntryPointFromContext(ctx)
|
||||||
|
if !ok {
|
||||||
|
return errors.New("Socks|Server: inbound entry point not specified.")
|
||||||
|
}
|
||||||
session := &ServerSession{
|
session := &ServerSession{
|
||||||
config: s.config,
|
config: s.config,
|
||||||
port: inboundDest.Port,
|
port: inboundDest.Port,
|
||||||
}
|
}
|
||||||
|
|
||||||
source := proxy.SourceFromContext(ctx)
|
|
||||||
request, err := session.Handshake(reader, conn)
|
request, err := session.Handshake(reader, conn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Access(source, "", log.AccessRejected, err)
|
if source, ok := proxy.SourceFromContext(ctx); ok {
|
||||||
|
log.Access(source, "", log.AccessRejected, err)
|
||||||
|
}
|
||||||
log.Info("Socks|Server: Failed to read request: ", err)
|
log.Info("Socks|Server: Failed to read request: ", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -83,7 +87,9 @@ func (s *Server) processTCP(ctx context.Context, conn internet.Connection, dispa
|
|||||||
if request.Command == protocol.RequestCommandTCP {
|
if request.Command == protocol.RequestCommandTCP {
|
||||||
dest := request.Destination()
|
dest := request.Destination()
|
||||||
log.Info("Socks|Server: TCP Connect request to ", dest)
|
log.Info("Socks|Server: TCP Connect request to ", dest)
|
||||||
log.Access(source, dest, log.AccessAccepted, "")
|
if source, ok := proxy.SourceFromContext(ctx); ok {
|
||||||
|
log.Access(source, dest, log.AccessAccepted, "")
|
||||||
|
}
|
||||||
|
|
||||||
return s.transport(ctx, reader, conn, dest, dispatcher)
|
return s.transport(ctx, reader, conn, dest, dispatcher)
|
||||||
}
|
}
|
||||||
@ -155,8 +161,9 @@ func (v *Server) transport(ctx context.Context, reader io.Reader, writer io.Writ
|
|||||||
func (v *Server) handleUDPPayload(ctx context.Context, conn internet.Connection, dispatcher dispatcher.Interface) error {
|
func (v *Server) handleUDPPayload(ctx context.Context, conn internet.Connection, dispatcher dispatcher.Interface) error {
|
||||||
udpServer := udp.NewDispatcher(dispatcher)
|
udpServer := udp.NewDispatcher(dispatcher)
|
||||||
|
|
||||||
source := proxy.SourceFromContext(ctx)
|
if source, ok := proxy.SourceFromContext(ctx); ok {
|
||||||
log.Info("Socks|Server: Client UDP connection from ", source)
|
log.Info("Socks|Server: Client UDP connection from ", source)
|
||||||
|
}
|
||||||
|
|
||||||
reader := buf.NewReader(conn)
|
reader := buf.NewReader(conn)
|
||||||
for {
|
for {
|
||||||
@ -176,7 +183,9 @@ func (v *Server) handleUDPPayload(ctx context.Context, conn internet.Connection,
|
|||||||
}
|
}
|
||||||
|
|
||||||
log.Info("Socks: Send packet to ", request.Destination(), " with ", len(data), " bytes")
|
log.Info("Socks: Send packet to ", request.Destination(), " with ", len(data), " bytes")
|
||||||
log.Access(source, request.Destination, log.AccessAccepted, "")
|
if source, ok := proxy.SourceFromContext(ctx); ok {
|
||||||
|
log.Access(source, request.Destination, log.AccessAccepted, "")
|
||||||
|
}
|
||||||
|
|
||||||
dataBuf := buf.NewSmall()
|
dataBuf := buf.NewSmall()
|
||||||
dataBuf.Append(data)
|
dataBuf.Append(data)
|
||||||
|
@ -67,7 +67,10 @@ func (v *VMessOutboundHandler) Process(ctx context.Context, outboundRay ray.Outb
|
|||||||
}
|
}
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
target := proxy.DestinationFromContext(ctx)
|
target, ok := proxy.TargetFromContext(ctx)
|
||||||
|
if !ok {
|
||||||
|
return errors.New("VMess|Outbound: Target not specified.")
|
||||||
|
}
|
||||||
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
|
command := protocol.RequestCommandTCP
|
||||||
|
@ -30,12 +30,12 @@ func TestChinaIPJson(t *testing.T) {
|
|||||||
assert.String(rule.Tag).Equals("x")
|
assert.String(rule.Tag).Equals("x")
|
||||||
cond, err := rule.BuildCondition()
|
cond, err := rule.BuildCondition()
|
||||||
assert.Error(err).IsNil()
|
assert.Error(err).IsNil()
|
||||||
assert.Bool(cond.Apply(proxy.ContextWithDestination(context.Background(), v2net.TCPDestination(v2net.ParseAddress("121.14.1.189"), 80)))).IsTrue() // sina.com.cn
|
assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.ParseAddress("121.14.1.189"), 80)))).IsTrue() // sina.com.cn
|
||||||
assert.Bool(cond.Apply(proxy.ContextWithDestination(context.Background(), v2net.TCPDestination(v2net.ParseAddress("101.226.103.106"), 80)))).IsTrue() // qq.com
|
assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.ParseAddress("101.226.103.106"), 80)))).IsTrue() // qq.com
|
||||||
assert.Bool(cond.Apply(proxy.ContextWithDestination(context.Background(), v2net.TCPDestination(v2net.ParseAddress("115.239.210.36"), 80)))).IsTrue() // image.baidu.com
|
assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.ParseAddress("115.239.210.36"), 80)))).IsTrue() // image.baidu.com
|
||||||
assert.Bool(cond.Apply(proxy.ContextWithDestination(context.Background(), v2net.TCPDestination(v2net.ParseAddress("120.135.126.1"), 80)))).IsTrue()
|
assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.ParseAddress("120.135.126.1"), 80)))).IsTrue()
|
||||||
|
|
||||||
assert.Bool(cond.Apply(proxy.ContextWithDestination(context.Background(), v2net.TCPDestination(v2net.ParseAddress("8.8.8.8"), 80)))).IsFalse()
|
assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.ParseAddress("8.8.8.8"), 80)))).IsFalse()
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestChinaSitesJson(t *testing.T) {
|
func TestChinaSitesJson(t *testing.T) {
|
||||||
@ -49,12 +49,12 @@ func TestChinaSitesJson(t *testing.T) {
|
|||||||
assert.String(rule.Tag).Equals("y")
|
assert.String(rule.Tag).Equals("y")
|
||||||
cond, err := rule.BuildCondition()
|
cond, err := rule.BuildCondition()
|
||||||
assert.Error(err).IsNil()
|
assert.Error(err).IsNil()
|
||||||
assert.Bool(cond.Apply(proxy.ContextWithDestination(context.Background(), v2net.TCPDestination(v2net.ParseAddress("v.qq.com"), 80)))).IsTrue()
|
assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.ParseAddress("v.qq.com"), 80)))).IsTrue()
|
||||||
assert.Bool(cond.Apply(proxy.ContextWithDestination(context.Background(), v2net.TCPDestination(v2net.ParseAddress("www.163.com"), 80)))).IsTrue()
|
assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.ParseAddress("www.163.com"), 80)))).IsTrue()
|
||||||
assert.Bool(cond.Apply(proxy.ContextWithDestination(context.Background(), v2net.TCPDestination(v2net.ParseAddress("ngacn.cc"), 80)))).IsTrue()
|
assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.ParseAddress("ngacn.cc"), 80)))).IsTrue()
|
||||||
assert.Bool(cond.Apply(proxy.ContextWithDestination(context.Background(), v2net.TCPDestination(v2net.ParseAddress("12306.cn"), 80)))).IsTrue()
|
assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.ParseAddress("12306.cn"), 80)))).IsTrue()
|
||||||
|
|
||||||
assert.Bool(cond.Apply(proxy.ContextWithDestination(context.Background(), v2net.TCPDestination(v2net.ParseAddress("v2ray.com"), 80)))).IsFalse()
|
assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.ParseAddress("v2ray.com"), 80)))).IsFalse()
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDomainRule(t *testing.T) {
|
func TestDomainRule(t *testing.T) {
|
||||||
@ -74,11 +74,11 @@ func TestDomainRule(t *testing.T) {
|
|||||||
assert.Pointer(rule).IsNotNil()
|
assert.Pointer(rule).IsNotNil()
|
||||||
cond, err := rule.BuildCondition()
|
cond, err := rule.BuildCondition()
|
||||||
assert.Error(err).IsNil()
|
assert.Error(err).IsNil()
|
||||||
assert.Bool(cond.Apply(proxy.ContextWithDestination(context.Background(), v2net.TCPDestination(v2net.ParseAddress("www.ooxx.com"), 80)))).IsTrue()
|
assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.ParseAddress("www.ooxx.com"), 80)))).IsTrue()
|
||||||
assert.Bool(cond.Apply(proxy.ContextWithDestination(context.Background(), v2net.TCPDestination(v2net.ParseAddress("www.aabb.com"), 80)))).IsFalse()
|
assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.ParseAddress("www.aabb.com"), 80)))).IsFalse()
|
||||||
assert.Bool(cond.Apply(proxy.ContextWithDestination(context.Background(), v2net.TCPDestination(v2net.IPAddress([]byte{127, 0, 0, 1}), 80)))).IsFalse()
|
assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.IPAddress([]byte{127, 0, 0, 1}), 80)))).IsFalse()
|
||||||
assert.Bool(cond.Apply(proxy.ContextWithDestination(context.Background(), v2net.TCPDestination(v2net.ParseAddress("www.12306.cn"), 80)))).IsTrue()
|
assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.ParseAddress("www.12306.cn"), 80)))).IsTrue()
|
||||||
assert.Bool(cond.Apply(proxy.ContextWithDestination(context.Background(), v2net.TCPDestination(v2net.ParseAddress("www.acn.com"), 80)))).IsFalse()
|
assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.ParseAddress("www.acn.com"), 80)))).IsFalse()
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIPRule(t *testing.T) {
|
func TestIPRule(t *testing.T) {
|
||||||
@ -97,10 +97,10 @@ func TestIPRule(t *testing.T) {
|
|||||||
assert.Pointer(rule).IsNotNil()
|
assert.Pointer(rule).IsNotNil()
|
||||||
cond, err := rule.BuildCondition()
|
cond, err := rule.BuildCondition()
|
||||||
assert.Error(err).IsNil()
|
assert.Error(err).IsNil()
|
||||||
assert.Bool(cond.Apply(proxy.ContextWithDestination(context.Background(), v2net.TCPDestination(v2net.DomainAddress("www.ooxx.com"), 80)))).IsFalse()
|
assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.DomainAddress("www.ooxx.com"), 80)))).IsFalse()
|
||||||
assert.Bool(cond.Apply(proxy.ContextWithDestination(context.Background(), v2net.TCPDestination(v2net.IPAddress([]byte{10, 0, 0, 1}), 80)))).IsTrue()
|
assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.IPAddress([]byte{10, 0, 0, 1}), 80)))).IsTrue()
|
||||||
assert.Bool(cond.Apply(proxy.ContextWithDestination(context.Background(), v2net.TCPDestination(v2net.IPAddress([]byte{127, 0, 0, 1}), 80)))).IsFalse()
|
assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.IPAddress([]byte{127, 0, 0, 1}), 80)))).IsFalse()
|
||||||
assert.Bool(cond.Apply(proxy.ContextWithDestination(context.Background(), v2net.TCPDestination(v2net.IPAddress([]byte{192, 0, 0, 1}), 80)))).IsTrue()
|
assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.IPAddress([]byte{192, 0, 0, 1}), 80)))).IsTrue()
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSourceIPRule(t *testing.T) {
|
func TestSourceIPRule(t *testing.T) {
|
||||||
|
Loading…
Reference in New Issue
Block a user