SagerNet / sing-box

The universal proxy platform
https://sing-box.sagernet.org/
Other
20.15k stars 2.4k forks source link

[Feature Request] Configurable "User-Agent" for SIP003 v2ray transport #2227

Open zedifen opened 3 weeks ago

zedifen commented 3 weeks ago

Custom headers is allowed in V2ray WebSocket transport. However for Shadowsocks outbounds, currently it's not possible to configure them through plugin_opts.

Setting User-Agent (or other headers) might be useful for server to distinguish clients (e.g. identifying sing-box client against web browsers / scrapers) so the sever can then choose to provide service or not.

It's not hard to read an additional argument from plugin_opts and apply it to option.V2RayTransportOptions. I made a simple patch as below and it's working.

diff --git a/transport/sip003/v2ray.go b/transport/sip003/v2ray.go
index 078adea278..afda3f15db 100644
--- a/transport/sip003/v2ray.go
+++ b/transport/sip003/v2ray.go
@@ -49,6 +49,7 @@ func newV2RayPlugin(ctx context.Context, pluginOpts Args, router adapter.Router,

    host := "cloudfront.com"
    path := "/"
+   userAgent := "Go-http-client/1.1 sing-box/1.10 SingBox/1.10"

    if hostOpt, loaded := pluginOpts.Get("host"); loaded {
        host = hostOpt
@@ -57,6 +58,9 @@ func newV2RayPlugin(ctx context.Context, pluginOpts Args, router adapter.Router,
    if pathOpt, loaded := pluginOpts.Get("path"); loaded {
        path = pathOpt
    }
+   if userAgentOpt, loaded := pluginOpts.Get("userAgent"); loaded {
+       userAgent = userAgentOpt
+   }

    var tlsClient tls.Config
    var err error
@@ -76,6 +80,7 @@ func newV2RayPlugin(ctx context.Context, pluginOpts Args, router adapter.Router,
            WebsocketOptions: option.V2RayWebsocketOptions{
                Headers: map[string]option.Listable[string]{
                    "Host": []string{host},
+                   "User-Agent": []string{userAgent},
                },
                Path: path,
            },
junx964 commented 3 weeks ago

Support. Looking forward to the update merge. Looking forward to shadowsocks support I am currently using simple-obfs to reverse the generation, and I am tired