mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-30 05:56:54 -05:00
V2Ray API Instance
This commit is contained in:
parent
c7c4effbd2
commit
58b16b8a1e
13
main/v2binding/v2api/api.go
Normal file
13
main/v2binding/v2api/api.go
Normal file
@ -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
main/v2binding/v2binding.go
Normal file
96
main/v2binding/v2binding.go
Normal file
@ -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()
|
||||
}
|
Loading…
Reference in New Issue
Block a user