2021-10-18 13:08:27 +00:00
|
|
|
package constant
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DNSModeMapping is a mapping for EnhancedMode enum
|
|
|
|
var DNSModeMapping = map[string]DNSMode{
|
|
|
|
DNSNormal.String(): DNSNormal,
|
|
|
|
DNSFakeIP.String(): DNSFakeIP,
|
|
|
|
DNSMapping.String(): DNSMapping,
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
DNSNormal DNSMode = iota
|
|
|
|
DNSFakeIP
|
|
|
|
DNSMapping
|
2022-10-29 01:02:38 +00:00
|
|
|
DNSHosts
|
2021-10-18 13:08:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type DNSMode int
|
|
|
|
|
|
|
|
// UnmarshalYAML unserialize EnhancedMode with yaml
|
2022-03-16 04:10:13 +00:00
|
|
|
func (e *DNSMode) UnmarshalYAML(unmarshal func(any) error) error {
|
2021-10-18 13:08:27 +00:00
|
|
|
var tp string
|
|
|
|
if err := unmarshal(&tp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
mode, exist := DNSModeMapping[tp]
|
|
|
|
if !exist {
|
|
|
|
return errors.New("invalid mode")
|
|
|
|
}
|
|
|
|
*e = mode
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalYAML serialize EnhancedMode with yaml
|
2022-03-16 04:10:13 +00:00
|
|
|
func (e DNSMode) MarshalYAML() (any, error) {
|
2021-10-18 13:08:27 +00:00
|
|
|
return e.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON unserialize EnhancedMode with json
|
|
|
|
func (e *DNSMode) UnmarshalJSON(data []byte) error {
|
|
|
|
var tp string
|
|
|
|
json.Unmarshal(data, &tp)
|
|
|
|
mode, exist := DNSModeMapping[tp]
|
|
|
|
if !exist {
|
|
|
|
return errors.New("invalid mode")
|
|
|
|
}
|
|
|
|
*e = mode
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON serialize EnhancedMode with json
|
|
|
|
func (e DNSMode) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(e.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e DNSMode) String() string {
|
|
|
|
switch e {
|
|
|
|
case DNSNormal:
|
|
|
|
return "normal"
|
|
|
|
case DNSFakeIP:
|
|
|
|
return "fake-ip"
|
|
|
|
case DNSMapping:
|
|
|
|
return "redir-host"
|
2022-10-29 01:02:38 +00:00
|
|
|
case DNSHosts:
|
|
|
|
return "hosts"
|
2021-10-18 13:08:27 +00:00
|
|
|
default:
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
}
|
2022-08-28 05:41:19 +00:00
|
|
|
|
|
|
|
type DNSPrefer int
|
|
|
|
|
|
|
|
const (
|
|
|
|
DualStack DNSPrefer = iota
|
|
|
|
IPv4Only
|
|
|
|
IPv6Only
|
|
|
|
IPv4Prefer
|
|
|
|
IPv6Prefer
|
|
|
|
)
|
|
|
|
|
|
|
|
var dnsPreferMap = map[string]DNSPrefer{
|
|
|
|
DualStack.String(): DualStack,
|
|
|
|
IPv4Only.String(): IPv4Only,
|
|
|
|
IPv6Only.String(): IPv6Only,
|
|
|
|
IPv4Prefer.String(): IPv4Prefer,
|
|
|
|
IPv6Prefer.String(): IPv6Prefer,
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d DNSPrefer) String() string {
|
|
|
|
switch d {
|
|
|
|
case DualStack:
|
|
|
|
return "dual"
|
|
|
|
case IPv4Only:
|
|
|
|
return "ipv4"
|
|
|
|
case IPv6Only:
|
|
|
|
return "ipv6"
|
|
|
|
case IPv4Prefer:
|
|
|
|
return "ipv4-prefer"
|
|
|
|
case IPv6Prefer:
|
|
|
|
return "ipv6-prefer"
|
|
|
|
default:
|
|
|
|
return "dual"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDNSPrefer(prefer string) DNSPrefer {
|
|
|
|
if p, ok := dnsPreferMap[prefer]; ok {
|
|
|
|
return p
|
|
|
|
} else {
|
|
|
|
return DualStack
|
|
|
|
}
|
|
|
|
}
|
2022-11-12 03:14:51 +00:00
|
|
|
|
|
|
|
type HTTPVersion string
|
|
|
|
|
|
|
|
const (
|
|
|
|
// HTTPVersion11 is HTTP/1.1.
|
|
|
|
HTTPVersion11 HTTPVersion = "http/1.1"
|
|
|
|
// HTTPVersion2 is HTTP/2.
|
|
|
|
HTTPVersion2 HTTPVersion = "h2"
|
|
|
|
// HTTPVersion3 is HTTP/3.
|
|
|
|
HTTPVersion3 HTTPVersion = "h3"
|
2023-02-17 08:31:37 +00:00
|
|
|
)
|