Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

panic: V is not in context. #698

Closed
rurirei opened this issue Feb 21, 2021 · 10 comments
Closed

panic: V is not in context. #698

rurirei opened this issue Feb 21, 2021 · 10 comments

Comments

@rurirei
Copy link
Contributor

rurirei commented Feb 21, 2021

i uses v2ray-core (commit top to #406 merged) as a Android library (code as below) and met errors as below, but do not met this if uses tree before FakeDns added. especially fakednssniffer.go appears at logs, maybe that is introduced by FakeDns commits? sorry for my not read codes of FakeDns.

[v2ray] [Warning] v2ray.com/core: V2Ray 4.34.0 started

[tun2socks] [Handle] tcp 172.19.0.1:41201 147.202.171.5:443 147.202.171.5:443

panic: V is not in context.

goroutine 31 [running]:

v2ray.com/core.MustFromContext(0x7dea651aa0, 0x4002cb94d0, 0x10)
	/home/runner/go/src/v2ray.com/core/context.go:26 +0x8c

v2ray.com/core.RequireFeatures(0x7dea651aa0, 0x4002cb94d0, 0x7dea471ce0, 0x4002638d40, 0x30, 0x7dea47c760)
	/home/runner/go/src/v2ray.com/core/v2ray.go:154 +0x28

v2ray.com/core/app/dispatcher.newFakeDNSSniffer(0x7dea651aa0, 0x4002cb94d0, 0x4002d8c070, 0x7de9f685f0, 0x7dea462280, 0x400000f460)
	/home/runner/go/src/v2ray.com/core/app/dispatcher/fakednssniffer.go:18 +0x88

v2ray.com/core/app/dispatcher.NewSniffer(0x7dea651aa0, 0x4002cb94d0, 0x400000f460)
	/home/runner/go/src/v2ray.com/core/app/dispatcher/sniffer.go:41 +0xa8

v2ray.com/core/app/dispatcher.sniffer(0x7dea651aa0, 0x4002cb94d0, 0x4002cb9590, 0x0, 0x0, 0x0, 0x0, 0x0)
	/home/runner/go/src/v2ray.com/core/app/dispatcher/default.go:252 +0xb4

v2ray.com/core/app/dispatcher.(*DefaultDispatcher).Dispatch.func1(0x400000f3c0, 0x7dea651aa0, 0x4002cb94d0, 0x400000f320, 0x2, 0x2, 0x7e00960001, 0x4000ead3c0, 0x400000f340, 0x4002cb94a0, ...)
	/home/runner/go/src/v2ray.com/core/app/dispatcher/default.go:232 +0x90

created by v2ray.com/core/app/dispatcher.(*DefaultDispatcher).Dispatch
	/home/runner/go/src/v2ray.com/core/app/dispatcher/default.go:227 +0x4f8
import (
    "context"

    vcore "v2ray.com/core"
    vnet "v2ray.com/core/common/net"
    vproxyman "v2ray.com/core/app/proxyman"
    vprotocol "v2ray.com/core/common/protocol"
    vsession "v2ray.com/core/common/session"
)

    vinbound := &vsession.Inbound{Tag: "vinbound"}

    vsniffing := &vproxyman.SniffingConfig{
        Enabled:             true,
        DestinationOverride: []string{"http", "tls"},
        MetadataOnly:        false,
    }

    vcontent := &vsession.Content{
        SniffingRequest: vsession.SniffingRequest{
            Enabled:                        vsniffing.Enabled,
            OverrideDestinationForProtocol: vsniffing.DestinationOverride,
            MetadataOnly:                   vsniffing.MetadataOnly,
        },
    }

    ctx := vsession.ContextWithID(context.Background(), vsession.NewID())
    ctx = vsession.ContextWithInbound(ctx, vinbound)
    ctx = vsession.ContextWithContent(ctx, vcontent)

    c, err := vcore.Dial(ctx, h.v, dest)

v2ray-core/context.go

Lines 14 to 29 in efb9632

// FromContext returns an Instance from the given context, or nil if the context doesn't contain one.
func FromContext(ctx context.Context) *Instance {
if s, ok := ctx.Value(v2rayKey).(*Instance); ok {
return s
}
return nil
}
// MustFromContext returns an Instance from the given context, or panics if not present.
func MustFromContext(ctx context.Context) *Instance {
v := FromContext(ctx)
if v == nil {
panic("V is not in context.")
}
return v
}

@yuhan6665
Copy link
Contributor

I wasn't see this issue. What's your json config?

@rurirei
Copy link
Contributor Author

rurirei commented Feb 21, 2021

@yuhan6665

{
  "log": {
    "loglevel": "debug"
  },
  "inbounds": [  // unused inbound
    {
      "listen": "127.0.0.1",
      "port": 1080,
      "protocol": "socks",
      "settings": {
        "auth": "noauth",
        "udp": true
      },
      "sniffing": {
        "destOverride": [
          "http",
          "tls"
        ],
        "enabled": true
      }
    }
  ],
  "outbounds": [
    {
      "protocol": "shadowsocks",
      "settings": {
        "servers": [
          ...
        ]
      },
      "streamSettings": {
        "network": "tcp",
        "security": "none"
      },
      "tag": "proxy"
    }
  ],
  "routing": {
    "domainStrategy": "AsIs",
    "rules": []
  },
  "stats": {}
}

@yuhan6665
Copy link
Contributor

@rurirei I tried config similar to yours but can not reproduce the issue. It is quite weird. Do you also have this issue if you turn fake dns on?

"fakedns": {
    "ipPool": "240.0.0.0/8",
    "poolSize": 65535
}
"dns": {
        "servers": [
            "fakedns"
            "8.8.8.8",
            "https://1.1.1.1/dns-query"
        ]
},
...
"sniffing": {
        "enabled": true,
        "destOverride": [
            "http",
            "tls",
            "fakedns"
        ]
},

@rurirei
Copy link
Contributor Author

rurirei commented Feb 22, 2021

@yuhan6665 errors met as well.

@rurirei
Copy link
Contributor Author

rurirei commented Feb 22, 2021

especially do not met this if disabled sniffing.

vsniffing := &vproxyman.SniffingConfig{
        Enabled:             false,
        DestinationOverride: []string{"http", "tls", "fakedns"},
        MetadataOnly:        false,
}

@yuhan6665
Copy link
Contributor

@rurirei based on your error log, the context has some issue when init sniffer. That's why if you turn off sniffer it will be ok.
I think it is issue in your way of init the context.

    ctx := vsession.ContextWithID(context.Background(), vsession.NewID())
    ctx = vsession.ContextWithInbound(ctx, vinbound)
    ctx = vsession.ContextWithContent(ctx, vcontent)

https://github.com/2dust/AndroidLibV2rayLite

@rurirei
Copy link
Contributor Author

rurirei commented Feb 22, 2021

@yuhan6665 that's it. i'll try v.RequireFeatures() to do not uses ctx.

func newFakeDNSSniffer(ctx context.Context) (protocolSnifferWithMetadata, error) {
var fakeDNSEngine dns.FakeDNSEngine
err := core.RequireFeatures(ctx, func(fdns dns.FakeDNSEngine) {
fakeDNSEngine = fdns
})

@rurirei
Copy link
Contributor Author

rurirei commented Feb 22, 2021

seems gently fixed by.

ctx = context.WithValue(v.ctx, v2rayKey, v)

@rurirei
Copy link
Contributor Author

rurirei commented Feb 22, 2021

seems ok.

[v2ray] [Debug] v2ray.com/core/app/dns: domain github.com will use DNS in order: [FakeDNS]
[v2ray] [Info] [3492521930] v2ray.com/core/app/dispatcher: fake dns got domain: github.com for ip: 198.18.0.2
[v2ray] [Info] [3492521930] v2ray.com/core/app/dispatcher: sniffed domain: github.com
[v2ray] [Info] [3492521930] v2ray.com/core/app/dispatcher: taking detour [proxy] for [tcp:github.com:443]

@simo8102
Copy link

Add to config

"sniffing": {
"enabled": true,
"destOverride": [
"http",
"tls",
"fakedns"
]
},

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants