-
Notifications
You must be signed in to change notification settings - Fork 0
/
sub_configure.go
100 lines (88 loc) · 3.42 KB
/
sub_configure.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"context"
"errors"
"github.com/ServiceWeaver/weaver"
"github.com/ServiceWeaver/weaver/metadata"
)
var (
responseOptionNotConfiguredError = errors.New("no response option configured")
invalidContextError = errors.New("invalid context")
subPublishPathNotConfiguredError = errors.New("sub publish path not configured")
subAuthParamNameNotConfiguredError = errors.New("sub auth param name not configured")
)
var _ subConfigureProvider = (*subConfigure)(nil)
type subConfigureProvider interface {
GetSubFilePaths(ctx context.Context) ([]string, error)
GetUrlSubs(ctx context.Context) ([]string, int, error)
GetSubPublishPath(ctx context.Context) (string, error)
GetSubAuthParamName(ctx context.Context) (string, error)
GetResponseOption(ctx context.Context) (*responseOption, error)
}
type subConfig struct {
weaver.AutoMarshal
PublicSubFilePaths []string `toml:"public_sub_file_paths"`
PrivateSubFilePaths []string `toml:"private_sub_file_paths"`
UrlSubFetchTimeoutSeconds int `toml:"url_sub_fetch_timeout_seconds"`
PublicUrlSubs []string `toml:"public_url_subs"`
PrivateUrlSubs []string `toml:"private_url_subs"`
PrivateSubToken string `toml:"private_sub_token"`
SubPublishPath string `toml:"sub_publish_path,omitempty"`
SubAuthParamName string `toml:"sub_auth_param_name,omitempty"`
ResponseOption *responseOption `toml:"response_option,omitempty"`
}
type responseOption struct {
weaver.AutoMarshal
//return to the client see https://www.clashverge.dev/guide/url_schemes.html#_
UpdateIntervalHours int `toml:"update_interval_hours,omitempty"`
ProfileWebPage string `toml:"profile_web_page,omitempty"`
// subscription-userinfo: upload=1234; download=2234; total=1024000; expire=2218532293
}
type subConfigure struct {
weaver.Implements[subConfigureProvider]
weaver.WithConfig[subConfig]
}
func (s *subConfigure) GetSubFilePaths(ctx context.Context) ([]string, error) {
config := s.Config()
meta, ok := metadata.FromContext(ctx)
if !ok {
return nil, invalidContextError
}
privateSubToken := meta["privateToken"]
if privateSubToken != config.PrivateSubToken {
return config.PublicSubFilePaths, nil
}
return append(config.PrivateSubFilePaths, config.PublicSubFilePaths...), nil
}
func (s *subConfigure) GetUrlSubs(ctx context.Context) ([]string, int, error) {
config := s.Config()
meta, ok := metadata.FromContext(ctx)
if !ok {
return nil, 0, invalidContextError
}
privateSubToken := meta["privateToken"]
if privateSubToken != config.PrivateSubToken {
s.Logger(ctx).Info("token check pass")
return config.PublicUrlSubs, config.UrlSubFetchTimeoutSeconds, nil
}
return append(config.PrivateUrlSubs, config.PublicUrlSubs...), config.UrlSubFetchTimeoutSeconds, nil
}
func (s *subConfigure) GetSubPublishPath(ctx context.Context) (string, error) {
if s.Config().SubPublishPath == "" {
return "", subPublishPathNotConfiguredError
}
return s.Config().SubPublishPath, nil
}
func (s *subConfigure) GetSubAuthParamName(ctx context.Context) (string, error) {
if s.Config().SubAuthParamName == "" {
return "", subAuthParamNameNotConfiguredError
}
return s.Config().SubAuthParamName, nil
}
func (s *subConfigure) GetResponseOption(ctx context.Context) (*responseOption, error) {
config := s.Config()
if config.ResponseOption == nil {
return nil, responseOptionNotConfiguredError
}
return config.ResponseOption, nil
}