github.com/sagernet/sing-box@v1.9.0-rc.20/option/v2ray_transport.go (about)

     1  package option
     2  
     3  import (
     4  	C "github.com/sagernet/sing-box/constant"
     5  	E "github.com/sagernet/sing/common/exceptions"
     6  	"github.com/sagernet/sing/common/json"
     7  )
     8  
     9  type _V2RayTransportOptions struct {
    10  	Type               string                  `json:"type"`
    11  	HTTPOptions        V2RayHTTPOptions        `json:"-"`
    12  	WebsocketOptions   V2RayWebsocketOptions   `json:"-"`
    13  	QUICOptions        V2RayQUICOptions        `json:"-"`
    14  	GRPCOptions        V2RayGRPCOptions        `json:"-"`
    15  	HTTPUpgradeOptions V2RayHTTPUpgradeOptions `json:"-"`
    16  }
    17  
    18  type V2RayTransportOptions _V2RayTransportOptions
    19  
    20  func (o V2RayTransportOptions) MarshalJSON() ([]byte, error) {
    21  	var v any
    22  	switch o.Type {
    23  	case C.V2RayTransportTypeHTTP:
    24  		v = o.HTTPOptions
    25  	case C.V2RayTransportTypeWebsocket:
    26  		v = o.WebsocketOptions
    27  	case C.V2RayTransportTypeQUIC:
    28  		v = o.QUICOptions
    29  	case C.V2RayTransportTypeGRPC:
    30  		v = o.GRPCOptions
    31  	case C.V2RayTransportTypeHTTPUpgrade:
    32  		v = o.HTTPUpgradeOptions
    33  	case "":
    34  		return nil, E.New("missing transport type")
    35  	default:
    36  		return nil, E.New("unknown transport type: " + o.Type)
    37  	}
    38  	return MarshallObjects((_V2RayTransportOptions)(o), v)
    39  }
    40  
    41  func (o *V2RayTransportOptions) UnmarshalJSON(bytes []byte) error {
    42  	err := json.Unmarshal(bytes, (*_V2RayTransportOptions)(o))
    43  	if err != nil {
    44  		return err
    45  	}
    46  	var v any
    47  	switch o.Type {
    48  	case C.V2RayTransportTypeHTTP:
    49  		v = &o.HTTPOptions
    50  	case C.V2RayTransportTypeWebsocket:
    51  		v = &o.WebsocketOptions
    52  	case C.V2RayTransportTypeQUIC:
    53  		v = &o.QUICOptions
    54  	case C.V2RayTransportTypeGRPC:
    55  		v = &o.GRPCOptions
    56  	case C.V2RayTransportTypeHTTPUpgrade:
    57  		v = &o.HTTPUpgradeOptions
    58  	default:
    59  		return E.New("unknown transport type: " + o.Type)
    60  	}
    61  	err = UnmarshallExcluded(bytes, (*_V2RayTransportOptions)(o), v)
    62  	if err != nil {
    63  		return err
    64  	}
    65  	return nil
    66  }
    67  
    68  type V2RayHTTPOptions struct {
    69  	Host        Listable[string] `json:"host,omitempty"`
    70  	Path        string           `json:"path,omitempty"`
    71  	Method      string           `json:"method,omitempty"`
    72  	Headers     HTTPHeader       `json:"headers,omitempty"`
    73  	IdleTimeout Duration         `json:"idle_timeout,omitempty"`
    74  	PingTimeout Duration         `json:"ping_timeout,omitempty"`
    75  }
    76  
    77  type V2RayWebsocketOptions struct {
    78  	Path                string     `json:"path,omitempty"`
    79  	Headers             HTTPHeader `json:"headers,omitempty"`
    80  	MaxEarlyData        uint32     `json:"max_early_data,omitempty"`
    81  	EarlyDataHeaderName string     `json:"early_data_header_name,omitempty"`
    82  }
    83  
    84  type V2RayQUICOptions struct{}
    85  
    86  type V2RayGRPCOptions struct {
    87  	ServiceName         string   `json:"service_name,omitempty"`
    88  	IdleTimeout         Duration `json:"idle_timeout,omitempty"`
    89  	PingTimeout         Duration `json:"ping_timeout,omitempty"`
    90  	PermitWithoutStream bool     `json:"permit_without_stream,omitempty"`
    91  	ForceLite           bool     `json:"-"` // for test
    92  }
    93  
    94  type V2RayHTTPUpgradeOptions struct {
    95  	Host    string     `json:"host,omitempty"`
    96  	Path    string     `json:"path,omitempty"`
    97  	Headers HTTPHeader `json:"headers,omitempty"`
    98  }