github.com/yaling888/clash@v1.53.0/constant/dns.go (about) 1 package constant 2 3 import ( 4 "encoding/json" 5 "fmt" 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 } 13 14 const ( 15 DNSNormal DNSMode = iota 16 DNSFakeIP 17 DNSMapping 18 ) 19 20 type DNSMode int 21 22 // UnmarshalYAML unserialize EnhancedMode with yaml 23 func (e *DNSMode) UnmarshalYAML(unmarshal func(any) error) error { 24 var tp string 25 if err := unmarshal(&tp); err != nil { 26 return err 27 } 28 mode, exist := DNSModeMapping[tp] 29 if !exist { 30 return fmt.Errorf("invalid mode: %s", tp) 31 } 32 *e = mode 33 return nil 34 } 35 36 // UnmarshalJSON unserialize EnhancedMode with json 37 func (e *DNSMode) UnmarshalJSON(data []byte) error { 38 var tp string 39 if err := json.Unmarshal(data, &tp); err != nil { 40 return err 41 } 42 mode, exist := DNSModeMapping[tp] 43 if !exist { 44 return fmt.Errorf("invalid mode: %s", tp) 45 } 46 *e = mode 47 return nil 48 } 49 50 // MarshalYAML serialize EnhancedMode with yaml 51 func (e DNSMode) MarshalYAML() (any, error) { 52 return e.String(), 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 default: 69 return "unknown" 70 } 71 }