Skip to content

Add Datagram transport #4283

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions common/crypto/chunk.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package crypto

import (
"context"
"encoding/binary"
"io"

"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/errors"
)

// ChunkSizeDecoder is a utility class to decode size value from bytes.
Expand Down Expand Up @@ -117,6 +119,7 @@ func (r *ChunkStreamReader) ReadMultiBuffer() (buf.MultiBuffer, error) {
}
r.leftOverSize = size

errors.LogInfo(context.Background(), "StreamReader read ", size)
mb, err := r.reader.ReadAtMost(size)
if !mb.IsEmpty() {
r.leftOverSize -= mb.Len()
Expand Down
2 changes: 2 additions & 0 deletions common/mux/reader.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mux

import (
"context"
"io"

"github.com/xtls/xray-core/common/buf"
Expand Down Expand Up @@ -33,6 +34,7 @@ func (r *PacketReader) ReadMultiBuffer() (buf.MultiBuffer, error) {
}

size, err := serial.ReadUint16(r.reader)
errors.LogInfo(context.Background(), "PacketReader read ", size, r.dest)
if err != nil {
return nil, err
}
Expand Down
6 changes: 4 additions & 2 deletions common/mux/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ func TestRegressionOutboundLeak(t *testing.T) {
}

{
b := buf.FromBytes([]byte("hello"))
b := buf.New()
b.Write([]byte("hello"))
common.Must(muxClientDownlink.Writer.WriteMultiBuffer(buf.MultiBuffer{b}))
}

Expand All @@ -102,7 +103,8 @@ func TestRegressionOutboundLeak(t *testing.T) {
}

{
b := buf.FromBytes([]byte("world"))
b := buf.New()
b.Write([]byte("world"))
common.Must(websiteUplink.Writer.WriteMultiBuffer(buf.MultiBuffer{b}))
}

Expand Down
6 changes: 5 additions & 1 deletion common/mux/writer.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package mux

import (
"context"

"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/common/protocol"
"github.com/xtls/xray-core/common/serial"
Expand Down Expand Up @@ -75,10 +78,10 @@ func writeMetaWithFrame(writer buf.Writer, meta FrameMetadata, data buf.MultiBuf
if _, err := serial.WriteUint16(frame, uint16(data.Len())); err != nil {
return err
}

mb2 := make(buf.MultiBuffer, 0, len(data)+1)
mb2 = append(mb2, frame)
mb2 = append(mb2, data...)
mb2 = buf.Compact(mb2)
return writer.WriteMultiBuffer(mb2)
}

Expand Down Expand Up @@ -106,6 +109,7 @@ func (w *Writer) WriteMultiBuffer(mb buf.MultiBuffer) error {
mb = mb2
chunk = buf.MultiBuffer{b}
}
errors.LogInfo(context.Background(), "MuxWriter write ", chunk.Len(), w.dest)
if err := w.writeData(chunk); err != nil {
return err
}
Expand Down
32 changes: 30 additions & 2 deletions infra/conf/transport_internet.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/xtls/xray-core/transport/internet"
"github.com/xtls/xray-core/transport/internet/httpupgrade"
"github.com/xtls/xray-core/transport/internet/kcp"
"github.com/xtls/xray-core/transport/internet/quic"
"github.com/xtls/xray-core/transport/internet/reality"
"github.com/xtls/xray-core/transport/internet/splithttp"
"github.com/xtls/xray-core/transport/internet/tcp"
Expand Down Expand Up @@ -332,6 +333,22 @@ func (c *SplitHTTPConfig) Build() (proto.Message, error) {
return config, nil
}

type QUICConfig struct {
// Header json.RawMessage `json:"header"`
// Security string `json:"security"`
// Key string `json:"key"`

Fec bool `json:"fec"`
}

// Build implements Buildable.
func (c *QUICConfig) Build() (proto.Message, error) {
config := &quic.Config{
Fec: c.Fec,
}
return config, nil
}

func readFileOrString(f string, s []string) ([]byte, error) {
if len(f) > 0 {
return filesystem.ReadFile(f)
Expand Down Expand Up @@ -683,8 +700,8 @@ func (p TransportProtocol) Build() (string, error) {
return "httpupgrade", nil
case "h2", "h3", "http":
return "", errors.PrintRemovedFeatureError("HTTP transport (without header padding, etc.)", "XHTTP stream-one H2 & H3")
case "quic":
return "", errors.PrintRemovedFeatureError("QUIC transport (without web service, etc.)", "XHTTP stream-one H3")
case "quic", "datagram":
return "quic", nil
default:
return "", errors.New("Config: unknown transport protocol: ", p)
}
Expand Down Expand Up @@ -839,6 +856,7 @@ type StreamConfig struct {
XHTTPSettings *SplitHTTPConfig `json:"xhttpSettings"`
SplitHTTPSettings *SplitHTTPConfig `json:"splithttpSettings"`
KCPSettings *KCPConfig `json:"kcpSettings"`
QUICSettings *QUICConfig `json:"quicSettings"`
GRPCSettings *GRPCConfig `json:"grpcSettings"`
WSSettings *WebSocketConfig `json:"wsSettings"`
HTTPUPGRADESettings *HttpUpgradeConfig `json:"httpupgradeSettings"`
Expand Down Expand Up @@ -930,6 +948,16 @@ func (c *StreamConfig) Build() (*internet.StreamConfig, error) {
Settings: serial.ToTypedMessage(ts),
})
}
if c.QUICSettings != nil {
qs, err := c.QUICSettings.Build()
if err != nil {
return nil, errors.New("Failed to build QUIC config").Base(err)
}
config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
ProtocolName: "quic",
Settings: serial.ToTypedMessage(qs),
})
}
if c.GRPCSettings != nil {
gs, err := c.GRPCSettings.Build()
if err != nil {
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 @@ -53,6 +53,7 @@ import (
_ "github.com/xtls/xray-core/transport/internet/grpc"
_ "github.com/xtls/xray-core/transport/internet/httpupgrade"
_ "github.com/xtls/xray-core/transport/internet/kcp"
_ "github.com/xtls/xray-core/transport/internet/quic"
_ "github.com/xtls/xray-core/transport/internet/reality"
_ "github.com/xtls/xray-core/transport/internet/splithttp"
_ "github.com/xtls/xray-core/transport/internet/tcp"
Expand Down
137 changes: 137 additions & 0 deletions transport/internet/quic/config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions transport/internet/quic/config.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
syntax = "proto3";

package xray.transport.internet.quic;
option csharp_namespace = "Xray.Transport.Internet.Quic";
option go_package = "github.com/xtls/xray-core/transport/internet/quic";
option java_package = "com.xray.transport.internet.quic";
option java_multiple_files = true;

message Config {
// string key = 1;
// xray.common.protocol.SecurityConfig security = 2;
// xray.common.serial.TypedMessage header = 3;
bool fec = 4;
}
Loading