Browse Source

V2Ray API Instance

Shelikhoo 4 years ago
parent
commit
58b16b8a1e
2 changed files with 109 additions and 0 deletions
  1. 13 0
      main/v2binding/v2api/api.go
  2. 96 0
      main/v2binding/v2binding.go

+ 13 - 0
main/v2binding/v2api/api.go

@@ -0,0 +1,13 @@
+package main
+
+import (
+	"github.com/v2fly/v2ray-core/v4/main/v2binding"
+	"time"
+)
+
+func main() {
+	v2binding.StartAPIInstance(".")
+	for {
+		time.Sleep(time.Minute)
+	}
+}

+ 96 - 0
main/v2binding/v2binding.go

@@ -0,0 +1,96 @@
+package v2binding
+
+import (
+	core "github.com/v2fly/v2ray-core/v4"
+	"github.com/v2fly/v2ray-core/v4/app/commander"
+	"github.com/v2fly/v2ray-core/v4/app/dispatcher"
+	"github.com/v2fly/v2ray-core/v4/app/instman"
+	"github.com/v2fly/v2ray-core/v4/app/instman/command"
+	"github.com/v2fly/v2ray-core/v4/app/proxyman"
+	"github.com/v2fly/v2ray-core/v4/app/router"
+	"github.com/v2fly/v2ray-core/v4/common/net"
+	"github.com/v2fly/v2ray-core/v4/common/serial"
+	_ "github.com/v2fly/v2ray-core/v4/main/distro/all"
+	"github.com/v2fly/v2ray-core/v4/proxy/blackhole"
+	"github.com/v2fly/v2ray-core/v4/proxy/dokodemo"
+	"google.golang.org/protobuf/types/known/anypb"
+)
+
+type bindingInstance struct {
+	started  bool
+	instance *core.Instance
+}
+
+var binding bindingInstance
+
+func (b *bindingInstance) startAPIInstance() {
+
+	bindConfig := &core.Config{
+		App: []*anypb.Any{
+			serial.ToTypedMessage(&instman.Config{}),
+			serial.ToTypedMessage(&commander.Config{
+				Tag: "api",
+				Service: []*anypb.Any{
+					serial.ToTypedMessage(&command.Config{}),
+				},
+			}),
+			serial.ToTypedMessage(&router.Config{
+				Rule: []*router.RoutingRule{
+					{
+						InboundTag: []string{"api"},
+						TargetTag: &router.RoutingRule_Tag{
+							Tag: "api",
+						},
+					},
+				},
+			}),
+		},
+		Inbound: []*core.InboundHandlerConfig{
+			{
+				Tag: "api",
+				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
+					PortRange: net.SinglePortRange(10999),
+					Listen:    net.NewIPOrDomain(net.AnyIP),
+				}),
+				ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
+					Address:  net.NewIPOrDomain(net.LocalHostIP),
+					Port:     uint32(10999),
+					Networks: []net.Network{net.Network_TCP},
+				}),
+			},
+		},
+		Outbound: []*core.OutboundHandlerConfig{
+			{
+				Tag:           "default-outbound",
+				ProxySettings: serial.ToTypedMessage(&blackhole.Config{}),
+			},
+		},
+	}
+	bindConfig = withDefaultApps(bindConfig)
+
+	instance, err := core.New(bindConfig)
+	if err != nil {
+		panic(err)
+	}
+	err = instance.Start()
+	if err != nil {
+		panic(err)
+	}
+	b.instance = instance
+	return
+}
+
+func withDefaultApps(config *core.Config) *core.Config {
+	config.App = append(config.App, serial.ToTypedMessage(&dispatcher.Config{}))
+	config.App = append(config.App, serial.ToTypedMessage(&proxyman.InboundConfig{}))
+	config.App = append(config.App, serial.ToTypedMessage(&proxyman.OutboundConfig{}))
+	return config
+}
+
+func StartAPIInstance(basedir string) {
+	if binding.started {
+		return
+	}
+	binding.started = true
+	binding.startAPIInstance()
+}