forked from Jigsaw-Code/outline-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: disorder: a new linux-only transport
- Loading branch information
Showing
7 changed files
with
212 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/transport/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 | ||
} | ||
prefixBytesStr := config.URL.Opaque | ||
prefixBytes, err := strconv.Atoi(prefixBytesStr) | ||
if err != nil { | ||
return nil, fmt.Errorf("prefixBytes is not a number: %v. Split config should be in split:<number> format", prefixBytesStr) | ||
} | ||
return disorder.NewStreamDialer(sd, int64(prefixBytes)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
fetch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// 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" | ||
"net/netip" | ||
|
||
"github.com/Jigsaw-Code/outline-sdk/transport" | ||
"golang.org/x/net/ipv4" | ||
"golang.org/x/net/ipv6" | ||
) | ||
|
||
var defaultTTL = 64 | ||
|
||
type disorderDialer struct { | ||
dialer transport.StreamDialer | ||
splitPoint int64 | ||
} | ||
|
||
var _ transport.StreamDialer = (*disorderDialer)(nil) | ||
|
||
// NewStreamDialer creates a [transport.StreamDialer] that splits the outgoing stream after writing "prefixBytes" bytes | ||
// using [SplitWriter]. | ||
func NewStreamDialer(dialer transport.StreamDialer, prefixBytes int64) (transport.StreamDialer, error) { | ||
if dialer == nil { | ||
return nil, errors.New("argument dialer must not be nil") | ||
} | ||
return &disorderDialer{dialer: dialer, splitPoint: prefixBytes}, 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 | ||
} | ||
|
||
oldTTL, err := setTtl(innerConn, 1) | ||
if err != nil { | ||
return nil, fmt.Errorf("disorder strategy: failed to change ttl: %w", err) | ||
} | ||
|
||
dw := NewWriter(innerConn, d.splitPoint, oldTTL) | ||
|
||
return transport.WrapConn(innerConn, innerConn, dw), nil | ||
} | ||
|
||
func setTtl(conn net.Conn, ttl int) (oldTTL int, err error) { | ||
|
||
addr, err := netip.ParseAddrPort(conn.RemoteAddr().String()) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
switch { | ||
case addr.Addr().Is4(): | ||
conn := ipv4.NewConn(conn) | ||
oldTTL, _ = conn.TTL() | ||
err = conn.SetTTL(ttl) | ||
case addr.Addr().Is6(): | ||
conn := ipv6.NewConn(conn) | ||
oldTTL, _ = conn.HopLimit() | ||
err = conn.SetHopLimit(ttl) | ||
} | ||
if err != nil { | ||
return 0, fmt.Errorf("failed to change TTL: %w", err) | ||
} | ||
|
||
if oldTTL == 0 { | ||
oldTTL = defaultTTL | ||
} | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// 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" | ||
"net" | ||
"sync" | ||
) | ||
|
||
type disorderWriter struct { | ||
conn net.Conn | ||
resetTTL sync.Once | ||
prefixBytes int64 | ||
oldTTL int | ||
} | ||
|
||
var _ io.Writer = (*disorderWriter)(nil) | ||
|
||
// TODO | ||
// var _ io.ReaderFrom = (*splitWriterReaderFrom)(nil) | ||
|
||
// TODO | ||
func NewWriter(conn net.Conn, prefixBytes int64, oldTTL int) io.Writer { | ||
// TODO support ReaderFrom | ||
return &disorderWriter{ | ||
conn: conn, | ||
prefixBytes: prefixBytes, | ||
oldTTL: oldTTL, | ||
} | ||
} | ||
|
||
func (w *disorderWriter) Write(data []byte) (written int, err error) { | ||
if 0 < w.prefixBytes && w.prefixBytes < int64(len(data)) { | ||
written, err = w.conn.Write(data[:w.prefixBytes]) | ||
w.prefixBytes -= int64(written) | ||
if err != nil { | ||
return written, err | ||
} | ||
data = data[written:] | ||
} | ||
w.resetTTL.Do(func() { | ||
_, err = setTtl(w.conn, w.oldTTL) | ||
}) | ||
if err != nil { | ||
return written, fmt.Errorf("setsockopt IPPROTO_IP/IP_TTL error: %w", err) | ||
} | ||
|
||
n, err := w.conn.Write(data) | ||
written += n | ||
w.prefixBytes -= int64(n) | ||
return written, err | ||
} |