Skip to content

Commit

Permalink
Add gRPC Transport support (#356)
Browse files Browse the repository at this point in the history
Co-authored-by: JimhHan <50871214+JimhHan@users.noreply.github.com>
  • Loading branch information
RPRX and JimhHan authored Mar 14, 2021
1 parent 60b0687 commit a0a32ee
Show file tree
Hide file tree
Showing 23 changed files with 1,564 additions and 4 deletions.
16 changes: 16 additions & 0 deletions common/buf/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ func New() *Buffer {
}
}

func NewExisted(b []byte) *Buffer {
if cap(b) < Size {
panic("Invalid buffer")
}

oLen := len(b)
if oLen < Size {
b = append(b, make([]byte, Size-oLen)...)
}

return &Buffer{
v: b,
end: int32(oLen),
}
}

// StackNew creates a new Buffer object on stack.
// This method is for buffers that is released in the same function.
func StackNew() Buffer {
Expand Down
16 changes: 16 additions & 0 deletions infra/conf/grpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package conf

import (
"github.com/golang/protobuf/proto"

"github.com/xtls/xray-core/transport/internet/grpc"
)

type GRPCConfig struct {
ServiceName string `json:"serviceName"`
MultiMode bool `json:"multiMode"`
}

func (g GRPCConfig) Build() (proto.Message, error) {
return &grpc.Config{ServiceName: g.ServiceName, MultiMode: g.MultiMode}, nil
}
16 changes: 16 additions & 0 deletions infra/conf/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ type TransportConfig struct {
HTTPConfig *HTTPConfig `json:"httpSettings"`
DSConfig *DomainSocketConfig `json:"dsSettings"`
QUICConfig *QUICConfig `json:"quicSettings"`
GRPCConfig *GRPCConfig `json:"grpcSettings"`
GUNConfig *GRPCConfig `json:"gunSettings"`
}

// Build implements Buildable.
Expand Down Expand Up @@ -85,5 +87,19 @@ func (c *TransportConfig) Build() (*global.Config, error) {
})
}

if c.GRPCConfig == nil {
c.GRPCConfig = c.GUNConfig
}
if c.GRPCConfig != nil {
gs, err := c.GRPCConfig.Build()
if err != nil {
return nil, newError("Failed to build gRPC config.").Base(err)
}
config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
ProtocolName: "grpc",
Settings: serial.ToTypedMessage(gs),
})
}

return config, nil
}
17 changes: 17 additions & 0 deletions infra/conf/transport_internet.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,8 @@ func (p TransportProtocol) Build() (string, error) {
return "domainsocket", nil
case "quic":
return "quic", nil
case "grpc", "gun":
return "grpc", nil
default:
return "", newError("Config: unknown transport protocol: ", p)
}
Expand Down Expand Up @@ -534,6 +536,8 @@ type StreamConfig struct {
DSSettings *DomainSocketConfig `json:"dsSettings"`
QUICSettings *QUICConfig `json:"quicSettings"`
SocketSettings *SocketConfig `json:"sockopt"`
GRPCConfig *GRPCConfig `json:"grpcSettings"`
GUNConfig *GRPCConfig `json:"gunSettings"`
}

// Build implements Buildable.
Expand Down Expand Up @@ -643,6 +647,19 @@ func (c *StreamConfig) Build() (*internet.StreamConfig, error) {
Settings: serial.ToTypedMessage(qs),
})
}
if c.GRPCConfig == nil {
c.GRPCConfig = c.GUNConfig
}
if c.GRPCConfig != nil {
gs, err := c.GRPCConfig.Build()
if err != nil {
return nil, newError("Failed to build gRPC config.").Base(err)
}
config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
ProtocolName: "grpc",
Settings: serial.ToTypedMessage(gs),
})
}
if c.SocketSettings != nil {
ss, err := c.SocketSettings.Build()
if err != nil {
Expand Down
30 changes: 30 additions & 0 deletions infra/conf/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
. "github.com/xtls/xray-core/infra/conf"
"github.com/xtls/xray-core/transport/global"
"github.com/xtls/xray-core/transport/internet"
"github.com/xtls/xray-core/transport/internet/grpc"
"github.com/xtls/xray-core/transport/internet/headers/http"
"github.com/xtls/xray-core/transport/internet/headers/noop"
"github.com/xtls/xray-core/transport/internet/headers/tls"
Expand Down Expand Up @@ -120,6 +121,10 @@ func TestTransportConfig(t *testing.T) {
"header": {
"type": "dtls"
}
},
"grpcSettings": {
"serviceName": "name",
"multiMode": true
}
}`,
Parser: createParser(),
Expand Down Expand Up @@ -190,6 +195,31 @@ func TestTransportConfig(t *testing.T) {
Header: serial.ToTypedMessage(&tls.PacketConfig{}),
}),
},
{
ProtocolName: "grpc",
Settings: serial.ToTypedMessage(&grpc.Config{
ServiceName: "name",
MultiMode: true,
}),
},
},
},
},
{
Input: `{
"gunSettings": {
"serviceName": "name"
}
}`,
Parser: createParser(),
Output: &global.Config{
TransportSettings: []*internet.TransportConfig{
{
ProtocolName: "grpc",
Settings: serial.ToTypedMessage(&grpc.Config{
ServiceName: "name",
}),
},
},
},
},
Expand Down
1 change: 1 addition & 0 deletions main/distro/all/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (

// Transports
_ "github.com/xtls/xray-core/transport/internet/domainsocket"
_ "github.com/xtls/xray-core/transport/internet/grpc"
_ "github.com/xtls/xray-core/transport/internet/http"
_ "github.com/xtls/xray-core/transport/internet/kcp"
_ "github.com/xtls/xray-core/transport/internet/quic"
Expand Down
Loading

0 comments on commit a0a32ee

Please sign in to comment.