Skip to content

Commit

Permalink
add etag-support to let user can disable this feature manually
Browse files Browse the repository at this point in the history
  • Loading branch information
wwqgtxx committed Sep 22, 2024
1 parent 17e58bd commit 72f9b79
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
18 changes: 16 additions & 2 deletions component/resource/vehicle.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ const (
dirMode os.FileMode = 0o755
)

var (
etag = false
)

func ETag() bool {
return etag
}

func SetETag(b bool) {
etag = b
}

func safeWrite(path string, buf []byte) error {
dir := filepath.Dir(path)

Expand Down Expand Up @@ -103,7 +115,7 @@ func (h *HTTPVehicle) Read(ctx context.Context, oldHash types.HashType) (buf []b
defer cancel()
header := h.header
setIfNoneMatch := false
if oldHash.IsValid() {
if etag && oldHash.IsValid() {
hashBytes, etag := cachefile.Cache().GetETagWithHash(h.url)
if oldHash.EqualBytes(hashBytes) && etag != "" {
if header == nil {
Expand Down Expand Up @@ -132,7 +144,9 @@ func (h *HTTPVehicle) Read(ctx context.Context, oldHash types.HashType) (buf []b
return
}
hash = types.MakeHash(buf)
cachefile.Cache().SetETagWithHash(h.url, hash.Bytes(), resp.Header.Get("ETag"))
if etag {
cachefile.Cache().SetETagWithHash(h.url, hash.Bytes(), resp.Header.Get("ETag"))
}
return
}

Expand Down
6 changes: 6 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/metacubex/mihomo/component/cidr"
"github.com/metacubex/mihomo/component/fakeip"
mihomoHttp "github.com/metacubex/mihomo/component/http"
"github.com/metacubex/mihomo/component/resource"
"github.com/metacubex/mihomo/component/sniffer"
"github.com/metacubex/mihomo/component/trie"
C "github.com/metacubex/mihomo/constant"
Expand Down Expand Up @@ -54,6 +55,7 @@ type General struct {
Sniffing bool `json:"sniffing"`
KeepAliveInterval int `json:"keep-alive-interval"`
GlobalUA string `json:"global-ua"`
ETagSupport bool `json:"etag-support"`
}

// Inbound
Expand Down Expand Up @@ -245,6 +247,7 @@ type RawConfig struct {
PreResolveProcessName bool `yaml:"pre-resolve-process-name" json:"pre-resolve-process-name"`
TCPConcurrent bool `yaml:"tcp-concurrent" json:"tcp-concurrent"`
GlobalUA string `yaml:"global-ua" json:"global-ua"`
ETagSupport bool `yaml:"etag-support" json:"etag-support"`
KeepAliveIdle int `yaml:"keep-alive-idle" json:"keep-alive-idle"`
KeepAliveInterval int `yaml:"keep-alive-interval" json:"keep-alive-interval"`
DisableKeepAlive bool `yaml:"disable-keep-alive" json:"disable-keep-alive"`
Expand Down Expand Up @@ -291,6 +294,7 @@ func DefaultRawConfig() *RawConfig {
PreResolveProcessName: false,
TCPConcurrent: true,
GlobalUA: "clash.meta/" + C.Version,
ETagSupport: true,
DNS: RawDNS{
Enable: true,
IPv6: true,
Expand Down Expand Up @@ -481,6 +485,7 @@ func ParseRawConfig(rawCfg *RawConfig) (*Config, error) {

func parseGeneral(cfg *RawConfig) (*General, error) {
mihomoHttp.SetUA(cfg.GlobalUA)
resource.SetETag(cfg.ETagSupport)

externalUI := cfg.ExternalUI

Expand Down Expand Up @@ -534,6 +539,7 @@ func parseGeneral(cfg *RawConfig) (*General, error) {
TCPConcurrent: cfg.TCPConcurrent,
KeepAliveInterval: cfg.KeepAliveInterval,
GlobalUA: cfg.GlobalUA,
ETagSupport: cfg.ETagSupport,
}, nil
}

Expand Down
2 changes: 2 additions & 0 deletions hub/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/metacubex/mihomo/component/profile/cachefile"
"github.com/metacubex/mihomo/component/profile/cachefileplain"
"github.com/metacubex/mihomo/component/resolver"
"github.com/metacubex/mihomo/component/resource"
"github.com/metacubex/mihomo/component/sniffer"
"github.com/metacubex/mihomo/component/trie"
"github.com/metacubex/mihomo/config"
Expand Down Expand Up @@ -142,6 +143,7 @@ func GetGeneral() *config.General {
PreResolveProcessName: tunnel.PreResolveProcessName(),
TCPConcurrent: dialer.GetTcpConcurrent(),
GlobalUA: mihomoHttp.UA(),
ETagSupport: resource.ETag(),
}

return general
Expand Down

0 comments on commit 72f9b79

Please sign in to comment.