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