-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathmieru.go
277 lines (248 loc) · 7.74 KB
/
mieru.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Copyright (C) 2024 mieru authors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package client
import (
"bytes"
"context"
"encoding/hex"
"fmt"
"io"
"net"
"strings"
"sync"
"time"
apicommon "github.com/enfein/mieru/v3/apis/common"
"github.com/enfein/mieru/v3/apis/constant"
"github.com/enfein/mieru/v3/apis/model"
"github.com/enfein/mieru/v3/pkg/appctl"
"github.com/enfein/mieru/v3/pkg/appctl/appctlpb"
"github.com/enfein/mieru/v3/pkg/cipher"
"github.com/enfein/mieru/v3/pkg/common"
"github.com/enfein/mieru/v3/pkg/log"
"github.com/enfein/mieru/v3/pkg/protocol"
"github.com/enfein/mieru/v3/pkg/stderror"
)
// mieruClient is the official implementation of mieru client APIs.
type mieruClient struct {
initTask sync.Once
mu sync.RWMutex
config *ClientConfig
mux *protocol.Mux
running bool
}
var _ Client = &mieruClient{}
// initOnce should be called when constructing the mieru client.
func (mc *mieruClient) initOnce() {
mc.initTask.Do(func() {
// Disable log.
log.SetFormatter(&log.NilFormatter{})
})
}
func (mc *mieruClient) Load() (*ClientConfig, error) {
mc.mu.Lock()
defer mc.mu.Unlock()
if mc.config == nil {
return nil, ErrNoClientConfig
}
return mc.config, nil
}
func (mc *mieruClient) Store(config *ClientConfig) error {
mc.mu.Lock()
defer mc.mu.Unlock()
if config == nil {
return fmt.Errorf("%w: client config is nil", ErrInvalidConfigConfig)
}
if config.Profile == nil {
return fmt.Errorf("%w: client config profile is nil", ErrInvalidConfigConfig)
}
if mc.running {
return ErrStoreClientConfigAfterStart
}
if err := appctl.ValidateClientConfigSingleProfile(config.Profile); err != nil {
return fmt.Errorf("%w: %s", ErrInvalidConfigConfig, err.Error())
}
mc.config = config
return nil
}
func (mc *mieruClient) Start() error {
mc.mu.Lock()
defer mc.mu.Unlock()
if mc.config == nil {
return ErrNoClientConfig
}
var err error
mc.mux = protocol.NewMux(true)
activeProfile := mc.config.Profile
// Set DNS resolver.
var resolver apicommon.DNSResolver
if mc.config.Resolver != nil {
resolver = mc.config.Resolver
} else {
resolver = &net.Resolver{} // Default DNS resolver.
}
mc.mux.SetResolver(resolver)
// Set user name and password.
user := activeProfile.GetUser()
var hashedPassword []byte
if user.GetHashedPassword() != "" {
hashedPassword, err = hex.DecodeString(user.GetHashedPassword())
if err != nil {
return fmt.Errorf(stderror.DecodeHashedPasswordFailedErr, err)
}
} else {
hashedPassword = cipher.HashPassword([]byte(user.GetPassword()), []byte(user.GetName()))
}
mc.mux = mc.mux.SetClientUserNamePassword(user.GetName(), hashedPassword)
// Set multiplex factor.
multiplexFactor := 1
switch activeProfile.GetMultiplexing().GetLevel() {
case appctlpb.MultiplexingLevel_MULTIPLEXING_OFF:
multiplexFactor = 0
case appctlpb.MultiplexingLevel_MULTIPLEXING_LOW:
multiplexFactor = 1
case appctlpb.MultiplexingLevel_MULTIPLEXING_MIDDLE:
multiplexFactor = 2
case appctlpb.MultiplexingLevel_MULTIPLEXING_HIGH:
multiplexFactor = 3
}
mc.mux = mc.mux.SetClientMultiplexFactor(multiplexFactor)
// Set server endpoints.
mtu := common.DefaultMTU
if activeProfile.GetMtu() != 0 {
mtu = int(activeProfile.GetMtu())
}
endpoints := make([]protocol.UnderlayProperties, 0)
for _, serverInfo := range activeProfile.GetServers() {
var proxyHost string
var proxyIP net.IP
if serverInfo.GetDomainName() != "" {
proxyHost = serverInfo.GetDomainName()
proxyIPs, err := resolver.LookupIP(context.Background(), "ip", proxyHost)
if err != nil {
return fmt.Errorf(stderror.LookupIPFailedErr, err)
}
if len(proxyIPs) == 0 {
return fmt.Errorf(stderror.IPAddressNotFound, proxyHost)
}
proxyIP = proxyIPs[0]
} else {
proxyHost = serverInfo.GetIpAddress()
proxyIP = net.ParseIP(proxyHost)
if proxyIP == nil {
return fmt.Errorf(stderror.ParseIPFailed)
}
}
portBindings, err := appctl.FlatPortBindings(serverInfo.GetPortBindings())
if err != nil {
return fmt.Errorf(stderror.InvalidPortBindingsErr, err)
}
for _, bindingInfo := range portBindings {
proxyPort := bindingInfo.GetPort()
switch bindingInfo.GetProtocol() {
case appctlpb.TransportProtocol_TCP:
endpoint := protocol.NewUnderlayProperties(mtu, common.StreamTransport, nil, &net.TCPAddr{IP: proxyIP, Port: int(proxyPort)})
endpoints = append(endpoints, endpoint)
case appctlpb.TransportProtocol_UDP:
endpoint := protocol.NewUnderlayProperties(mtu, common.PacketTransport, nil, &net.UDPAddr{IP: proxyIP, Port: int(proxyPort)})
endpoints = append(endpoints, endpoint)
default:
return fmt.Errorf(stderror.InvalidTransportProtocol)
}
}
}
mc.mux.SetEndpoints(endpoints)
mc.running = true
return nil
}
func (mc *mieruClient) Stop() error {
mc.mu.Lock()
defer mc.mu.Unlock()
mc.running = false
if mc.mux != nil {
mc.mux.Close()
}
return nil
}
func (mc *mieruClient) IsRunning() bool {
mc.mu.RLock()
defer mc.mu.RUnlock()
return mc.running
}
func (mc *mieruClient) DialContext(ctx context.Context, addr net.Addr) (net.Conn, error) {
mc.mu.RLock()
defer mc.mu.RUnlock()
if !mc.running {
return nil, ErrClientIsNotRunning
}
// Check destination address.
var netAddrSpec model.NetAddrSpec
if err := netAddrSpec.From(addr); err != nil {
return nil, fmt.Errorf("invalid destination address: %w", err)
}
if !strings.HasPrefix(netAddrSpec.Network(), "tcp") {
return nil, fmt.Errorf("only tcp network is supported")
}
conn, err := mc.mux.DialContext(ctx)
if err != nil {
return nil, err
}
return mc.dialPostHandshake(conn, netAddrSpec)
}
func (mc *mieruClient) DialContextWithConn(ctx context.Context, conn net.Conn, addr net.Addr) (net.Conn, error) {
mc.mu.RLock()
defer mc.mu.RUnlock()
if !mc.running {
return nil, ErrClientIsNotRunning
}
// Check destination address.
var netAddrSpec model.NetAddrSpec
if err := netAddrSpec.From(addr); err != nil {
return nil, fmt.Errorf("invalid destination address: %w", err)
}
if !strings.HasPrefix(netAddrSpec.Network(), "tcp") {
return nil, fmt.Errorf("only tcp network is supported")
}
subConn, err := mc.mux.DialContextWithConn(ctx, conn)
if err != nil {
return nil, err
}
return mc.dialPostHandshake(subConn, netAddrSpec)
}
func (mc *mieruClient) dialPostHandshake(conn net.Conn, netAddrSpec model.NetAddrSpec) (net.Conn, error) {
var req bytes.Buffer
req.Write([]byte{constant.Socks5Version, constant.Socks5ConnectCmd, 0})
if err := netAddrSpec.WriteToSocks5(&req); err != nil {
return nil, err
}
if _, err := conn.Write(req.Bytes()); err != nil {
return nil, fmt.Errorf("failed to write socks5 connection request to the server: %w", err)
}
common.SetReadTimeout(conn, 10*time.Second)
defer func() {
common.SetReadTimeout(conn, 0)
}()
resp := make([]byte, 3)
if _, err := io.ReadFull(conn, resp); err != nil {
return nil, fmt.Errorf("failed to read socks5 connection response from the server: %w", err)
}
var respAddr model.NetAddrSpec
if err := respAddr.ReadFromSocks5(conn); err != nil {
return nil, fmt.Errorf("failed to read socks5 connection address response from the server: %w", err)
}
if resp[1] != 0 {
return nil, fmt.Errorf("server returned socks5 error code %d", resp[1])
}
return conn, nil
}