github.com/inazumav/sing-box@v0.0.0-20230926072359-ab51429a14f1/option/v2ray_transport.go (about)

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