sing-box/option/types.go

285 lines
6.1 KiB
Go
Raw Normal View History

package option
import (
2023-10-21 04:00:00 +00:00
"net/http"
"net/netip"
"strings"
2022-07-08 04:58:43 +00:00
"time"
2022-07-11 10:44:59 +00:00
"github.com/sagernet/sing-dns"
2022-07-08 15:03:57 +00:00
E "github.com/sagernet/sing/common/exceptions"
2023-02-08 08:18:40 +00:00
F "github.com/sagernet/sing/common/format"
"github.com/sagernet/sing/common/json"
2022-07-29 16:29:22 +00:00
N "github.com/sagernet/sing/common/network"
2023-02-08 08:18:40 +00:00
mDNS "github.com/miekg/dns"
)
type ListenAddress netip.Addr
2023-03-19 12:46:22 +00:00
func NewListenAddress(addr netip.Addr) *ListenAddress {
address := ListenAddress(addr)
return &address
}
func (a ListenAddress) MarshalJSON() ([]byte, error) {
addr := netip.Addr(a)
if !addr.IsValid() {
2022-08-25 06:49:49 +00:00
return nil, nil
}
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
}
2023-03-19 12:46:22 +00:00
func (a *ListenAddress) Build() netip.Addr {
if a == nil {
return netip.AddrFrom4([4]byte{127, 0, 0, 1})
}
return (netip.Addr)(*a)
2022-08-25 06:49:49 +00:00
}
type AddrPrefix netip.Prefix
func (a AddrPrefix) MarshalJSON() ([]byte, error) {
prefix := netip.Prefix(a)
if prefix.Bits() == prefix.Addr().BitLen() {
return json.Marshal(prefix.Addr().String())
} else {
return json.Marshal(prefix.String())
}
}
func (a *AddrPrefix) UnmarshalJSON(content []byte) error {
var value string
err := json.Unmarshal(content, &value)
if err != nil {
return err
}
prefix, prefixErr := netip.ParsePrefix(value)
if prefixErr == nil {
*a = AddrPrefix(prefix)
return nil
}
addr, addrErr := netip.ParseAddr(value)
if addrErr == nil {
*a = AddrPrefix(netip.PrefixFrom(addr, addr.BitLen()))
return nil
}
return prefixErr
}
2024-10-21 15:38:34 +00:00
func (a *AddrPrefix) Build() netip.Prefix {
if a == nil {
return netip.Prefix{}
}
return netip.Prefix(*a)
}
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 {
2022-07-29 16:29:22 +00:00
case N.NetworkTCP, N.NetworkUDP:
break
default:
return E.New("unknown network: " + networkName)
}
}
*v = NetworkList(strings.Join(networkList, "\n"))
return nil
}
func (v NetworkList) Build() []string {
if v == "" {
2022-07-29 16:29:22 +00:00
return []string{N.NetworkTCP, N.NetworkUDP}
}
return strings.Split(string(v), "\n")
}
2023-12-04 07:24:25 +00:00
type Listable[T any] []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 {
2024-07-22 04:10:22 +00:00
err := json.UnmarshalDisallowUnknownFields(content, (*[]T)(l))
if err == nil {
return nil
}
var singleItem T
2024-07-22 04:10:22 +00:00
newError := json.UnmarshalDisallowUnknownFields(content, &singleItem)
2023-07-23 06:42:19 +00:00
if newError != nil {
return E.Errors(err, newError)
}
*l = []T{singleItem}
return nil
}
2022-07-06 15:11:48 +00:00
2022-07-11 10:44:59 +00:00
type DomainStrategy dns.DomainStrategy
2022-07-06 15:11:48 +00:00
2024-10-21 15:38:34 +00:00
func (s DomainStrategy) String() string {
switch dns.DomainStrategy(s) {
case dns.DomainStrategyAsIS:
return ""
case dns.DomainStrategyPreferIPv4:
return "prefer_ipv4"
case dns.DomainStrategyPreferIPv6:
return "prefer_ipv6"
case dns.DomainStrategyUseIPv4:
return "ipv4_only"
case dns.DomainStrategyUseIPv6:
return "ipv6_only"
default:
panic(E.New("unknown domain strategy: ", s))
}
}
2022-07-06 15:11:48 +00:00
func (s DomainStrategy) MarshalJSON() ([]byte, error) {
var value string
2022-07-11 10:44:59 +00:00
switch dns.DomainStrategy(s) {
case dns.DomainStrategyAsIS:
2022-07-07 13:47:21 +00:00
value = ""
2024-10-21 15:38:34 +00:00
// value = "as_is"
2022-07-11 10:44:59 +00:00
case dns.DomainStrategyPreferIPv4:
2022-07-07 15:36:32 +00:00
value = "prefer_ipv4"
2022-07-11 10:44:59 +00:00
case dns.DomainStrategyPreferIPv6:
2022-07-07 15:36:32 +00:00
value = "prefer_ipv6"
2022-07-11 10:44:59 +00:00
case dns.DomainStrategyUseIPv4:
2022-07-07 15:36:32 +00:00
value = "ipv4_only"
2022-07-11 10:44:59 +00:00
case dns.DomainStrategyUseIPv6:
2022-07-07 15:36:32 +00:00
value = "ipv6_only"
2022-07-06 15:11:48 +00: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 {
case "", "as_is":
2022-07-11 10:44:59 +00:00
*s = DomainStrategy(dns.DomainStrategyAsIS)
2022-07-07 15:36:32 +00:00
case "prefer_ipv4":
2022-07-11 10:44:59 +00:00
*s = DomainStrategy(dns.DomainStrategyPreferIPv4)
2022-07-07 15:36:32 +00:00
case "prefer_ipv6":
2022-07-11 10:44:59 +00:00
*s = DomainStrategy(dns.DomainStrategyPreferIPv6)
2022-07-07 15:36:32 +00:00
case "ipv4_only":
2022-07-11 10:44:59 +00:00
*s = DomainStrategy(dns.DomainStrategyUseIPv4)
2022-07-07 15:36:32 +00:00
case "ipv6_only":
2022-07-11 10:44:59 +00:00
*s = DomainStrategy(dns.DomainStrategyUseIPv6)
2022-07-06 15:11:48 +00:00
default:
return E.New("unknown domain strategy: ", value)
}
return nil
}
2022-07-08 04:58:43 +00: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
}
2023-12-01 05:24:12 +00:00
duration, err := ParseDuration(value)
2022-07-08 04:58:43 +00:00
if err != nil {
return err
}
*d = Duration(duration)
return nil
}
2022-07-09 11:18:37 +00:00
2023-02-08 08:18:40 +00:00
type DNSQueryType uint16
2023-12-01 05:24:12 +00:00
func (t DNSQueryType) String() string {
typeName, loaded := mDNS.TypeToString[uint16(t)]
if loaded {
return typeName
}
return F.ToString(uint16(t))
}
2023-02-08 08:18:40 +00:00
func (t DNSQueryType) MarshalJSON() ([]byte, error) {
typeName, loaded := mDNS.TypeToString[uint16(t)]
if loaded {
return json.Marshal(typeName)
}
return json.Marshal(uint16(t))
}
func (t *DNSQueryType) UnmarshalJSON(bytes []byte) error {
var valueNumber uint16
err := json.Unmarshal(bytes, &valueNumber)
if err == nil {
*t = DNSQueryType(valueNumber)
return nil
}
var valueString string
err = json.Unmarshal(bytes, &valueString)
if err == nil {
queryType, loaded := mDNS.StringToType[valueString]
if loaded {
*t = DNSQueryType(queryType)
return nil
}
}
return E.New("unknown DNS query type: ", string(bytes))
}
func DNSQueryTypeToString(queryType uint16) string {
typeName, loaded := mDNS.TypeToString[queryType]
if loaded {
return typeName
}
return F.ToString(queryType)
}
2023-10-21 04:00:00 +00:00
type HTTPHeader map[string]Listable[string]
func (h HTTPHeader) Build() http.Header {
header := make(http.Header)
for name, values := range h {
for _, value := range values {
header.Add(name, value)
}
}
return header
}