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

chore: add Int32Enum for common/atomic

This commit is contained in:
wwqgtxx
2025-06-12 01:23:34 +08:00
parent 082bcec281
commit 85bb40aaf8
8 changed files with 55 additions and 243 deletions

50
common/atomic/enum.go Normal file
View File

@ -0,0 +1,50 @@
package atomic
import (
"encoding/json"
"fmt"
"sync/atomic"
)
type Int32Enum[T ~int32] struct {
value atomic.Int32
}
func (i *Int32Enum[T]) MarshalJSON() ([]byte, error) {
return json.Marshal(i.Load())
}
func (i *Int32Enum[T]) UnmarshalJSON(b []byte) error {
var v T
if err := json.Unmarshal(b, &v); err != nil {
return err
}
i.Store(v)
return nil
}
func (i *Int32Enum[T]) String() string {
return fmt.Sprint(i.Load())
}
func (i *Int32Enum[T]) Store(v T) {
i.value.Store(int32(v))
}
func (i *Int32Enum[T]) Load() T {
return T(i.value.Load())
}
func (i *Int32Enum[T]) Swap(new T) T {
return T(i.value.Swap(int32(new)))
}
func (i *Int32Enum[T]) CompareAndSwap(old, new T) bool {
return i.value.CompareAndSwap(int32(old), int32(new))
}
func NewInt32Enum[T ~int32](v T) *Int32Enum[T] {
a := &Int32Enum[T]{}
a.Store(v)
return a
}

View File

