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
|
package obfsvpn
import (
"context"
"fmt"
"net"
"strconv"
pt "git.torproject.org/pluggable-transports/goptlib.git"
"gitlab.com/yawning/obfs4.git/common/ntor"
"gitlab.com/yawning/obfs4.git/transports/base"
"gitlab.com/yawning/obfs4.git/transports/obfs4"
)
const (
ptArgNode = "node-id"
ptArgKey = "public-key"
ptArgMode = "iat-mode"
)
// IATMode determines the amount of time sent between packets.
type IATMode int
// Valid IAT modes.
const (
IATNone IATMode = iota
IATEnabled
IATParanoid
)
// Dialer contains options for connecting to an address and obfuscating traffic
// with the obfs4 protocol.
// It performs the ntor handshake on all dialed connections.
type Dialer struct {
Dialer net.Dialer
NodeID *ntor.NodeID
PublicKey *ntor.PublicKey
IATMode IATMode
ptArgs pt.Args
cf base.ClientFactory
}
// NewDialer creates a dialer from existing pluggable transport arguments.
func NewDialer(args pt.Args) (*Dialer, error) {
cf, err := (&obfs4.Transport{}).ClientFactory("")
if err != nil {
return nil, err
}
nodeHex, _ := args.Get(ptArgNode)
node, err := ntor.NodeIDFromHex(nodeHex)
if err != nil {
return nil, err
}
keyHex, _ := args.Get(ptArgKey)
pub, err := ntor.PublicKeyFromHex(keyHex)
if err != nil {
return nil, err
}
iatModeStr, _ := args.Get(ptArgMode)
iatMode, err := strconv.Atoi(iatModeStr)
if err != nil {
return nil, fmt.Errorf("error parsing IAT mode to integer: %w", err)
}
return &Dialer{
NodeID: node,
PublicKey: pub,
IATMode: IATMode(iatMode),
ptArgs: args,
cf: cf,
}, nil
}
// Dial creates an outbound net.Conn and performs the ntor handshake.
func (d *Dialer) Dial(ctx context.Context, network, address string) (net.Conn, error) {
if d.cf == nil {
cf, err := (&obfs4.Transport{}).ClientFactory("")
if err != nil {
return nil, err
}
d.cf = cf
}
ptArgs := d.Args()
args, err := d.cf.ParseArgs(&ptArgs)
if err != nil {
return nil, err
}
return d.cf.Dial(network, address, func(network, address string) (net.Conn, error) {
return d.Dialer.DialContext(ctx, network, address)
}, args)
}
// Args returns the dialers options as pluggable transport arguments.
func (d *Dialer) Args() pt.Args {
if d.ptArgs == nil {
d.ptArgs = make(pt.Args)
d.ptArgs.Add(ptArgNode, d.NodeID.Hex())
d.ptArgs.Add(ptArgKey, d.PublicKey.Hex())
d.ptArgs.Add(ptArgMode, strconv.Itoa(int(d.IATMode)))
}
return d.ptArgs
}
|