1
0
mirror of https://github.com/MetaCubeX/mihomo.git synced 2025-09-19 20:15:59 +08:00
Files
mihomo/tunnel/mode.go

50 lines
805 B
Go
Raw Normal View History

2018-11-21 13:47:46 +08:00
package tunnel
import (
"errors"
"strings"
2018-11-21 13:47:46 +08:00
)
2025-06-12 01:23:34 +08:00
type TunnelMode int32
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 (
Global TunnelMode = iota
2018-11-21 13:47:46 +08:00
Rule
Direct
)
// 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
}
// MarshalText serialize Mode
func (m TunnelMode) MarshalText() ([]byte, error) {
return []byte(m.String()), nil
}
func (m TunnelMode) String() string {
2018-11-21 13:47:46 +08:00
switch m {
case Global:
return "global"
2018-11-21 13:47:46 +08:00
case Rule:
return "rule"
2018-11-21 13:47:46 +08:00
case Direct:
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
}
}