-
Notifications
You must be signed in to change notification settings - Fork 279
/
Copy pathstream_dialer.go
142 lines (122 loc) · 4.08 KB
/
stream_dialer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright 2023 Jigsaw Operations LLC
//
// 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
//
// http://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 intra
import (
"context"
"errors"
"fmt"
"net"
"net/netip"
"sync/atomic"
"time"
"github.com/Jigsaw-Code/Intra/Android/app/src/go/intra/doh"
"github.com/Jigsaw-Code/Intra/Android/app/src/go/intra/protect"
"github.com/Jigsaw-Code/Intra/Android/app/src/go/intra/split"
"github.com/Jigsaw-Code/outline-sdk/transport"
)
type intraStreamDialer struct {
fakeDNSAddr netip.AddrPort
dns atomic.Pointer[doh.Transport]
dialer *net.Dialer
alwaysSplitHTTPS atomic.Bool
listener TCPListener
sniReporter *tcpSNIReporter
}
var _ transport.StreamDialer = (*intraStreamDialer)(nil)
func newIntraStreamDialer(
fakeDNS netip.AddrPort,
dns doh.Transport,
protector protect.Protector,
listener TCPListener,
sniReporter *tcpSNIReporter,
) (*intraStreamDialer, error) {
if dns == nil {
return nil, errors.New("dns is required")
}
dohsd := &intraStreamDialer{
fakeDNSAddr: fakeDNS,
dialer: protect.MakeDialer(protector),
listener: listener,
sniReporter: sniReporter,
}
dohsd.dns.Store(&dns)
return dohsd, nil
}
// Dial implements StreamDialer.Dial.
func (sd *intraStreamDialer) Dial(ctx context.Context, raddr string) (transport.StreamConn, error) {
dest, err := netip.ParseAddrPort(raddr)
if err != nil {
return nil, fmt.Errorf("invalid raddr (%v): %w", raddr, err)
}
if isEquivalentAddrPort(dest, sd.fakeDNSAddr) {
src, dst := net.Pipe()
go doh.Accept(*sd.dns.Load(), dst)
return newStreamConnFromPipeConns(src, dst)
}
stats := makeTCPSocketSummary(dest)
beforeConn := time.Now()
conn, err := sd.dial(ctx, dest, stats)
if err != nil {
return nil, fmt.Errorf("failed to dial to target: %w", err)
}
stats.Synack = int32(time.Since(beforeConn).Milliseconds())
return makeTCPWrapConn(conn, stats, sd.listener, sd.sniReporter), nil
}
func (sd *intraStreamDialer) SetDNS(dns doh.Transport) error {
if dns == nil {
return errors.New("dns is required")
}
sd.dns.Store(&dns)
return nil
}
func (sd *intraStreamDialer) dial(ctx context.Context, dest netip.AddrPort, stats *TCPSocketSummary) (transport.StreamConn, error) {
if dest.Port() == 443 {
if sd.alwaysSplitHTTPS.Load() {
return split.DialWithSplit(sd.dialer, net.TCPAddrFromAddrPort(dest))
} else {
stats.Retry = &split.RetryStats{}
return split.DialWithSplitRetry(sd.dialer, net.TCPAddrFromAddrPort(dest), stats.Retry)
}
} else {
tcpsd := &transport.TCPStreamDialer{
Dialer: *sd.dialer,
}
return tcpsd.Dial(ctx, dest.String())
}
}
// transport.StreamConn wrapper around net.Pipe call
type pipeconn struct {
net.Conn
remote net.Conn
}
var _ transport.StreamConn = (*pipeconn)(nil)
// newStreamConnFromPipeConns creates a new [transport.StreamConn] that wraps around the local [net.Conn].
// The remote [net.Conn] will be closed when you call CloseRead() on the returned [transport.StreamConn]
func newStreamConnFromPipeConns(local, remote net.Conn) (transport.StreamConn, error) {
if local == nil || remote == nil {
return nil, errors.New("local conn and remote conn are required")
}
return &pipeconn{local, remote}, nil
}
func (c *pipeconn) Close() error {
return errors.Join(c.CloseRead(), c.CloseWrite())
}
// CloseRead makes sure all read on the local conn returns io.EOF, and write on the remote conn returns ErrClosedPipe.
func (c *pipeconn) CloseRead() error {
return c.remote.Close()
}
// CloseWrite makes sure all read on the remote conn returns io.EOF, and write on the local conn returns ErrClosedPipe.
func (c *pipeconn) CloseWrite() error {
return c.Conn.Close()
}