github.com/kelleygo/clashcore@v1.0.2/constant/dns.go (about) 1 package constant 2 3 import ( 4 "encoding/json" 5 "errors" 6 ) 7 8 // DNSModeMapping is a mapping for EnhancedMode enum 9 var DNSModeMapping = map[string]DNSMode{ 10 DNSNormal.String(): DNSNormal, 11 DNSFakeIP.String(): DNSFakeIP, 12 DNSMapping.String(): DNSMapping, 13 } 14 15 const ( 16 DNSNormal DNSMode = iota 17 DNSFakeIP 18 DNSMapping 19 DNSHosts 20 ) 21 22 type DNSMode int 23 24 // UnmarshalYAML unserialize EnhancedMode with yaml 25 func (e *DNSMode) UnmarshalYAML(unmarshal func(any) error) error { 26 var tp string 27 if err := unmarshal(&tp); err != nil { 28 return err 29 } 30 mode, exist := DNSModeMapping[tp] 31 if !exist { 32 return errors.New("invalid mode") 33 } 34 *e = mode 35 return nil 36 } 37 38 // MarshalYAML serialize EnhancedMode with yaml 39 func (e DNSMode) MarshalYAML() (any, error) { 40 return e.String(), nil 41 } 42 43 // UnmarshalJSON unserialize EnhancedMode with json 44 func (e *DNSMode) UnmarshalJSON(data []byte) error { 45 var tp string 46 json.Unmarshal(data, &tp) 47 mode, exist := DNSModeMapping[tp] 48 if !exist { 49 return errors.New("invalid mode") 50 } 51 *e = mode 52 return nil 53 } 54 55 // MarshalJSON serialize EnhancedMode with json 56 func (e DNSMode) MarshalJSON() ([]byte, error) { 57 return json.Marshal(e.String()) 58 } 59 60 func (e DNSMode) String() string { 61 switch e { 62 case DNSNormal: 63 return "normal" 64 case DNSFakeIP: 65 return "fake-ip" 66 case DNSMapping: 67 return "redir-host" 68 case DNSHosts: 69 return "hosts" 70 default: 71 return "unknown" 72 } 73 } 74 75 type DNSPrefer int 76 77 const ( 78 DualStack DNSPrefer = iota 79 IPv4Only 80 IPv6Only 81 IPv4Prefer 82 IPv6Prefer 83 ) 84 85 var dnsPreferMap = map[string]DNSPrefer{ 86 DualStack.String(): DualStack, 87 IPv4Only.String(): IPv4Only, 88 IPv6Only.String(): IPv6Only, 89 IPv4Prefer.String(): IPv4Prefer, 90 IPv6Prefer.String(): IPv6Prefer, 91 } 92 93 func (d DNSPrefer) String() string { 94 switch d { 95 case DualStack: 96 return "dual" 97 case IPv4Only: 98 return "ipv4" 99 case IPv6Only: 100 return "ipv6" 101 case IPv4Prefer: 102 return "ipv4-prefer" 103 case IPv6Prefer: 104 return "ipv6-prefer" 105 default: 106 return "dual" 107 } 108 } 109 110 func NewDNSPrefer(prefer string) DNSPrefer { 111 if p, ok := dnsPreferMap[prefer]; ok { 112 return p 113 } else { 114 return DualStack 115 } 116 } 117 118 type HTTPVersion string 119 120 const ( 121 // HTTPVersion11 is HTTP/1.1. 122 HTTPVersion11 HTTPVersion = "http/1.1" 123 // HTTPVersion2 is HTTP/2. 124 HTTPVersion2 HTTPVersion = "h2" 125 // HTTPVersion3 is HTTP/3. 126 HTTPVersion3 HTTPVersion = "h3" 127 )