-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproxy.go
161 lines (134 loc) · 4.02 KB
/
proxy.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright (c) 2022 Vasiliy Vasilyuk. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tornado
import (
"context"
"fmt"
"net"
"net/url"
"os"
"os/exec"
"runtime"
"sync"
"golang.org/x/net/proxy"
)
// NewProxy creates new instance of Proxy.
//
// If the Proxy was created using NewProxy, it must be closed wia using Close
// method after end usage to prevent memory leak and tor demon process leak.
func NewProxy(ctx context.Context, ops ...Option) (*Proxy, error) {
if ctx == nil {
panic("tornado: nil context")
}
state := options{
numberOfProxy: 1,
}
for _, option := range ops {
option.apply(&state)
}
trc, err := newTorrcFromState(state)
if err != nil {
return nil, err
}
cmd, err := launchBackgroundTorDemon(ctx, trc)
if err != nil {
const format = "cannot run tor demon for a single proxy: %v"
return nil, fmt.Errorf(format, err)
}
closeFunc := makeCloseFunc(cmd)
prx, err := openSOCKS5Proxy(trc.socksPort[0], state.forwardDialer, closeFunc)
if err != nil {
const format = "cannot create proxy instance: %v"
return nil, fmt.Errorf(format, err)
}
return prx, nil
}
// Proxy returns a ContextDialer that makes connections to the given
// address over tor network.
type Proxy struct {
proxy ContextDialer
valid bool
closeFunc func() error
closeOnce sync.Once
}
// DialContext connects to the address on the named network over
// tor network using the provided context.
//
// The provided Context must be non-nil. If the context expires before
// the connection is complete, an error is returned. Once successfully
// connected, any expiration of the context will not affect the
// connection.
//
// See func Dial of the net package of standard library for a
// description of the network and address parameters.
func (p *Proxy) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
if ctx == nil {
panic("tornado: nil context")
}
return p.proxy.DialContext(ctx, network, address)
}
// Dial connects to the address on the named network over tor network.
//
// See func Dial of the net package of standard library for a
// description of the network and address parameters.
//
// Dial uses context.Background internally; to specify the context, use
// DialContext.
func (p *Proxy) Dial(network, address string) (c net.Conn, err error) {
return p.proxy.DialContext(context.Background(), network, address)
}
// Close stops the tor demon running in the background.
// If the Proxy was created using NewPool directly instead of NewProxy,
// Close has no effect.
//
// This operation will not wait for active connections to close,
// they will be aborted.
func (p *Proxy) Close() (err error) {
p.closeOnce.Do(func() {
if p.closeFunc != nil {
err = p.closeFunc()
}
// no need for a finalizer anymore.
runtime.SetFinalizer(p, nil)
})
return err
}
func (p *Proxy) isValid() bool {
return p != nil && p.valid
}
func openSOCKS5Proxy(port int, forward dialer, closeFunc func() error) (*Proxy, error) {
const socksFormat = "socks5://localhost:%d"
socks5URL, err := url.Parse(fmt.Sprintf(socksFormat, port))
if err != nil {
const format = "cannot create socks5 url: %v"
return nil, fmt.Errorf(format, err)
}
dialer, err := proxy.FromURL(socks5URL, forward)
if err != nil {
const format = "cannot create proxy dialer: %v"
return nil, fmt.Errorf(format, err)
}
prx := &Proxy{
proxy: dialer.(ContextDialer),
valid: true,
closeFunc: closeFunc,
}
runtime.SetFinalizer(prx, (*Proxy).Close)
return prx, nil
}
func makeCloseFunc(cmd *exec.Cmd) func() error {
return func() error {
if err := cmd.Process.Signal(os.Interrupt); err != nil {
format := "an error occurred while sending, a signal to interrupt" +
" the operation of the tor demon: %v"
return fmt.Errorf(format, err)
}
if err := cmd.Wait(); err != nil {
const format = "error while waiting is the result of sending," +
" a signal to interrupt the command: %v"
return fmt.Errorf(format, err)
}
return nil
}
}