-
Notifications
You must be signed in to change notification settings - Fork 25
/
outline_device.go
104 lines (90 loc) · 2.93 KB
/
outline_device.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
// 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
//
// https://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 main
import (
"errors"
"fmt"
"net"
"net/url"
"strings"
"github.com/Jigsaw-Code/outline-sdk/network"
"github.com/Jigsaw-Code/outline-sdk/network/lwip2transport"
"github.com/Jigsaw-Code/outline-sdk/transport"
"github.com/Jigsaw-Code/outline-sdk/x/config"
)
const (
connectivityTestDomain = "www.google.com"
connectivityTestResolver = "1.1.1.1:53"
)
type OutlineDevice struct {
network.IPDevice
sd transport.StreamDialer
pp *outlinePacketProxy
svrIP net.IP
}
var configToDialer = config.NewDefaultConfigToDialer()
func NewOutlineDevice(transportConfig string) (od *OutlineDevice, err error) {
ip, err := resolveShadowsocksServerIPFromConfig(transportConfig)
if err != nil {
return nil, err
}
od = &OutlineDevice{
svrIP: ip,
}
if od.sd, err = configToDialer.NewStreamDialer(transportConfig); err != nil {
return nil, fmt.Errorf("failed to create TCP dialer: %w", err)
}
if od.pp, err = newOutlinePacketProxy(transportConfig); err != nil {
return nil, fmt.Errorf("failed to create delegate UDP proxy: %w", err)
}
if od.IPDevice, err = lwip2transport.ConfigureDevice(od.sd, od.pp); err != nil {
return nil, fmt.Errorf("failed to configure lwIP: %w", err)
}
return
}
func (d *OutlineDevice) Close() error {
return d.IPDevice.Close()
}
func (d *OutlineDevice) Refresh() error {
return d.pp.testConnectivityAndRefresh(connectivityTestResolver, connectivityTestDomain)
}
func (d *OutlineDevice) GetServerIP() net.IP {
return d.svrIP
}
func resolveShadowsocksServerIPFromConfig(transportConfig string) (net.IP, error) {
if strings.Contains(transportConfig, "|") {
return nil, errors.New("multi-part config is not supported")
}
if transportConfig = strings.TrimSpace(transportConfig); transportConfig == "" {
return nil, errors.New("config is required")
}
url, err := url.Parse(transportConfig)
if err != nil {
return nil, fmt.Errorf("failed to parse config: %w", err)
}
if url.Scheme != "ss" {
return nil, errors.New("config must start with 'ss://'")
}
ipList, err := net.LookupIP(url.Hostname())
if err != nil {
return nil, fmt.Errorf("invalid server hostname: %w", err)
}
// todo: we only tested IPv4 routing table, need to test IPv6 in the future
for _, ip := range ipList {
if ip = ip.To4(); ip != nil {
return ip, nil
}
}
return nil, errors.New("IPv6 only Shadowsocks server is not supported yet")
}