2018-11-21 13:47:46 +08:00
|
|
|
package tunnel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2020-06-01 00:32:37 +08:00
|
|
|
"strings"
|
2018-11-21 13:47:46 +08:00
|
|
|
)
|
|
|
|
|
2020-02-15 21:42:46 +08:00
|
|
|
type TunnelMode int
|
2018-11-21 13:47:46 +08:00
|
|
|
|
2021-10-10 23:44:09 +08:00
|
|
|
// ModeMapping is a mapping for Mode enum
|
|
|
|
var ModeMapping = map[string]TunnelMode{
|
|
|
|
Global.String(): Global,
|
|
|
|
Rule.String(): Rule,
|
|
|
|
Direct.String(): Direct,
|
|
|
|
}
|
2018-11-21 13:47:46 +08:00
|
|
|
|
|
|
|
const (
|
2020-02-15 21:42:46 +08:00
|
|
|
Global TunnelMode = iota
|
2018-11-21 13:47:46 +08:00
|
|
|
Rule
|
|
|
|
Direct
|
|
|
|
)
|
|
|
|
|
2024-09-19 18:06:36 +08:00
|
|
|
// UnmarshalYAML unserialize Mode with yaml
|
|
|
|
func (m *TunnelMode) UnmarshalYAML(unmarshal func(any) error) error {
|
|
|
|
var tp string
|
|
|
|
unmarshal(&tp)
|
|
|
|
mode, exist := ModeMapping[strings.ToLower(tp)]
|
|
|
|
if !exist {
|
|
|
|
return errors.New("invalid mode")
|
|
|
|
}
|
|
|
|
*m = mode
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-21 13:47:46 +08:00
|
|
|
// UnmarshalJSON unserialize Mode
|
2020-02-15 21:42:46 +08:00
|
|
|
func (m *TunnelMode) UnmarshalJSON(data []byte) error {
|
2018-11-21 13:47:46 +08:00
|
|
|
var tp string
|
2018-11-28 10:38:30 +08:00
|
|
|
json.Unmarshal(data, &tp)
|
2020-06-01 00:32:37 +08:00
|
|
|
mode, exist := ModeMapping[strings.ToLower(tp)]
|
2018-11-21 13:47:46 +08:00
|
|
|
if !exist {
|
|
|
|
return errors.New("invalid mode")
|
|
|
|
}
|
|
|
|
*m = mode
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-19 18:06:36 +08:00
|
|
|
// UnmarshalText unserialize Mode
|
|
|
|
func (m *TunnelMode) UnmarshalText(data []byte) error {
|
|
|
|
mode, exist := ModeMapping[strings.ToLower(string(data))]
|
2018-12-05 21:13:29 +08:00
|
|
|
if !exist {
|
|
|
|
return errors.New("invalid mode")
|
|
|
|
}
|
|
|
|
*m = mode
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-19 18:06:36 +08:00
|
|
|
// MarshalYAML serialize TunnelMode with yaml
|
|
|
|
func (m TunnelMode) MarshalYAML() (any, error) {
|
|
|
|
return m.String(), nil
|
|
|
|
}
|
|
|
|
|
2018-11-21 13:47:46 +08:00
|
|
|
// MarshalJSON serialize Mode
|
2020-02-15 21:42:46 +08:00
|
|
|
func (m TunnelMode) MarshalJSON() ([]byte, error) {
|
2018-11-21 13:47:46 +08:00
|
|
|
return json.Marshal(m.String())
|
|
|
|
}
|
|
|
|
|
2024-09-19 18:06:36 +08:00
|
|
|
// MarshalText serialize Mode
|
|
|
|
func (m TunnelMode) MarshalText() ([]byte, error) {
|
|
|
|
return []byte(m.String()), nil
|
2020-03-15 19:40:39 +08:00
|
|
|
}
|
|
|
|
|
2020-02-15 21:42:46 +08:00
|
|
|
func (m TunnelMode) String() string {
|
2018-11-21 13:47:46 +08:00
|
|
|
switch m {
|
|
|
|
case Global:
|
2020-06-01 00:32:37 +08:00
|
|
|
return "global"
|
2018-11-21 13:47:46 +08:00
|
|
|
case Rule:
|
2020-06-01 00:32:37 +08:00
|
|
|
return "rule"
|
2018-11-21 13:47:46 +08:00
|
|
|
case Direct:
|
2020-06-01 00:32:37 +08:00
|
|
|
return "direct"
|
2018-11-21 13:47:46 +08:00
|
|
|
default:
|
2019-07-29 10:12:10 +08:00
|
|
|
return "Unknown"
|
2018-11-21 13:47:46 +08:00
|
|
|
}
|
|
|
|
}
|