github.com/imannamdari/v2ray-core/v5@v5.0.5/main/v2binding/v2binding.go (about)

     1  package v2binding
     2  
     3  import (
     4  	"google.golang.org/protobuf/types/known/anypb"
     5  
     6  	core "github.com/imannamdari/v2ray-core/v5"
     7  	"github.com/imannamdari/v2ray-core/v5/app/commander"
     8  	"github.com/imannamdari/v2ray-core/v5/app/dispatcher"
     9  	"github.com/imannamdari/v2ray-core/v5/app/instman"
    10  	"github.com/imannamdari/v2ray-core/v5/app/instman/command"
    11  	"github.com/imannamdari/v2ray-core/v5/app/proxyman"
    12  	"github.com/imannamdari/v2ray-core/v5/app/router"
    13  	"github.com/imannamdari/v2ray-core/v5/common/net"
    14  	"github.com/imannamdari/v2ray-core/v5/common/serial"
    15  	_ "github.com/imannamdari/v2ray-core/v5/main/distro/all"
    16  	"github.com/imannamdari/v2ray-core/v5/proxy/blackhole"
    17  	"github.com/imannamdari/v2ray-core/v5/proxy/dokodemo"
    18  )
    19  
    20  type bindingInstance struct {
    21  	started  bool
    22  	instance *core.Instance
    23  }
    24  
    25  var binding bindingInstance
    26  
    27  func (b *bindingInstance) startAPIInstance() {
    28  	bindConfig := &core.Config{
    29  		App: []*anypb.Any{
    30  			serial.ToTypedMessage(&instman.Config{}),
    31  			serial.ToTypedMessage(&commander.Config{
    32  				Tag: "api",
    33  				Service: []*anypb.Any{
    34  					serial.ToTypedMessage(&command.Config{}),
    35  				},
    36  			}),
    37  			serial.ToTypedMessage(&router.Config{
    38  				Rule: []*router.RoutingRule{
    39  					{
    40  						InboundTag: []string{"api"},
    41  						TargetTag: &router.RoutingRule_Tag{
    42  							Tag: "api",
    43  						},
    44  					},
    45  				},
    46  			}),
    47  		},
    48  		Inbound: []*core.InboundHandlerConfig{
    49  			{
    50  				Tag: "api",
    51  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
    52  					PortRange: net.SinglePortRange(10999),
    53  					Listen:    net.NewIPOrDomain(net.AnyIP),
    54  				}),
    55  				ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
    56  					Address:  net.NewIPOrDomain(net.LocalHostIP),
    57  					Port:     uint32(10999),
    58  					Networks: []net.Network{net.Network_TCP},
    59  				}),
    60  			},
    61  		},
    62  		Outbound: []*core.OutboundHandlerConfig{
    63  			{
    64  				Tag:           "default-outbound",
    65  				ProxySettings: serial.ToTypedMessage(&blackhole.Config{}),
    66  			},
    67  		},
    68  	}
    69  	bindConfig = withDefaultApps(bindConfig)
    70  
    71  	instance, err := core.New(bindConfig)
    72  	if err != nil {
    73  		panic(err)
    74  	}
    75  	err = instance.Start()
    76  	if err != nil {
    77  		panic(err)
    78  	}
    79  	b.instance = instance
    80  }
    81  
    82  func withDefaultApps(config *core.Config) *core.Config {
    83  	config.App = append(config.App, serial.ToTypedMessage(&dispatcher.Config{}))
    84  	config.App = append(config.App, serial.ToTypedMessage(&proxyman.InboundConfig{}))
    85  	config.App = append(config.App, serial.ToTypedMessage(&proxyman.OutboundConfig{}))
    86  	return config
    87  }
    88  
    89  func StartAPIInstance(basedir string) {
    90  	if binding.started {
    91  		return
    92  	}
    93  	binding.started = true
    94  	binding.startAPIInstance()
    95  }