feat: add etag-support to let user can disable this feature manually

This commit is contained in:
wwqgtxx 2024-09-22 14:41:45 +08:00
parent b7cb6774bf
commit ddfa9e8671
3 changed files with 24 additions and 2 deletions

View File

@ -21,6 +21,18 @@ const (
dirMode os.FileMode = 0o755 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 { func safeWrite(path string, buf []byte) error {
dir := filepath.Dir(path) dir := filepath.Dir(path)
@ -103,7 +115,7 @@ func (h *HTTPVehicle) Read(ctx context.Context, oldHash types.HashType) (buf []b
defer cancel() defer cancel()
header := h.header header := h.header
setIfNoneMatch := false setIfNoneMatch := false
if oldHash.IsValid() { if etag && oldHash.IsValid() {
hashBytes, etag := cachefile.Cache().GetETagWithHash(h.url) hashBytes, etag := cachefile.Cache().GetETagWithHash(h.url)
if oldHash.EqualBytes(hashBytes) && etag != "" { if oldHash.EqualBytes(hashBytes) && etag != "" {
if header == nil { if header == nil {
@ -132,7 +144,9 @@ func (h *HTTPVehicle) Read(ctx context.Context, oldHash types.HashType) (buf []b
return return
} }
hash = types.MakeHash(buf) hash = types.MakeHash(buf)
if etag {
cachefile.Cache().SetETagWithHash(h.url, hash.Bytes(), resp.Header.Get("ETag")) cachefile.Cache().SetETagWithHash(h.url, hash.Bytes(), resp.Header.Get("ETag"))
}
return return
} }

View File

@ -24,6 +24,7 @@ import (
mihomoHttp "github.com/metacubex/mihomo/component/http" mihomoHttp "github.com/metacubex/mihomo/component/http"
P "github.com/metacubex/mihomo/component/process" P "github.com/metacubex/mihomo/component/process"
"github.com/metacubex/mihomo/component/resolver" "github.com/metacubex/mihomo/component/resolver"
"github.com/metacubex/mihomo/component/resource"
"github.com/metacubex/mihomo/component/sniffer" "github.com/metacubex/mihomo/component/sniffer"
tlsC "github.com/metacubex/mihomo/component/tls" tlsC "github.com/metacubex/mihomo/component/tls"
"github.com/metacubex/mihomo/component/trie" "github.com/metacubex/mihomo/component/trie"
@ -65,6 +66,7 @@ type General struct {
Sniffing bool `json:"sniffing"` Sniffing bool `json:"sniffing"`
GlobalClientFingerprint string `json:"global-client-fingerprint"` GlobalClientFingerprint string `json:"global-client-fingerprint"`
GlobalUA string `json:"global-ua"` GlobalUA string `json:"global-ua"`
ETagSupport bool `json:"etag-support"`
} }
// Inbound config // Inbound config
@ -381,6 +383,7 @@ type RawConfig struct {
FindProcessMode P.FindProcessMode `yaml:"find-process-mode" json:"find-process-mode"` FindProcessMode P.FindProcessMode `yaml:"find-process-mode" json:"find-process-mode"`
GlobalClientFingerprint string `yaml:"global-client-fingerprint" json:"global-client-fingerprint"` GlobalClientFingerprint string `yaml:"global-client-fingerprint" json:"global-client-fingerprint"`
GlobalUA string `yaml:"global-ua" json:"global-ua"` 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"` KeepAliveIdle int `yaml:"keep-alive-idle" json:"keep-alive-idle"`
KeepAliveInterval int `yaml:"keep-alive-interval" json:"keep-alive-interval"` KeepAliveInterval int `yaml:"keep-alive-interval" json:"keep-alive-interval"`
DisableKeepAlive bool `yaml:"disable-keep-alive" json:"disable-keep-alive"` DisableKeepAlive bool `yaml:"disable-keep-alive" json:"disable-keep-alive"`
@ -444,6 +447,7 @@ func DefaultRawConfig() *RawConfig {
TCPConcurrent: false, TCPConcurrent: false,
FindProcessMode: P.FindProcessStrict, FindProcessMode: P.FindProcessStrict,
GlobalUA: "clash.meta/" + C.Version, GlobalUA: "clash.meta/" + C.Version,
ETagSupport: true,
DNS: RawDNS{ DNS: RawDNS{
Enable: false, Enable: false,
IPv6: false, IPv6: false,
@ -690,6 +694,7 @@ func parseGeneral(cfg *RawConfig) (*General, error) {
geodata.SetMmdbUrl(cfg.GeoXUrl.Mmdb) geodata.SetMmdbUrl(cfg.GeoXUrl.Mmdb)
geodata.SetASNUrl(cfg.GeoXUrl.ASN) geodata.SetASNUrl(cfg.GeoXUrl.ASN)
mihomoHttp.SetUA(cfg.GlobalUA) mihomoHttp.SetUA(cfg.GlobalUA)
resource.SetETag(cfg.ETagSupport)
if cfg.KeepAliveIdle != 0 { if cfg.KeepAliveIdle != 0 {
N.KeepAliveIdle = time.Duration(cfg.KeepAliveIdle) * time.Second N.KeepAliveIdle = time.Duration(cfg.KeepAliveIdle) * time.Second
@ -755,6 +760,7 @@ func parseGeneral(cfg *RawConfig) (*General, error) {
FindProcessMode: cfg.FindProcessMode, FindProcessMode: cfg.FindProcessMode,
GlobalClientFingerprint: cfg.GlobalClientFingerprint, GlobalClientFingerprint: cfg.GlobalClientFingerprint,
GlobalUA: cfg.GlobalUA, GlobalUA: cfg.GlobalUA,
ETagSupport: cfg.ETagSupport,
}, nil }, nil
} }

View File

@ -22,6 +22,7 @@ import (
"github.com/metacubex/mihomo/component/profile" "github.com/metacubex/mihomo/component/profile"
"github.com/metacubex/mihomo/component/profile/cachefile" "github.com/metacubex/mihomo/component/profile/cachefile"
"github.com/metacubex/mihomo/component/resolver" "github.com/metacubex/mihomo/component/resolver"
"github.com/metacubex/mihomo/component/resource"
"github.com/metacubex/mihomo/component/sniffer" "github.com/metacubex/mihomo/component/sniffer"
tlsC "github.com/metacubex/mihomo/component/tls" tlsC "github.com/metacubex/mihomo/component/tls"
"github.com/metacubex/mihomo/component/trie" "github.com/metacubex/mihomo/component/trie"
@ -172,6 +173,7 @@ func GetGeneral() *config.General {
Sniffing: tunnel.IsSniffing(), Sniffing: tunnel.IsSniffing(),
GlobalClientFingerprint: tlsC.GetGlobalFingerprint(), GlobalClientFingerprint: tlsC.GetGlobalFingerprint(),
GlobalUA: mihomoHttp.UA(), GlobalUA: mihomoHttp.UA(),
ETagSupport: resource.ETag(),
} }
return general return general