2022-08-22 18:53:47 +08:00
|
|
|
package option
|
|
|
|
|
|
|
|
import (
|
|
|
|
C "github.com/sagernet/sing-box/constant"
|
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
2023-12-01 20:15:11 +08:00
|
|
|
"github.com/sagernet/sing/common/json"
|
2022-08-22 18:53:47 +08:00
|
|
|
)
|
|
|
|
|
2022-08-22 20:20:56 +08:00
|
|
|
type _V2RayTransportOptions struct {
|
2023-12-05 19:09:13 +08:00
|
|
|
Type string `json:"type"`
|
2023-11-03 01:47:25 +08:00
|
|
|
HTTPOptions V2RayHTTPOptions `json:"-"`
|
|
|
|
WebsocketOptions V2RayWebsocketOptions `json:"-"`
|
|
|
|
QUICOptions V2RayQUICOptions `json:"-"`
|
|
|
|
GRPCOptions V2RayGRPCOptions `json:"-"`
|
|
|
|
HTTPUpgradeOptions V2RayHTTPUpgradeOptions `json:"-"`
|
2022-08-22 18:53:47 +08:00
|
|
|
}
|
|
|
|
|
2022-08-22 20:20:56 +08:00
|
|
|
type V2RayTransportOptions _V2RayTransportOptions
|
2022-08-22 18:53:47 +08:00
|
|
|
|
2022-08-22 20:20:56 +08:00
|
|
|
func (o V2RayTransportOptions) MarshalJSON() ([]byte, error) {
|
2022-08-22 18:53:47 +08:00
|
|
|
var v any
|
|
|
|
switch o.Type {
|
2022-08-22 22:19:25 +08:00
|
|
|
case C.V2RayTransportTypeHTTP:
|
|
|
|
v = o.HTTPOptions
|
2022-08-22 20:20:56 +08:00
|
|
|
case C.V2RayTransportTypeWebsocket:
|
|
|
|
v = o.WebsocketOptions
|
2022-08-22 21:20:05 +08:00
|
|
|
case C.V2RayTransportTypeQUIC:
|
|
|
|
v = o.QUICOptions
|
2022-08-22 22:19:25 +08:00
|
|
|
case C.V2RayTransportTypeGRPC:
|
|
|
|
v = o.GRPCOptions
|
2023-11-03 01:47:25 +08:00
|
|
|
case C.V2RayTransportTypeHTTPUpgrade:
|
|
|
|
v = o.HTTPUpgradeOptions
|
2023-12-05 19:09:13 +08:00
|
|
|
case "":
|
|
|
|
return nil, E.New("missing transport type")
|
2022-08-22 18:53:47 +08:00
|
|
|
default:
|
|
|
|
return nil, E.New("unknown transport type: " + o.Type)
|
|
|
|
}
|
2022-08-22 20:20:56 +08:00
|
|
|
return MarshallObjects((_V2RayTransportOptions)(o), v)
|
2022-08-22 18:53:47 +08:00
|
|
|
}
|
|
|
|
|
2022-08-22 20:20:56 +08:00
|
|
|
func (o *V2RayTransportOptions) UnmarshalJSON(bytes []byte) error {
|
|
|
|
err := json.Unmarshal(bytes, (*_V2RayTransportOptions)(o))
|
2022-08-22 18:53:47 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var v any
|
|
|
|
switch o.Type {
|
2022-08-22 22:19:25 +08:00
|
|
|
case C.V2RayTransportTypeHTTP:
|
|
|
|
v = &o.HTTPOptions
|
2022-08-22 21:20:05 +08:00
|
|
|
case C.V2RayTransportTypeWebsocket:
|
|
|
|
v = &o.WebsocketOptions
|
|
|
|
case C.V2RayTransportTypeQUIC:
|
|
|
|
v = &o.QUICOptions
|
2022-08-22 18:53:47 +08:00
|
|
|
case C.V2RayTransportTypeGRPC:
|
|
|
|
v = &o.GRPCOptions
|
2023-11-03 01:47:25 +08:00
|
|
|
case C.V2RayTransportTypeHTTPUpgrade:
|
|
|
|
v = &o.HTTPUpgradeOptions
|
2022-08-22 18:53:47 +08:00
|
|
|
default:
|
|
|
|
return E.New("unknown transport type: " + o.Type)
|
|
|
|
}
|
2022-08-22 22:19:25 +08:00
|
|
|
err = UnmarshallExcluded(bytes, (*_V2RayTransportOptions)(o), v)
|
2022-08-22 18:53:47 +08:00
|
|
|
if err != nil {
|
2023-12-01 20:15:11 +08:00
|
|
|
return err
|
2022-08-22 18:53:47 +08:00
|
|
|
}
|
|
|
|
return nil
|
2022-08-22 22:19:25 +08:00
|
|
|
}
|
2022-08-22 18:53:47 +08:00
|
|
|
|
2022-08-22 22:19:25 +08:00
|
|
|
type V2RayHTTPOptions struct {
|
2023-10-21 12:00:00 +08:00
|
|
|
Host Listable[string] `json:"host,omitempty"`
|
|
|
|
Path string `json:"path,omitempty"`
|
|
|
|
Method string `json:"method,omitempty"`
|
|
|
|
Headers HTTPHeader `json:"headers,omitempty"`
|
|
|
|
IdleTimeout Duration `json:"idle_timeout,omitempty"`
|
|
|
|
PingTimeout Duration `json:"ping_timeout,omitempty"`
|
2022-08-22 18:53:47 +08:00
|
|
|
}
|
2022-08-22 20:20:56 +08:00
|
|
|
|
|
|
|
type V2RayWebsocketOptions struct {
|
2023-10-21 12:00:00 +08:00
|
|
|
Path string `json:"path,omitempty"`
|
|
|
|
Headers HTTPHeader `json:"headers,omitempty"`
|
|
|
|
MaxEarlyData uint32 `json:"max_early_data,omitempty"`
|
|
|
|
EarlyDataHeaderName string `json:"early_data_header_name,omitempty"`
|
2022-08-22 20:20:56 +08:00
|
|
|
}
|
2022-08-22 21:20:05 +08:00
|
|
|
|
|
|
|
type V2RayQUICOptions struct{}
|
2022-08-22 22:19:25 +08:00
|
|
|
|
|
|
|
type V2RayGRPCOptions struct {
|
2023-03-11 15:49:02 +08:00
|
|
|
ServiceName string `json:"service_name,omitempty"`
|
|
|
|
IdleTimeout Duration `json:"idle_timeout,omitempty"`
|
|
|
|
PingTimeout Duration `json:"ping_timeout,omitempty"`
|
|
|
|
PermitWithoutStream bool `json:"permit_without_stream,omitempty"`
|
|
|
|
ForceLite bool `json:"-"` // for test
|
2022-08-22 22:19:25 +08:00
|
|
|
}
|
2023-11-03 01:47:25 +08:00
|
|
|
|
|
|
|
type V2RayHTTPUpgradeOptions struct {
|
|
|
|
Host string `json:"host,omitempty"`
|
|
|
|
Path string `json:"path,omitempty"`
|
|
|
|
Headers HTTPHeader `json:"headers,omitempty"`
|
|
|
|
}
|