From aa6528722c4d7d3fd294b71baddc86f112366f03 Mon Sep 17 00:00:00 2001 From: Juraj Hilje Date: Fri, 29 Sep 2023 11:56:12 +0200 Subject: [PATCH] feat: update V2RayControl/ctrl.go --- V2RayControl/ctrl.go | 63 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 60 insertions(+), 3 deletions(-) diff --git a/V2RayControl/ctrl.go b/V2RayControl/ctrl.go index 8fd902485..e71ce1461 100644 --- a/V2RayControl/ctrl.go +++ b/V2RayControl/ctrl.go @@ -2,16 +2,55 @@ package v2rayControl import ( "fmt" + "sync" core "github.com/v2fly/v2ray-core/v5" - _ "github.com/v2fly/v2ray-core/v5/main/distro/all" // required for loading configuration loaders (we use only "JSON") + + // Mandatory features + _ "github.com/v2fly/v2ray-core/v5/app/dispatcher" + _ "github.com/v2fly/v2ray-core/v5/app/proxyman/inbound" + _ "github.com/v2fly/v2ray-core/v5/app/proxyman/outbound" + + // Inbound and outbound proxies + _ "github.com/v2fly/v2ray-core/v5/proxy/blackhole" + _ "github.com/v2fly/v2ray-core/v5/proxy/dns" + _ "github.com/v2fly/v2ray-core/v5/proxy/dokodemo" + _ "github.com/v2fly/v2ray-core/v5/proxy/freedom" + _ "github.com/v2fly/v2ray-core/v5/proxy/http" + _ "github.com/v2fly/v2ray-core/v5/proxy/shadowsocks" + _ "github.com/v2fly/v2ray-core/v5/proxy/socks" + _ "github.com/v2fly/v2ray-core/v5/proxy/trojan" + _ "github.com/v2fly/v2ray-core/v5/proxy/vless/inbound" + _ "github.com/v2fly/v2ray-core/v5/proxy/vless/outbound" + _ "github.com/v2fly/v2ray-core/v5/proxy/vmess/inbound" + _ "github.com/v2fly/v2ray-core/v5/proxy/vmess/outbound" + + // Transport headers + _ "github.com/v2fly/v2ray-core/v5/transport/internet/headers/http" + _ "github.com/v2fly/v2ray-core/v5/transport/internet/headers/noop" + _ "github.com/v2fly/v2ray-core/v5/transport/internet/headers/srtp" + _ "github.com/v2fly/v2ray-core/v5/transport/internet/headers/tls" + _ "github.com/v2fly/v2ray-core/v5/transport/internet/headers/utp" + _ "github.com/v2fly/v2ray-core/v5/transport/internet/headers/wechat" + _ "github.com/v2fly/v2ray-core/v5/transport/internet/headers/wireguard" + + // JSON, TOML, YAML config support + _ "github.com/v2fly/v2ray-core/v5/main/formats" ) type Instance struct { server *core.Instance } +var ( + locker sync.Mutex + v2rayInstance *Instance +) + func Start(jsonConfig string) (*Instance, error) { + locker.Lock() + defer locker.Unlock() + config, err := core.LoadConfig("json", []byte(jsonConfig)) if err != nil { return nil, err @@ -27,13 +66,31 @@ func Start(jsonConfig string) (*Instance, error) { return nil, err } - return &Instance{server: server}, nil + v2rayInstance = &Instance{server: server} + + return v2rayInstance, nil } func Stop(instance *Instance) error { + locker.Lock() + defer locker.Unlock() + if instance.server == nil { return fmt.Errorf("server instance is nil") } - return instance.server.Close() + if v2rayInstance != nil { + v2rayInstance.server.Close() + v2rayInstance = nil + } + + if instance != nil { + err := instance.server.Close() + if err != nil { + return err + } + instance = nil + } + + return nil }