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{}
|
2023-08-25 00:42:47 -04:00
|
|
|
if len(c.SingConfig.OriginalPath) != 0 {
|
2024-01-25 09:06:37 -05:00
|
|
|
data, err := os.ReadFile(c.SingConfig.OriginalPath)
|
2023-08-25 00:42:47 -04:00
|
|
|
if err != nil {
|
2024-01-25 09:06:37 -05:00
|
|
|
return nil, fmt.Errorf("read original config error: %s", err)
|
2023-08-25 00:42:47 -04:00
|
|
|
}
|
2024-01-25 09:06:37 -05:00
|
|
|
err = json.Unmarshal(data, &options)
|
2023-08-25 00:42:47 -04:00
|
|
|
if err != nil {
|
2024-01-25 09:06:37 -05:00
|
|
|
return nil, fmt.Errorf("unmarshal original config error: %s", err)
|
2023-08-25 00:42:47 -04:00
|
|
|
}
|
|
|
|
}
|
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-01-21 23:38:55 -05:00
|
|
|
Context: context.Background(),
|
2023-10-26 01:06:43 -04:00
|
|
|
Options: options,
|
2023-07-27 21:13:11 -04:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-07-14 11:33:54 -04:00
|
|
|
hs := NewHookServer(b.Router().GetCtx(), 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",
|
2023-08-08 06:24:10 -04:00
|
|
|
"trojan",
|
|
|
|
"hysteria",
|
2023-11-17 17:05:28 -05:00
|
|
|
"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"
|
|
|
|
}
|