@ -19,7 +19,7 @@ var (
} }
) )
type FindProcessMode int type FindProcessMode int32
// UnmarshalText unserialize FindProcessMode // UnmarshalText unserialize FindProcessMode
func (m *FindProcessMode) UnmarshalText(data []byte) error { func (m *FindProcessMode) UnmarshalText(data []byte) error {

View File

@ -1,7 +1,6 @@
package constant package constant
import ( import (
"encoding/json"
"errors" "errors"
"strings" "strings"
) )
@ -22,44 +21,6 @@ const (
type DNSMode int type DNSMode int
// UnmarshalYAML unserialize EnhancedMode with yaml
func (e *DNSMode) UnmarshalYAML(unmarshal func(any) error) error {
var tp string
if err := unmarshal(&tp); err != nil {
return err
}
mode, exist := DNSModeMapping[strings.ToLower(tp)]
if !exist {
return errors.New("invalid mode")
}
*e = mode
return nil
}
// MarshalYAML serialize EnhancedMode with yaml
func (e DNSMode) MarshalYAML() (any, error) {
return e.String(), nil
}
// UnmarshalJSON unserialize EnhancedMode with json
func (e *DNSMode) UnmarshalJSON(data []byte) error {
var tp string
if err := json.Unmarshal(data, &tp); err != nil {
return err
}
mode, exist := DNSModeMapping[strings.ToLower(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())
}
// UnmarshalText unserialize EnhancedMode // UnmarshalText unserialize EnhancedMode
func (e *DNSMode) UnmarshalText(data []byte) error { func (e *DNSMode) UnmarshalText(data []byte) error {
mode, exist := DNSModeMapping[strings.ToLower(string(data))] mode, exist := DNSModeMapping[strings.ToLower(string(data))]
@ -157,40 +118,6 @@ func (e FilterMode) String() string {
} }
} }
func (e FilterMode) MarshalYAML() (interface{}, error) {
return e.String(), nil
}
func (e *FilterMode) UnmarshalYAML(unmarshal func(interface{}) error) error {
var tp string
if err := unmarshal(&tp); err != nil {
return err
}
mode, exist := FilterModeMapping[strings.ToLower(tp)]
if !exist {
return errors.New("invalid mode")
}
*e = mode
return nil
}
func (e FilterMode) MarshalJSON() ([]byte, error) {
return json.Marshal(e.String())
}
func (e *FilterMode) UnmarshalJSON(data []byte) error {
var tp string
if err := json.Unmarshal(data, &tp); err != nil {
return err
}
mode, exist := FilterModeMapping[strings.ToLower(tp)]
if !exist {
return errors.New("invalid mode")
}
*e = mode
return nil
}
func (e FilterMode) MarshalText() ([]byte, error) { func (e FilterMode) MarshalText() ([]byte, error) {
return []byte(e.String()), nil return []byte(e.String()), nil
} }

View File

@ -1,7 +1,6 @@
package constant package constant
import ( import (
"encoding/json"
"errors" "errors"
"strings" "strings"
) )
@ -20,42 +19,6 @@ const (
type TUNStack int type TUNStack int
// UnmarshalYAML unserialize TUNStack with yaml
func (e *TUNStack) UnmarshalYAML(unmarshal func(any) error) error {
var tp string
if err := unmarshal(&tp); err != nil {
return err
}
mode, exist := StackTypeMapping[strings.ToLower(tp)]
if !exist {
return errors.New("invalid tun stack")
}
*e = mode
return nil
}
// MarshalYAML serialize TUNStack with yaml
func (e TUNStack) MarshalYAML() (any, error) {
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.ToLower(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())
}
// UnmarshalText unserialize TUNStack // UnmarshalText unserialize TUNStack
func (e *TUNStack) UnmarshalText(data []byte) error { func (e *TUNStack) UnmarshalText(data []byte) error {
mode, exist := StackTypeMapping[strings.ToLower(string(data))] mode, exist := StackTypeMapping[strings.ToLower(string(data))]

View File

@ -1,7 +1,6 @@
package log package log
import ( import (
"encoding/json"
"errors" "errors"
"strings" "strings"
) )
@ -25,30 +24,6 @@ const (
type LogLevel int type LogLevel int
// UnmarshalYAML unserialize LogLevel with yaml
func (l *LogLevel) UnmarshalYAML(unmarshal func(any) error) error {
var tp string
unmarshal(&tp)
level, exist := LogLevelMapping[strings.ToLower(tp)]
if !exist {
return errors.New("invalid log-level")
}
*l = level
return nil
}
// UnmarshalJSON unserialize LogLevel with json
func (l *LogLevel) UnmarshalJSON(data []byte) error {
var tp string
json.Unmarshal(data, &tp)
level, exist := LogLevelMapping[strings.ToLower(tp)]
if !exist {
return errors.New("invalid log-level")
}
*l = level
return nil
}
// UnmarshalText unserialize LogLevel // UnmarshalText unserialize LogLevel
func (l *LogLevel) UnmarshalText(data []byte) error { func (l *LogLevel) UnmarshalText(data []byte) error {
level, exist := LogLevelMapping[strings.ToLower(string(data))] level, exist := LogLevelMapping[strings.ToLower(string(data))]
@ -59,16 +34,6 @@ func (l *LogLevel) UnmarshalText(data []byte) error {
return nil return nil
} }
// MarshalYAML serialize LogLevel with yaml
func (l LogLevel) MarshalYAML() (any, error) {
return l.String(), nil
}
// MarshalJSON serialize LogLevel with json
func (l LogLevel) MarshalJSON() ([]byte, error) {
return json.Marshal(l.String())
}
// MarshalText serialize LogLevel // MarshalText serialize LogLevel
func (l LogLevel) MarshalText() ([]byte, error) { func (l LogLevel) MarshalText() ([]byte, error) {
return []byte(l.String()), nil return []byte(l.String()), nil

View File

@ -1,12 +1,11 @@
package tunnel package tunnel
import ( import (
"encoding/json"
"errors" "errors"
"strings" "strings"
) )
type TunnelMode int type TunnelMode int32
// ModeMapping is a mapping for Mode enum // ModeMapping is a mapping for Mode enum
var ModeMapping = map[string]TunnelMode{ var ModeMapping = map[string]TunnelMode{
@ -21,30 +20,6 @@ const (
Direct Direct
) )
// 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
}
// UnmarshalJSON unserialize Mode
func (m *TunnelMode) UnmarshalJSON(data []byte) error {
var tp string
json.Unmarshal(data, &tp)
mode, exist := ModeMapping[strings.ToLower(tp)]
if !exist {
return errors.New("invalid mode")
}
*m = mode
return nil
}
// UnmarshalText unserialize Mode // UnmarshalText unserialize Mode
func (m *TunnelMode) UnmarshalText(data []byte) error { func (m *TunnelMode) UnmarshalText(data []byte) error {
mode, exist := ModeMapping[strings.ToLower(string(data))] mode, exist := ModeMapping[strings.ToLower(string(data))]
@ -55,16 +30,6 @@ func (m *TunnelMode) UnmarshalText(data []byte) error {
return nil return nil
} }
// MarshalYAML serialize TunnelMode with yaml
func (m TunnelMode) MarshalYAML() (any, error) {
return m.String(), nil
}
// MarshalJSON serialize Mode
func (m TunnelMode) MarshalJSON() ([]byte, error) {
return json.Marshal(m.String())
}
// MarshalText serialize Mode // MarshalText serialize Mode
func (m TunnelMode) MarshalText() ([]byte, error) { func (m TunnelMode) MarshalText() ([]byte, error) {
return []byte(m.String()), nil return []byte(m.String()), nil

View File

@ -1,13 +1,11 @@
package tunnel package tunnel
import ( import (
"encoding/json"
"errors" "errors"
"strings" "strings"
"sync/atomic"
) )
type TunnelStatus int type TunnelStatus int32
// StatusMapping is a mapping for Status enum // StatusMapping is a mapping for Status enum
var StatusMapping = map[string]TunnelStatus{ var StatusMapping = map[string]TunnelStatus{
@ -22,30 +20,6 @@ const (
Running Running
) )
// UnmarshalYAML unserialize Status with yaml
func (s *TunnelStatus) UnmarshalYAML(unmarshal func(any) error) error {
var tp string
unmarshal(&tp)
status, exist := StatusMapping[strings.ToLower(tp)]
if !exist {
return errors.New("invalid status")
}
*s = status
return nil
}
// UnmarshalJSON unserialize Status
func (s *TunnelStatus) UnmarshalJSON(data []byte) error {
var tp string
json.Unmarshal(data, &tp)
status, exist := StatusMapping[strings.ToLower(tp)]
if !exist {
return errors.New("invalid status")
}
*s = status
return nil
}
// UnmarshalText unserialize Status // UnmarshalText unserialize Status
func (s *TunnelStatus) UnmarshalText(data []byte) error { func (s *TunnelStatus) UnmarshalText(data []byte) error {
status, exist := StatusMapping[strings.ToLower(string(data))] status, exist := StatusMapping[strings.ToLower(string(data))]
@ -56,16 +30,6 @@ func (s *TunnelStatus) UnmarshalText(data []byte) error {
return nil return nil
} }
// MarshalYAML serialize TunnelMode with yaml
func (s TunnelStatus) MarshalYAML() (any, error) {
return s.String(), nil
}
// MarshalJSON serialize Status
func (s TunnelStatus) MarshalJSON() ([]byte, error) {
return json.Marshal(s.String())
}
// MarshalText serialize Status // MarshalText serialize Status
func (s TunnelStatus) MarshalText() ([]byte, error) { func (s TunnelStatus) MarshalText() ([]byte, error) {
return []byte(s.String()), nil return []byte(s.String()), nil
@ -83,25 +47,3 @@ func (s TunnelStatus) String() string {
return "Unknown" return "Unknown"
} }
} }
type AtomicStatus struct {
value atomic.Int32
}
func (a *AtomicStatus) Store(s TunnelStatus) {
a.value.Store(int32(s))
}
func (a *AtomicStatus) Load() TunnelStatus {
return TunnelStatus(a.value.Load())
}
func (a *AtomicStatus) String() string {
return a.Load().String()
}
func newAtomicStatus(s TunnelStatus) *AtomicStatus {
a := &AtomicStatus{}
a.Store(s)
return a
}

View File

@ -35,7 +35,7 @@ const (
) )
var ( var (
status = newAtomicStatus(Suspend) status = atomic.NewInt32Enum(Suspend)
udpInit sync.Once udpInit sync.Once
udpQueues []chan C.PacketAdapter udpQueues []chan C.PacketAdapter
natTable = nat.New() natTable = nat.New()
@ -59,7 +59,7 @@ var (
// default timeout for UDP session // default timeout for UDP session
udpTimeout = 60 * time.Second udpTimeout = 60 * time.Second
findProcessMode = atomic.NewTypedValue(P.FindProcessStrict) findProcessMode = atomic.NewInt32Enum(P.FindProcessStrict)
fakeIPRange netip.Prefix fakeIPRange netip.Prefix