sing-box/script/source.go
2025-02-04 15:00:59 +08:00

31 lines
802 B
Go

package script
import (
"context"
"github.com/sagernet/sing-box/adapter"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/option"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/logger"
"github.com/dop251/goja"
)
type Source interface {
StartContext(ctx context.Context, startContext *adapter.HTTPStartContext) error
PostStart() error
Program() *goja.Program
Close() error
}
func NewSource(ctx context.Context, logger logger.Logger, options option.Script) (Source, error) {
switch options.Source {
case C.ScriptSourceTypeLocal:
return NewLocalSource(ctx, logger, options)
case C.ScriptSourceTypeRemote:
return NewRemoteSource(ctx, logger, options)
default:
return nil, E.New("unknown source type: ", options.Source)
}
}