mirror of
https://github.com/cedar2025/hysteria.git
synced 2025-06-08 05:19:56 +00:00
feat: traffic stats API secret auth
This commit is contained in:
parent
a633d3e320
commit
9ff8020803
@ -177,6 +177,7 @@ type serverConfigOutboundEntry struct {
|
|||||||
|
|
||||||
type serverConfigTrafficStats struct {
|
type serverConfigTrafficStats struct {
|
||||||
Listen string `mapstructure:"listen"`
|
Listen string `mapstructure:"listen"`
|
||||||
|
Secret string `mapstructure:"secret"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type serverConfigMasqueradeFile struct {
|
type serverConfigMasqueradeFile struct {
|
||||||
@ -596,7 +597,7 @@ func (c *serverConfig) fillEventLogger(hyConfig *server.Config) error {
|
|||||||
|
|
||||||
func (c *serverConfig) fillTrafficLogger(hyConfig *server.Config) error {
|
func (c *serverConfig) fillTrafficLogger(hyConfig *server.Config) error {
|
||||||
if c.TrafficStats.Listen != "" {
|
if c.TrafficStats.Listen != "" {
|
||||||
tss := trafficlogger.NewTrafficStatsServer()
|
tss := trafficlogger.NewTrafficStatsServer(c.TrafficStats.Secret)
|
||||||
hyConfig.TrafficLogger = tss
|
hyConfig.TrafficLogger = tss
|
||||||
go runTrafficStatsServer(c.TrafficStats.Listen, tss)
|
go runTrafficStatsServer(c.TrafficStats.Listen, tss)
|
||||||
}
|
}
|
||||||
|
@ -135,6 +135,7 @@ func TestServerConfig(t *testing.T) {
|
|||||||
},
|
},
|
||||||
TrafficStats: serverConfigTrafficStats{
|
TrafficStats: serverConfigTrafficStats{
|
||||||
Listen: ":9999",
|
Listen: ":9999",
|
||||||
|
Secret: "its_me_mario",
|
||||||
},
|
},
|
||||||
Masquerade: serverConfigMasquerade{
|
Masquerade: serverConfigMasquerade{
|
||||||
Type: "proxy",
|
Type: "proxy",
|
||||||
|
@ -100,6 +100,7 @@ outbounds:
|
|||||||
|
|
||||||
trafficStats:
|
trafficStats:
|
||||||
listen: :9999
|
listen: :9999
|
||||||
|
secret: its_me_mario
|
||||||
|
|
||||||
masquerade:
|
masquerade:
|
||||||
type: proxy
|
type: proxy
|
||||||
|
@ -20,10 +20,11 @@ type TrafficStatsServer interface {
|
|||||||
http.Handler
|
http.Handler
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTrafficStatsServer() TrafficStatsServer {
|
func NewTrafficStatsServer(secret string) TrafficStatsServer {
|
||||||
return &trafficStatsServerImpl{
|
return &trafficStatsServerImpl{
|
||||||
StatsMap: make(map[string]*trafficStatsEntry),
|
StatsMap: make(map[string]*trafficStatsEntry),
|
||||||
KickMap: make(map[string]struct{}),
|
KickMap: make(map[string]struct{}),
|
||||||
|
Secret: secret,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -31,6 +32,7 @@ type trafficStatsServerImpl struct {
|
|||||||
Mutex sync.RWMutex
|
Mutex sync.RWMutex
|
||||||
StatsMap map[string]*trafficStatsEntry
|
StatsMap map[string]*trafficStatsEntry
|
||||||
KickMap map[string]struct{}
|
KickMap map[string]struct{}
|
||||||
|
Secret string
|
||||||
}
|
}
|
||||||
|
|
||||||
type trafficStatsEntry struct {
|
type trafficStatsEntry struct {
|
||||||
@ -60,6 +62,10 @@ func (s *trafficStatsServerImpl) Log(id string, tx, rx uint64) (ok bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *trafficStatsServerImpl) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (s *trafficStatsServerImpl) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if s.Secret != "" && r.Header.Get("Authorization") != s.Secret {
|
||||||
|
http.Error(w, "unauthorized", http.StatusUnauthorized)
|
||||||
|
return
|
||||||
|
}
|
||||||
if r.Method == http.MethodGet && r.URL.Path == "/" {
|
if r.Method == http.MethodGet && r.URL.Path == "/" {
|
||||||
_, _ = w.Write([]byte(indexHTML))
|
_, _ = w.Write([]byte(indexHTML))
|
||||||
return
|
return
|
||||||
|
Loading…
x
Reference in New Issue
Block a user