functions.go 875 B

1234567891011121314151617181920212223242526272829303132333435
  1. package core
  2. import (
  3. "context"
  4. "v2ray.com/core/common"
  5. "v2ray.com/core/common/buf"
  6. )
  7. // CreateObject creates a new object based on the given V2Ray instance and config. The V2Ray instance may be nil.
  8. func CreateObject(v *Instance, config interface{}) (interface{}, error) {
  9. ctx := context.Background()
  10. if v != nil {
  11. ctx = context.WithValue(ctx, v2rayKey, v)
  12. }
  13. return common.CreateObject(ctx, config)
  14. }
  15. // StartInstance starts a new V2Ray instance with given serialized config.
  16. func StartInstance(configFormat string, configBytes []byte) (*Instance, error) {
  17. var mb buf.MultiBuffer
  18. common.Must2(mb.Write(configBytes))
  19. config, err := LoadConfig(configFormat, "", &mb)
  20. if err != nil {
  21. return nil, err
  22. }
  23. instance, err := New(config)
  24. if err != nil {
  25. return nil, err
  26. }
  27. if err := instance.Start(); err != nil {
  28. return nil, err
  29. }
  30. return instance, nil
  31. }