sing-box/option/types.go

190 lines
3.9 KiB
Go
Raw Normal View History

package option
import (
"net/netip"
"strings"
2022-07-08 12:58:43 +08:00
"time"
2022-07-06 15:01:09 +08:00
C "github.com/sagernet/sing-box/constant"
2022-07-11 18:44:59 +08:00
"github.com/sagernet/sing-dns"
2022-07-08 23:03:57 +08:00
E "github.com/sagernet/sing/common/exceptions"
2022-07-06 15:01:09 +08:00
"github.com/goccy/go-json"
)
type ListenAddress netip.Addr
func (a ListenAddress) MarshalJSON() ([]byte, error) {
addr := netip.Addr(a)
if !addr.IsValid() {
return json.Marshal("")
}
return json.Marshal(addr.String())
}
func (a *ListenAddress) UnmarshalJSON(content []byte) error {
var value string
err := json.Unmarshal(content, &value)
if err != nil {
return err
}
addr, err := netip.ParseAddr(value)
if err != nil {
return err
}
*a = ListenAddress(addr)
return nil
}
type NetworkList string
func (v *NetworkList) UnmarshalJSON(content []byte) error {
var networkList []string
err := json.Unmarshal(content, &networkList)
if err != nil {
var networkItem string
err = json.Unmarshal(content, &networkItem)
if err != nil {
return err
}
networkList = []string{networkItem}
}
for _, networkName := range networkList {
switch networkName {
case C.NetworkTCP, C.NetworkUDP:
break
default:
return E.New("unknown network: " + networkName)
}
}
*v = NetworkList(strings.Join(networkList, "\n"))
return nil
}
func (v NetworkList) Build() []string {
if v == "" {
return []string{C.NetworkTCP, C.NetworkUDP}
}
return strings.Split(string(v), "\n")
}
type Listable[T comparable] []T
func (l Listable[T]) MarshalJSON() ([]byte, error) {
arrayList := []T(l)
if len(arrayList) == 1 {
return json.Marshal(arrayList[0])
}
return json.Marshal(arrayList)
}
func (l *Listable[T]) UnmarshalJSON(content []byte) error {
err := json.Unmarshal(content, (*[]T)(l))
if err == nil {
return nil
}
var singleItem T
err = json.Unmarshal(content, &singleItem)
if err != nil {
return err
}
*l = []T{singleItem}
return nil
}
2022-07-06 23:11:48 +08:00
2022-07-11 18:44:59 +08:00
type DomainStrategy dns.DomainStrategy
2022-07-06 23:11:48 +08:00
func (s DomainStrategy) MarshalJSON() ([]byte, error) {
var value string
2022-07-11 18:44:59 +08:00
switch dns.DomainStrategy(s) {
case dns.DomainStrategyAsIS:
2022-07-07 21:47:21 +08:00
value = ""
// value = "AsIS"
2022-07-11 18:44:59 +08:00
case dns.DomainStrategyPreferIPv4:
2022-07-07 23:36:32 +08:00
value = "prefer_ipv4"
2022-07-11 18:44:59 +08:00
case dns.DomainStrategyPreferIPv6:
2022-07-07 23:36:32 +08:00
value = "prefer_ipv6"
2022-07-11 18:44:59 +08:00
case dns.DomainStrategyUseIPv4:
2022-07-07 23:36:32 +08:00
value = "ipv4_only"
2022-07-11 18:44:59 +08:00
case dns.DomainStrategyUseIPv6:
2022-07-07 23:36:32 +08:00
value = "ipv6_only"
2022-07-06 23:11:48 +08:00
default:
return nil, E.New("unknown domain strategy: ", s)
}
return json.Marshal(value)
}
func (s *DomainStrategy) UnmarshalJSON(bytes []byte) error {
var value string
err := json.Unmarshal(bytes, &value)
if err != nil {
return err
}
switch value {
2022-07-07 21:47:21 +08:00
case "", "AsIS":
2022-07-11 18:44:59 +08:00
*s = DomainStrategy(dns.DomainStrategyAsIS)
2022-07-07 23:36:32 +08:00
case "prefer_ipv4":
2022-07-11 18:44:59 +08:00
*s = DomainStrategy(dns.DomainStrategyPreferIPv4)
2022-07-07 23:36:32 +08:00
case "prefer_ipv6":
2022-07-11 18:44:59 +08:00
*s = DomainStrategy(dns.DomainStrategyPreferIPv6)
2022-07-07 23:36:32 +08:00
case "ipv4_only":
2022-07-11 18:44:59 +08:00
*s = DomainStrategy(dns.DomainStrategyUseIPv4)
2022-07-07 23:36:32 +08:00
case "ipv6_only":
2022-07-11 18:44:59 +08:00
*s = DomainStrategy(dns.DomainStrategyUseIPv6)
2022-07-06 23:11:48 +08:00
default:
return E.New("unknown domain strategy: ", value)
}
return nil
}
2022-07-08 12:58:43 +08:00
type Duration time.Duration
func (d Duration) MarshalJSON() ([]byte, error) {
return json.Marshal((time.Duration)(d).String())
}
func (d *Duration) UnmarshalJSON(bytes []byte) error {
var value string
err := json.Unmarshal(bytes, &value)
if err != nil {
return err
}
duration, err := time.ParseDuration(value)
if err != nil {
return err
}
*d = Duration(duration)
return nil
}
2022-07-09 19:18:37 +08:00
type ListenPrefix netip.Prefix
func (p ListenPrefix) MarshalJSON() ([]byte, error) {
prefix := netip.Prefix(p)
if !prefix.IsValid() {
2022-07-10 14:22:28 +08:00
return json.Marshal(nil)
2022-07-09 19:18:37 +08:00
}
return json.Marshal(prefix.String())
}
func (p *ListenPrefix) UnmarshalJSON(bytes []byte) error {
var value string
err := json.Unmarshal(bytes, &value)
if err != nil {
return err
}
prefix, err := netip.ParsePrefix(value)
if err != nil {
return err
}
*p = ListenPrefix(prefix)
return nil
}
2022-07-10 14:22:28 +08:00
func (p *ListenPrefix) Build() netip.Prefix {
if p == nil {
return netip.Prefix{}
}
return netip.Prefix(*p)
}