Procházet zdrojové kódy

help function for create instance from config bytes

Darien Raymond před 7 roky
rodič
revize
ca4d42f2bc
3 změnil soubory, kde provedl 28 přidání a 6 odebrání
  1. 19 0
      functions.go
  2. 1 1
      proxy/freedom/freedom.go
  3. 8 5
      v2ray_test.go

+ 19 - 0
functions.go

@@ -4,6 +4,7 @@ import (
 	"context"
 
 	"v2ray.com/core/common"
+	"v2ray.com/core/common/buf"
 )
 
 // CreateObject creates a new object based on the given V2Ray instance and config. The V2Ray instance may be nil.
@@ -15,6 +16,24 @@ func CreateObject(v *Instance, config interface{}) (interface{}, error) {
 	return common.CreateObject(ctx, config)
 }
 
+// StartInstance starts a new V2Ray instance with given serialized config, and return a handle for shutting down the instance.
+func StartInstance(configFormat string, configBytes []byte) (common.Closable, error) {
+	var mb buf.MultiBuffer
+	common.Must2(mb.Write(configBytes))
+	config, err := LoadConfig(configFormat, "", &mb)
+	if err != nil {
+		return nil, err
+	}
+	instance, err := New(config)
+	if err != nil {
+		return nil, err
+	}
+	if err := instance.Start(); err != nil {
+		return nil, err
+	}
+	return instance, nil
+}
+
 func PrintDeprecatedFeatureWarning(feature string) {
 	newError("You are using a deprecated feature: " + feature + ". Please update your config file with latest configuration format, or update your client software.").WriteToLog()
 }

+ 1 - 1
proxy/freedom/freedom.go

@@ -6,7 +6,6 @@ import (
 	"context"
 	"time"
 
-	"github.com/miekg/dns"
 	"v2ray.com/core"
 	"v2ray.com/core/common"
 	"v2ray.com/core/common/buf"
@@ -17,6 +16,7 @@ import (
 	"v2ray.com/core/common/signal"
 	"v2ray.com/core/common/task"
 	"v2ray.com/core/common/vio"
+	"v2ray.com/core/features/dns"
 	"v2ray.com/core/features/policy"
 	"v2ray.com/core/proxy"
 	"v2ray.com/core/transport/internet"

+ 8 - 5
v2ray_test.go

@@ -3,8 +3,11 @@ package core_test
 import (
 	"testing"
 
+	proto "github.com/golang/protobuf/proto"
 	. "v2ray.com/core"
+	"v2ray.com/core/app/dispatcher"
 	"v2ray.com/core/app/proxyman"
+	"v2ray.com/core/common"
 	"v2ray.com/core/common/dice"
 	"v2ray.com/core/common/net"
 	"v2ray.com/core/common/protocol"
@@ -14,16 +17,14 @@ import (
 	"v2ray.com/core/proxy/dokodemo"
 	"v2ray.com/core/proxy/vmess"
 	"v2ray.com/core/proxy/vmess/outbound"
-	. "v2ray.com/ext/assert"
 )
 
 func TestV2RayClose(t *testing.T) {
-	assert := With(t)
-
 	port := net.Port(dice.RollUint16())
 	userId := uuid.New()
 	config := &Config{
 		App: []*serial.TypedMessage{
+			serial.ToTypedMessage(&dispatcher.Config{}),
 			serial.ToTypedMessage(&proxyman.InboundConfig{}),
 			serial.ToTypedMessage(&proxyman.OutboundConfig{}),
 		},
@@ -63,8 +64,10 @@ func TestV2RayClose(t *testing.T) {
 		},
 	}
 
-	server, err := New(config)
-	assert(err, IsNil)
+	cfgBytes, err := proto.Marshal(config)
+	common.Must(err)
 
+	server, err := StartInstance("protobuf", cfgBytes)
+	common.Must(err)
 	server.Close()
 }