-
Notifications
You must be signed in to change notification settings - Fork 211
Expand file tree
/
Copy pathconfig.go
More file actions
150 lines (127 loc) · 3.95 KB
/
config.go
File metadata and controls
150 lines (127 loc) · 3.95 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package config
import (
"encoding/json"
"fmt"
"os"
"strings"
"time"
"github.com/Ehco1996/ehco/internal/constant"
"github.com/Ehco1996/ehco/internal/relay/conf"
"github.com/Ehco1996/ehco/internal/tls"
myhttp "github.com/Ehco1996/ehco/pkg/http"
xConf "github.com/xtls/xray-core/infra/conf"
"go.uber.org/zap"
)
type Config struct {
PATH string `json:"-"`
NodeLabel string `json:"node_label,omitempty"`
WebHost string `json:"web_host,omitempty"`
WebPort int `json:"web_port,omitempty"`
// Dashboard login password for the embedded SPA. The username is
// implicit (single-tenant); only the password is configured.
DashboardPass string `json:"dashboard_pass,omitempty"`
// Bearer token for non-browser callers (mizhiwu reload client,
// scrapers, curl). Sent via Authorization: Bearer or X-Ehco-Token.
ApiToken string `json:"api_token,omitempty"`
LogLeveL string `json:"log_level,omitempty"`
EnablePing bool `json:"enable_ping,omitempty"`
ReloadInterval int `json:"reload_interval,omitempty"`
RelayConfigs []*conf.Config `json:"relay_configs"`
RelaySyncURL string `json:"relay_sync_url,omitempty"`
RelaySyncInterval int `json:"relay_sync_interval,omitempty"`
XRayConfig *xConf.Config `json:"xray_config,omitempty"`
SyncTrafficEndPoint string `json:"sync_traffic_endpoint,omitempty"`
lastLoadTime time.Time
l *zap.SugaredLogger
}
func NewConfig(path string) *Config {
return &Config{PATH: path, l: zap.S().Named("cfg")}
}
func (c *Config) NeedSyncFromServer() bool {
return strings.Contains(c.PATH, "http")
}
// LastLoadTime returns the wall-clock timestamp of the most recent
// successful (or attempted — see LoadConfig) reload. Zero before the
// first call.
func (c *Config) LastLoadTime() time.Time {
return c.lastLoadTime
}
func (c *Config) LoadConfig(force bool) error {
if c.ReloadInterval > 0 && time.Since(c.lastLoadTime).Seconds() < float64(c.ReloadInterval) && !force {
c.l.Warnf("Skip Load Config, last load time: %s", c.lastLoadTime)
return nil
}
// reset: drop all decoded state before re-unmarshaling. Several xray-conf
// types implement UnmarshalJSON that append rather than replace (e.g.
// PortList.Range), so re-decoding into a stale struct accumulates state
// across reloads. Forcing fields to nil makes the decoder allocate fresh
// objects.
c.RelayConfigs = nil
c.XRayConfig = nil
c.lastLoadTime = time.Now()
if c.NeedSyncFromServer() {
if err := c.readFromHttp(); err != nil {
return err
}
} else {
if err := c.readFromFile(); err != nil {
return err
}
}
return c.Adjust()
}
func (c *Config) readFromFile() error {
file, err := os.ReadFile(c.PATH)
if err != nil {
return err
}
c.l.Infof("Load Config From File: %s", c.PATH)
return json.Unmarshal([]byte(file), &c)
}
func (c *Config) readFromHttp() error {
c.l.Infof("Load Config From HTTP: %s", c.PATH)
return myhttp.GetJSONWithRetry(c.PATH, &c)
}
func (c *Config) Adjust() error {
if c.LogLeveL == "" {
c.LogLeveL = "info"
}
if c.WebHost == "" {
c.WebHost = "0.0.0.0"
}
for _, r := range c.RelayConfigs {
if err := r.Validate(); err != nil {
return err
}
}
// check relay config label is unique
labelMap := make(map[string]struct{})
for _, r := range c.RelayConfigs {
if _, ok := labelMap[r.Label]; ok {
return fmt.Errorf("relay label %s is not unique", r.Label)
}
labelMap[r.Label] = struct{}{}
}
// init tls when need
for _, r := range c.RelayConfigs {
if r.ListenType == constant.RelayTypeWSS || r.TransportType == constant.RelayTypeWSS {
if err := tls.InitTlsCfg(); err != nil {
return err
}
break
}
}
return nil
}
func (c *Config) NeedStartWebServer() bool {
return c.WebPort != 0
}
func (c *Config) NeedStartXrayServer() bool {
return c.XRayConfig != nil
}
func (c *Config) NeedStartRelayServer() bool {
return len(c.RelayConfigs) > 0
}
func (c *Config) NeedStartCmgr() bool {
return c.RelaySyncURL != "" && c.RelaySyncInterval > 0
}