1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-10 14:26:26 -05:00
v2fly/main/v2binding/v2binding.go
Loyalsoldier dce8764fd7
Lint: fix lint (#1427)
* Lint: replace golint with revive
* Lint: fix lint
2021-11-27 17:16:41 +08:00

98 lines
2.5 KiB
Go

package v2binding
import (
"google.golang.org/protobuf/types/known/anypb"
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"
// Initialization
_ "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"
)
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
}
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()
}