Skip to content

Commit

Permalink
Support sing-box config as subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Sep 17, 2023
1 parent ce53488 commit d0cefb3
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 16 deletions.
4 changes: 2 additions & 2 deletions cmd/serenity/command_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"runtime/debug"
"syscall"

"github.com/spf13/cobra"

"github.com/sagernet/serenity"
"github.com/sagernet/sing-box/log"

"github.com/spf13/cobra"
)

var configPath string
Expand Down
4 changes: 2 additions & 2 deletions cmd/serenity/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package main

import (
"github.com/sagernet/sing-box/log"

"github.com/spf13/cobra"

"github.com/sagernet/sing-box/log"
)

var command = &cobra.Command{
Expand Down
7 changes: 4 additions & 3 deletions libsubscription/subscription.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
package libsubscription

import (
"github.com/sagernet/sing-box/log"
E "github.com/sagernet/sing/common/exceptions"
)

var subscriptionParsers = []func(string) ([]Server, error){
ParseBoxSubscription,
ParseClashSubscription,
ParseSIP008Subscription,
ParseRawSubscription,
}

func ParseSubscription(content string) ([]Server, error) {
var pErr error
for _, parser := range subscriptionParsers {
servers, err := parser(content)
if len(servers) > 0 {
return servers, nil
}
log.Trace("parse subscription failed: ", err)
pErr = E.Errors(pErr, err)
}
return nil, E.New("no servers found")
return nil, E.Cause(pErr, "no servers found")
}
12 changes: 6 additions & 6 deletions libsubscription/subscription_clash.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ package libsubscription
import (
"strings"

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/format"
N "github.com/sagernet/sing/common/network"

"github.com/Dreamacro/clash/adapter"
clash_outbound "github.com/Dreamacro/clash/adapter/outbound"
"github.com/Dreamacro/clash/common/structure"
"github.com/Dreamacro/clash/config"
"github.com/Dreamacro/clash/constant"

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/format"
N "github.com/sagernet/sing/common/network"
)

func ParseClashSubscription(content string) ([]Server, error) {
Expand Down
25 changes: 25 additions & 0 deletions libsubscription/subscription_sing_box.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package libsubscription

import (
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions"
)

func ParseBoxSubscription(content string) ([]Server, error) {
var options option.Options
err := options.UnmarshalJSON([]byte(content))
if err != nil {
return nil, err
}
if len(options.Outbounds) == 0 {
return nil, E.New("no servers found")
}
return common.Map(options.Outbounds, func(it option.Outbound) Server {
return Server{
Name: it.Tag,
Outbounds: []option.Outbound{it},
}
}), nil
}
6 changes: 3 additions & 3 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import (
"strings"
"time"

"github.com/go-chi/chi/v5"
"golang.org/x/net/http2"

"github.com/sagernet/sing-box"
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/common/tls"
Expand All @@ -19,9 +22,6 @@ import (
"github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/logger"

"github.com/go-chi/chi/v5"
"golang.org/x/net/http2"
)

var _ adapter.Service = (*Server)(nil)
Expand Down

0 comments on commit d0cefb3

Please sign in to comment.