V2bX/core/sing/sing.go

121 lines
2.8 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
"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-09-25 03:31:26 -04:00
"github.com/goccy/go-json"
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"
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
inbounds map[string]adapter.Inbound
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) {
2023-07-27 21:13:11 -04:00
options := option.Options{}
if len(c.SingConfig.OriginalPath) != 0 {
f, err := os.Open(c.SingConfig.OriginalPath)
if err != nil {
return nil, fmt.Errorf("open original config error: %s", err)
}
defer f.Close()
2023-08-26 10:48:24 -04:00
err = json.NewDecoder(f).Decode(&options)
if err != nil {
return nil, fmt.Errorf("decode 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,
2023-12-11 19:06:55 -05:00
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-10-19 01:18:07 -04:00
options.DNS = &option.DNSOptions{}
2023-09-21 00:34:03 -04:00
if c.SingConfig.DnsConfigPath != "" {
2023-10-19 01:18:07 -04:00
f, err := os.OpenFile(c.SingConfig.DnsConfigPath, os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
2023-10-26 01:06:43 -04:00
return nil, fmt.Errorf("failed to open or create sing dns config file: %s", err)
2023-10-19 01:18:07 -04:00
}
defer f.Close()
if err := json.NewDecoder(f).Decode(options.DNS); err != nil {
2023-10-26 01:06:43 -04:00
log.Warn(fmt.Sprintf(
"Failed to unmarshal sing dns config from file '%v': %v. Using default DNS options",
f.Name(), err))
2023-10-19 01:18:07 -04:00
options.DNS = &option.DNSOptions{}
2023-09-21 00:34:03 -04:00
}
os.Setenv("SING_DNS_PATH", c.SingConfig.DnsConfigPath)
}
2023-07-27 21:13:11 -04:00
ctx := context.Background()
2023-11-17 20:37:08 -05:00
b, err := box.New(box.Options{
2023-10-26 01:06:43 -04:00
Context: ctx,
Options: options,
2023-07-27 21:13:11 -04:00
})
if err != nil {
return nil, err
}
2023-10-26 01:06:43 -04:00
hs := NewHookServer(c.SingConfig.EnableConnClear)
2023-11-17 20:37:08 -05:00
b.Router().SetClashServer(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(),
inbounds: make(map[string]adapter.Inbound),
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"
}