This repository has been archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
proxy.go
221 lines (192 loc) · 5.97 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// proxy.go - Katzenpost client mail proxy upstream proxy support.
// Copyright (C) 2018 Yawning Angel.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// Package proxy implements the support for an upstream (outgoing) proxy.
package proxy
import (
"context"
"crypto/sha512"
"encoding/binary"
"encoding/hex"
"fmt"
"net"
"os"
"strings"
"time"
"github.com/katzenpost/core/pki"
"github.com/katzenpost/core/utils"
"golang.org/x/net/proxy"
)
const (
typeNone = "none"
typeTorSocks5 = "tor+socks5"
typeSocks5 = "socks5"
netUnix = "unix"
netTCP = "tcp"
maxSocks5AuthLen = 255
)
var torSocks5ProcessIsolation string
// Config is the proxy configuration.
type Config struct {
// PreferedTransports is a list of the transports will be used to make
// outgoing network connections, with the most prefered first.
PreferedTransports []pki.Transport
// Type is the proxy type (Eg: "none"," socks5", "tor+socks5").
Type string
// Network is the proxy address' network (`unix`, `tcp`).
Network string
// Address is the proxy's address.
Address string
// User is the optional proxy username.
User string
// Password is the optional proxy password.
Password string
auth *proxy.Auth
}
// DialContextFn is a function that matches the Dialer.DialContext prototype.
type DialContextFn func(context.Context, string, string) (net.Conn, error)
// FixupAndValidate applies defaults to config entires and validates the
// supplied configuration.
func (cfg *Config) FixupAndValidate() error {
cfg.Type = strings.ToLower(cfg.Type)
switch cfg.Type {
case "":
cfg.Type = typeNone
case typeNone:
case typeSocks5, typeTorSocks5:
uLen, pLen := len(cfg.User), len(cfg.Password)
if uLen > maxSocks5AuthLen {
return fmt.Errorf("proxy/config: User too long")
}
if pLen > maxSocks5AuthLen {
return fmt.Errorf("proxy/config: Password too long")
}
if uLen != 0 && pLen == 0 || uLen == 0 && pLen != 0 {
return fmt.Errorf("proxy/config: Both User and Password must be specified")
}
if uLen != 0 && pLen != 0 {
if cfg.Type == typeTorSocks5 {
return fmt.Errorf("proxy:config: Tor SOCKS5 conflicts with setting User/Password")
}
cfg.auth = &proxy.Auth{
User: cfg.User,
Password: cfg.Password,
}
}
cfg.Network = strings.ToLower(cfg.Network)
switch cfg.Network {
case netTCP:
if err := utils.EnsureAddrIPPort(cfg.Address); err != nil {
return fmt.Errorf("proxy/config: Address '%v' is invalid: %v", cfg.Address, err)
}
case netUnix:
fi, err := os.Lstat(cfg.Address)
if err != nil {
return fmt.Errorf("proxy/config: Address '%v' failed to stat(): %v", cfg.Address, err)
}
if fi.Mode()&os.ModeSocket == 0 {
return fmt.Errorf("proxy/config: Address '%v' does not appear to be a socket", cfg.Address)
}
default:
return fmt.Errorf("proxy/config: Network '%v' is invalid", cfg.Network)
}
default:
return fmt.Errorf("proxy/config: Type '%v' is invalid", cfg.Type)
}
return nil
}
// ToDialContext returns a function matching Dialer.DialContext() that will
// utilize the configured proxy or nil iff no proxy is configured.
func (cfg *Config) ToDialContext(tag string) DialContextFn {
switch cfg.Type {
case typeNone:
return nil
case typeSocks5, typeTorSocks5:
return cfg.newContextSOCKS5(tag)
default:
panic("proxy: ToDialContext(): invalid type: " + cfg.Type)
}
}
func (cfg *Config) newContextSOCKS5(tag string) DialContextFn {
auth := cfg.auth
if cfg.Type == typeTorSocks5 {
auth = &proxy.Auth{}
// Craft an SOCKSPort isolation entry from `tag`, and jam it into
// the User/Password.
sum := sha512.Sum512_256([]byte(tag))
isolationTag := torSocks5ProcessIsolation + hex.EncodeToString(sum[:16])
auth.User = isolationTag
auth.Password = string([]byte{0x00})
}
s := &contextSOCKS5{
proxyNet: cfg.Network,
proxyAddr: cfg.Address,
proxyAuth: auth,
}
return s.dialContext
}
type contextSOCKS5 struct {
proxyNet string
proxyAddr string
proxyAuth *proxy.Auth
}
func (s *contextSOCKS5) dialContext(ctx context.Context, network, address string) (net.Conn, error) {
// One day, golang.org/x/net/proxy will support using a context.
// See: https://github.com/golang/go/issues/19354
fwdDialer := &contextDialer{
ctx: ctx,
connCh: make(chan net.Conn),
}
defer close(fwdDialer.connCh)
socksDialer, err := proxy.SOCKS5(s.proxyNet, s.proxyAddr, s.proxyAuth, fwdDialer)
if err != nil {
return nil, err
}
go func() {
// Wait for the forward dial process to finish.
conn, ok := <-fwdDialer.connCh
if !ok {
return
}
// Do the "right" thing based on the context.
select {
case <-ctx.Done():
if conn != nil {
conn.Close()
}
case <-fwdDialer.connCh:
}
}()
return socksDialer.Dial(network, address)
}
type contextDialer struct {
ctx context.Context // I know this is frowned upon.
connCh chan net.Conn
}
func (c *contextDialer) Dial(network, address string) (net.Conn, error) {
directDialer := &net.Dialer{}
conn, err := directDialer.DialContext(c.ctx, network, address)
c.connCh <- conn
return conn, err
}
func init() {
// Initialize the per-process Tor SOCKS isolation tag. This is
// probably massive overkill.
var buf [16]byte
binary.BigEndian.PutUint64(buf[0:], uint64(os.Getpid()))
binary.BigEndian.PutUint64(buf[8:], uint64(time.Now().Unix()))
sum := sha512.Sum512_256(buf[:])
torSocks5ProcessIsolation = "katzenpost/mailproxy:" + hex.EncodeToString(sum[:8]) + ":"
}