2021-06-28 16:00:05 -04:00
|
|
|
package instman
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-10-28 06:34:19 -04:00
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
core "github.com/v2fly/v2ray-core/v5"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/features/extension"
|
2021-06-28 16:00:05 -04:00
|
|
|
)
|
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
//go:generate go run github.com/v2fly/v2ray-core/v5/common/errors/errorgen
|
2021-06-28 16:00:05 -04:00
|
|
|
|
|
|
|
type InstanceMgr struct {
|
2021-11-27 04:16:41 -05:00
|
|
|
config *Config // nolint: structcheck
|
2021-06-28 16:00:05 -04:00
|
|
|
instances map[string]*core.Instance
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i InstanceMgr) Type() interface{} {
|
|
|
|
return extension.InstanceManagementType()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i InstanceMgr) Start() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i InstanceMgr) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i InstanceMgr) ListInstance(ctx context.Context) ([]string, error) {
|
|
|
|
var instanceNames []string
|
2021-10-28 06:34:19 -04:00
|
|
|
for k := range i.instances {
|
2021-06-28 16:00:05 -04:00
|
|
|
instanceNames = append(instanceNames, k)
|
|
|
|
}
|
|
|
|
return instanceNames, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i InstanceMgr) AddInstance(ctx context.Context, name string, config []byte, configType string) error {
|
|
|
|
coreConfig, err := core.LoadConfig(configType, config)
|
|
|
|
if err != nil {
|
|
|
|
return newError("unable to load config").Base(err)
|
|
|
|
}
|
|
|
|
instance, err := core.New(coreConfig)
|
|
|
|
if err != nil {
|
|
|
|
return newError("unable to create instance").Base(err)
|
|
|
|
}
|
|
|
|
i.instances[name] = instance
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i InstanceMgr) StartInstance(ctx context.Context, name string) error {
|
|
|
|
err := i.instances[name].Start()
|
|
|
|
if err != nil {
|
|
|
|
return newError("failed to start instance").Base(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i InstanceMgr) StopInstance(ctx context.Context, name string) error {
|
|
|
|
err := i.instances[name].Close()
|
|
|
|
if err != nil {
|
|
|
|
return newError("failed to stop instance").Base(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i InstanceMgr) UntrackInstance(ctx context.Context, name string) error {
|
|
|
|
delete(i.instances, name)
|
|
|
|
return nil
|
|
|
|
}
|
2021-06-28 16:16:48 -04:00
|
|
|
|
|
|
|
func NewInstanceMgr(ctx context.Context, config *Config) (extension.InstanceManagement, error) {
|
|
|
|
return InstanceMgr{instances: map[string]*core.Instance{}}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
|
|
|
var f extension.InstanceManagement
|
|
|
|
var err error
|
|
|
|
if f, err = NewInstanceMgr(ctx, config.(*Config)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return f, nil
|
|
|
|
}))
|
|
|
|
}
|