clash/constant/tun.go

67 lines
1.3 KiB
Go
Raw Normal View History

2022-03-08 21:08:35 +00:00
package constant
import (
"encoding/json"
"errors"
"strings"
)
var StackTypeMapping = map[string]TUNStack{
strings.ToUpper(TunGvisor.String()): TunGvisor,
strings.ToUpper(TunSystem.String()): TunSystem,
}
const (
TunGvisor TUNStack = iota
TunSystem
)
type TUNStack int
// UnmarshalYAML unserialize TUNStack with yaml
2022-03-16 14:00:20 +00:00
func (e *TUNStack) UnmarshalYAML(unmarshal func(any) error) error {
2022-03-08 21:08:35 +00:00
var tp string
if err := unmarshal(&tp); err != nil {
return err
}
mode, exist := StackTypeMapping[strings.ToUpper(tp)]
if !exist {
return errors.New("invalid tun stack")
}
*e = mode
return nil
}
// MarshalYAML serialize TUNStack with yaml
2022-03-16 14:00:20 +00:00
func (e TUNStack) MarshalYAML() (any, error) {
2022-03-08 21:08:35 +00:00
return e.String(), nil
}
// UnmarshalJSON unserialize TUNStack with json
func (e *TUNStack) UnmarshalJSON(data []byte) error {
var tp string
json.Unmarshal(data, &tp)
mode, exist := StackTypeMapping[strings.ToUpper(tp)]
if !exist {
return errors.New("invalid tun stack")
}
*e = mode
return nil
}
// MarshalJSON serialize TUNStack with json
func (e TUNStack) MarshalJSON() ([]byte, error) {
return json.Marshal(e.String())
}
func (e TUNStack) String() string {
switch e {
case TunGvisor:
return "gVisor"
case TunSystem:
return "System"
default:
return "unknown"
}
}