mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-23 17:11:29 +00:00
59 lines
1,008 B
Go
59 lines
1,008 B
Go
|
package daemon
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"os"
|
||
|
"sync"
|
||
|
|
||
|
"github.com/sagernet/sing-box"
|
||
|
"github.com/sagernet/sing-box/option"
|
||
|
)
|
||
|
|
||
|
type Instance struct {
|
||
|
access sync.Mutex
|
||
|
boxInstance *box.Box
|
||
|
boxCancel context.CancelFunc
|
||
|
}
|
||
|
|
||
|
func (i *Instance) Running() bool {
|
||
|
i.access.Lock()
|
||
|
defer i.access.Unlock()
|
||
|
return i.boxInstance != nil
|
||
|
}
|
||
|
|
||
|
func (i *Instance) Start(options option.Options) error {
|
||
|
i.access.Lock()
|
||
|
defer i.access.Unlock()
|
||
|
if i.boxInstance != nil {
|
||
|
i.boxCancel()
|
||
|
i.boxInstance.Close()
|
||
|
}
|
||
|
ctx, cancel := context.WithCancel(context.Background())
|
||
|
instance, err := box.New(ctx, options)
|
||
|
if err != nil {
|
||
|
cancel()
|
||
|
return err
|
||
|
}
|
||
|
err = instance.Start()
|
||
|
if err != nil {
|
||
|
cancel()
|
||
|
return err
|
||
|
}
|
||
|
i.boxInstance = instance
|
||
|
i.boxCancel = cancel
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (i *Instance) Close() error {
|
||
|
i.access.Lock()
|
||
|
defer i.access.Unlock()
|
||
|
if i.boxInstance == nil {
|
||
|
return os.ErrClosed
|
||
|
}
|
||
|
i.boxCancel()
|
||
|
err := i.boxInstance.Close()
|
||
|
i.boxInstance = nil
|
||
|
i.boxCancel = nil
|
||
|
return err
|
||
|
}
|