mirror of
https://github.com/daeuniverse/dae.git
synced 2025-07-14 01:40:32 +07:00
feat: subscription supports file://
This commit is contained in:
@ -122,6 +122,5 @@ See [example.dae](https://github.com/v2rayA/dae/blob/main/example.dae).
|
|||||||
1. MACv2 extension extraction.
|
1. MACv2 extension extraction.
|
||||||
1. Log to userspace.
|
1. Log to userspace.
|
||||||
1. Support include section.
|
1. Support include section.
|
||||||
1. Subscription section supports "file://"
|
1. Subscription section supports key. And support to filter by subscription key.
|
||||||
1. Subscription section supports key.
|
|
||||||
1. ...
|
1. ...
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package internal
|
package internal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
@ -9,6 +11,8 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@ -80,26 +84,71 @@ func resolveSubscriptionAsSIP008(log *logrus.Logger, b []byte) (nodes []string,
|
|||||||
return nodes, nil
|
return nodes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ResolveSubscription(log *logrus.Logger, subscription string) (nodes []string, err error) {
|
func resolveFile(u *url.URL, configDir string) (b []byte, err error) {
|
||||||
|
if u.Host == "" {
|
||||||
|
return nil, fmt.Errorf("not support absolute path")
|
||||||
|
}
|
||||||
|
/// Relative location.
|
||||||
|
// Make sure path safety.
|
||||||
|
path := filepath.Join(configDir, u.Host, u.Path)
|
||||||
|
if err = common.IsFileInSubDir(path, configDir); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
/// Read and resolve
|
||||||
|
f, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// Resolve the first line instruction.
|
||||||
|
fReader := bufio.NewReader(f)
|
||||||
|
b, err = fReader.Peek(1)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if string(b[0]) == "@" {
|
||||||
|
// Instruction line. But not support yet.
|
||||||
|
_, _, err = fReader.ReadLine()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
b, err = io.ReadAll(fReader)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return bytes.TrimSpace(b), err
|
||||||
|
}
|
||||||
|
|
||||||
|
func ResolveSubscription(log *logrus.Logger, configDir string, subscription string) (nodes []string, err error) {
|
||||||
u, err := url.Parse(subscription)
|
u, err := url.Parse(subscription)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to parse subscription \"%v\": %w", subscription, err)
|
return nil, fmt.Errorf("failed to parse subscription \"%v\": %w", subscription, err)
|
||||||
}
|
}
|
||||||
|
log.Debugf("ResolveSubscription: %v", subscription)
|
||||||
|
var (
|
||||||
|
b []byte
|
||||||
|
resp *http.Response
|
||||||
|
)
|
||||||
switch u.Scheme {
|
switch u.Scheme {
|
||||||
case "file":
|
case "file":
|
||||||
// TODO
|
b, err = resolveFile(u, configDir)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to resolve file: %w", err)
|
||||||
|
}
|
||||||
|
goto resolve
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
log.Debugf("ResolveSubscription: %v", subscription)
|
resp, err = http.Get(subscription)
|
||||||
resp, err := http.Get(subscription)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
b, err := io.ReadAll(resp.Body)
|
b, err = io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
resolve:
|
||||||
if nodes, err = resolveSubscriptionAsSIP008(log, b); err == nil {
|
if nodes, err = resolveSubscriptionAsSIP008(log, b); err == nil {
|
||||||
return nodes, nil
|
return nodes, nil
|
||||||
} else {
|
} else {
|
||||||
|
@ -11,6 +11,7 @@ import (
|
|||||||
"github.com/v2rayA/dae/pkg/logger"
|
"github.com/v2rayA/dae/pkg/logger"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"path/filepath"
|
||||||
"syscall"
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -55,7 +56,7 @@ func Run(log *logrus.Logger, param *config.Params) (err error) {
|
|||||||
nodeList := make([]string, len(param.Node))
|
nodeList := make([]string, len(param.Node))
|
||||||
copy(nodeList, param.Node)
|
copy(nodeList, param.Node)
|
||||||
for _, sub := range param.Subscription {
|
for _, sub := range param.Subscription {
|
||||||
nodes, err := internal.ResolveSubscription(log, sub)
|
nodes, err := internal.ResolveSubscription(log, filepath.Dir(cfgFile), sub)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warnf(`failed to resolve subscription "%v": %v`, sub, err)
|
log.Warnf(`failed to resolve subscription "%v": %v`, sub, err)
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,7 @@ import (
|
|||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -307,3 +308,18 @@ func FuzzyDecode(to interface{}, val string) bool {
|
|||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func IsFileInSubDir(filePath string, dir string) (err error) {
|
||||||
|
fileDir := filepath.Dir(filePath)
|
||||||
|
if len(dir) == 0 {
|
||||||
|
return fmt.Errorf("bad dir: %v", dir)
|
||||||
|
}
|
||||||
|
rel, err := filepath.Rel(dir, fileDir)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if strings.HasPrefix(rel, "..") {
|
||||||
|
return fmt.Errorf("file is out of scope: %v", rel)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user