Skip to content
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

Disorder strategy from byeDPI #323

Merged
merged 8 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
39 changes: 39 additions & 0 deletions x/configurl/disorder.go
PeterZhizhin marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2024 The Outline Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package configurl

import (
"context"
"fmt"
"strconv"

"github.com/Jigsaw-Code/outline-sdk/transport"
"github.com/Jigsaw-Code/outline-sdk/x/disorder"
)

func registerDisorderDialer(r TypeRegistry[transport.StreamDialer], typeID string, newSD BuildFunc[transport.StreamDialer]) {
r.RegisterType(typeID, func(ctx context.Context, config *Config) (transport.StreamDialer, error) {
sd, err := newSD(ctx, config.BaseConfig)
if err != nil {
return nil, err
}
disorderPacketNStr := config.URL.Opaque
disorderPacketN, err := strconv.Atoi(disorderPacketNStr)
if err != nil {
return nil, fmt.Errorf("disoder: could not parse splice position: %v", err)
}
return disorder.NewStreamDialer(sd, disorderPacketN)
})
}
1 change: 1 addition & 0 deletions x/configurl/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func NewProviderContainer() *ProviderContainer {
// RegisterDefaultProviders registers a set of default providers with the providers in [ProviderContainer].
func RegisterDefaultProviders(c *ProviderContainer) *ProviderContainer {
// Please keep the list in alphabetical order.
registerDisorderDialer(&c.StreamDialers, "disorder", c.StreamDialers.NewInstance)
registerDO53StreamDialer(&c.StreamDialers, "do53", c.StreamDialers.NewInstance, c.PacketDialers.NewInstance)
registerDOHStreamDialer(&c.StreamDialers, "doh", c.StreamDialers.NewInstance)

Expand Down
73 changes: 73 additions & 0 deletions x/disorder/stream_dialer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2024 The Outline Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package disorder

import (
"context"
"errors"
"fmt"
"net"

"github.com/Jigsaw-Code/outline-sdk/transport"
"github.com/Jigsaw-Code/outline-sdk/x/sockopt"
)

type disorderDialer struct {
dialer transport.StreamDialer
disorderPacketN int
}

var _ transport.StreamDialer = (*disorderDialer)(nil)

// NewStreamDialer creates a [transport.StreamDialer]
PeterZhizhin marked this conversation as resolved.
Show resolved Hide resolved
// It work almost the same as the other split dialer, however, it also manipulates socket TTL:
// * Before sending the first prefixBytes TTL is set to 1
// * This packet is dropped somewhere in the network and never reaches the server
// * TTL is restored
// * The next part of data is sent normally
// * Server notices the lost fragment and requests re-transmission
// Currently this only works with Linux kernel (for Windows/Mac a different implementation is required)
PeterZhizhin marked this conversation as resolved.
Show resolved Hide resolved
PeterZhizhin marked this conversation as resolved.
Show resolved Hide resolved
func NewStreamDialer(dialer transport.StreamDialer, disorderPacketN int) (transport.StreamDialer, error) {
if dialer == nil {
return nil, errors.New("argument dialer must not be nil")
}
return &disorderDialer{dialer: dialer, disorderPacketN: disorderPacketN}, nil
}

// DialStream implements [transport.StreamDialer].DialStream.
func (d *disorderDialer) DialStream(ctx context.Context, remoteAddr string) (transport.StreamConn, error) {
innerConn, err := d.dialer.DialStream(ctx, remoteAddr)
if err != nil {
return nil, err
}

tcpInnerConn, ok := innerConn.(*net.TCPConn)
if !ok {
return nil, fmt.Errorf("disorder strategy: expected base dialer to return TCPConn")
}
tcpOptions, err := sockopt.NewTCPOptions(tcpInnerConn)
if err != nil {
return nil, err
}

defaultHopLimit, err := tcpOptions.HopLimit()
if err != nil {
return nil, fmt.Errorf("disorder strategy: failed to get base connection HopLimit: %w", err)
}

dw := NewWriter(innerConn, tcpOptions, d.disorderPacketN, defaultHopLimit)

return transport.WrapConn(innerConn, innerConn, dw), nil
}
76 changes: 76 additions & 0 deletions x/disorder/writer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright 2024 The Outline Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package disorder

import (
"fmt"
"io"

"github.com/Jigsaw-Code/outline-sdk/x/sockopt"
)

type disorderWriter struct {
conn io.Writer
tcpOptions sockopt.TCPOptions
runAtPacketN int
defaultHopLimit int
writeCalls int
}

var _ io.Writer = (*disorderWriter)(nil)

// Setting number of hops to 1 will lead to data to get lost on host
PeterZhizhin marked this conversation as resolved.
Show resolved Hide resolved
var disorderHopN = 1

func NewWriter(conn io.Writer, tcpOptions sockopt.TCPOptions, runAtPacketN int, defaultHopLimit int) io.Writer {
PeterZhizhin marked this conversation as resolved.
Show resolved Hide resolved
return &disorderWriter{
conn: conn,
tcpOptions: tcpOptions,
runAtPacketN: runAtPacketN,
PeterZhizhin marked this conversation as resolved.
Show resolved Hide resolved
defaultHopLimit: defaultHopLimit,
PeterZhizhin marked this conversation as resolved.
Show resolved Hide resolved
writeCalls: 0,
}
}

func (w *disorderWriter) Write(data []byte) (written int, err error) {
shouldDoDisorder := w.writeCalls == w.runAtPacketN
if shouldDoDisorder {
err = w.tcpOptions.SetHopLimit(disorderHopN)
PeterZhizhin marked this conversation as resolved.
Show resolved Hide resolved
fortuna marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return 0, fmt.Errorf("failed to set the hop limit to %d: %w", disorderHopN, err)
fortuna marked this conversation as resolved.
Show resolved Hide resolved
}

// The packet will get lost at the first send, since the hop limit is too low
}

n, err := w.conn.Write(data)

// TODO: Wait for queued data to be sent by the kernel to the socket
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if the OS will respect the splits if we change options between writes.

PeterZhizhin marked this conversation as resolved.
Show resolved Hide resolved

if shouldDoDisorder {
// The packet with low hop limit was sent
// Make next calls send data normally
//
// The packet with the low hop limit will get resent by the kernel later
// The network filters will receive data out of order
err = w.tcpOptions.SetHopLimit(w.defaultHopLimit)
PeterZhizhin marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return n, fmt.Errorf("failed to set the hop limit error %d: %w", w.defaultHopLimit, err)
}
}

w.writeCalls += 1
return n, err
}
Loading