V2bX/core/sing/sing.go

107 lines
2.3 KiB
Go
Raw Normal View History

2023-07-27 21:13:11 -04:00
package sing
import (
"context"
"fmt"
2023-10-13 03:32:06 -04:00
"os"
2023-10-26 01:06:43 -04:00
2024-12-12 16:22:44 -05:00
"github.com/sagernet/sing-box/include"
2023-10-26 01:06:43 -04:00
"github.com/sagernet/sing-box/log"
2023-10-13 03:32:06 -04:00
2023-07-29 07:27:15 -04:00
"github.com/InazumaV/V2bX/conf"
vCore "github.com/InazumaV/V2bX/core"
2023-10-26 01:06:43 -04:00
box "github.com/sagernet/sing-box"
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/option"
2024-12-12 16:22:44 -05:00
"github.com/sagernet/sing/common/json"
2023-07-27 21:13:11 -04:00
)
2023-10-26 01:06:43 -04:00
var _ vCore.Core = (*Sing)(nil)
2023-07-27 21:13:11 -04:00
2023-09-21 00:34:03 -04:00
type DNSConfig struct {
Servers []map[string]interface{} `json:"servers"`
Rules []map[string]interface{} `json:"rules"`
}
2023-10-26 01:06:43 -04:00
type Sing struct {
2023-11-17 20:37:08 -05:00
box *box.Box
2023-10-13 03:32:06 -04:00
ctx context.Context
2023-07-27 21:13:11 -04:00
hookServer *HookServer
2023-11-17 20:37:08 -05:00
router adapter.Router
2023-10-26 01:06:43 -04:00
logFactory log.Factory
2023-07-27 21:13:11 -04:00
}
func init() {
vCore.RegisterCore("sing", New)
}
2023-07-29 06:47:47 -04:00
func New(c *conf.CoreConfig) (vCore.Core, error) {
2024-12-12 16:22:44 -05:00
ctx := context.Background()
ctx = box.Context(ctx, include.InboundRegistry(), include.OutboundRegistry(), include.EndpointRegistry())
2023-07-27 21:13:11 -04:00
options := option.Options{}
if len(c.SingConfig.OriginalPath) != 0 {
2024-01-25 09:06:37 -05:00
data, err := os.ReadFile(c.SingConfig.OriginalPath)
if err != nil {
2024-01-25 09:06:37 -05:00
return nil, fmt.Errorf("read original config error: %s", err)
}
2024-12-12 16:22:44 -05:00
options, err = json.UnmarshalExtendedContext[option.Options](ctx, data)
if err != nil {
2024-01-25 09:06:37 -05:00
return nil, fmt.Errorf("unmarshal original config error: %s", err)
}
}
2023-07-29 06:47:47 -04:00
options.Log = &option.LogOptions{
Disabled: c.SingConfig.LogConfig.Disabled,
Level: c.SingConfig.LogConfig.Level,
Timestamp: c.SingConfig.LogConfig.Timestamp,
Output: c.SingConfig.LogConfig.Output,
}
2023-08-25 23:38:18 -04:00
options.NTP = &option.NTPOptions{
Enabled: c.SingConfig.NtpConfig.Enable,
WriteToSystem: true,
2024-06-12 11:06:22 -04:00
ServerOptions: option.ServerOptions{
Server: c.SingConfig.NtpConfig.Server,
ServerPort: c.SingConfig.NtpConfig.ServerPort,
},
2023-08-25 23:38:18 -04:00
}
2023-09-21 00:34:03 -04:00
os.Setenv("SING_DNS_PATH", "")
2023-11-17 20:37:08 -05:00
b, err := box.New(box.Options{
2024-12-12 16:22:44 -05:00
Context: ctx,
2023-10-26 01:06:43 -04:00
Options: options,
2023-07-27 21:13:11 -04:00
})
if err != nil {
return nil, err
}
2025-01-10 02:33:05 -05:00
hs := NewHookServer()
2024-12-12 16:22:44 -05:00
b.Router().SetTracker(hs)
2023-10-26 01:06:43 -04:00
return &Sing{
2023-11-17 20:37:08 -05:00
ctx: b.Router().GetCtx(),
2023-10-26 01:06:43 -04:00
box: b,
hookServer: hs,
2023-11-17 20:37:08 -05:00
router: b.Router(),
2023-10-26 01:06:43 -04:00
logFactory: b.LogFactory(),
2023-07-27 21:13:11 -04:00
}, nil
}
2023-10-26 01:06:43 -04:00
func (b *Sing) Start() error {
return b.box.Start()
2023-07-27 21:13:11 -04:00
}
2023-10-26 01:06:43 -04:00
func (b *Sing) Close() error {
return b.box.Close()
2023-07-27 21:13:11 -04:00
}
2023-10-26 01:06:43 -04:00
func (b *Sing) Protocols() []string {
2023-07-27 21:13:11 -04:00
return []string{
2023-08-19 08:06:42 -04:00
"vmess",
"vless",
2023-07-27 21:13:11 -04:00
"shadowsocks",
"trojan",
"hysteria",
"hysteria2",
2023-07-27 21:13:11 -04:00
}
}
2023-08-20 03:13:52 -04:00
2023-10-26 01:06:43 -04:00
func (b *Sing) Type() string {
2023-08-20 03:13:52 -04:00
return "sing"
}