forked from SagerNet/LibSagerNetCore
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathprotect.go
148 lines (130 loc) · 3.3 KB
/
protect.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
package libcore
import (
"context"
"errors"
"fmt"
"github.com/sirupsen/logrus"
v2rayNet "github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/features/dns"
"github.com/xtls/xray-core/transport/internet"
"golang.org/x/sys/unix"
"net"
"os"
"time"
)
var fdProtector Protector
type Protector interface {
Protect(fd int32) bool
}
func SetProtector(protector Protector) {
fdProtector = protector
}
type protectedDialer struct {
resolver func(domain string) ([]net.IP, error)
}
func (dialer protectedDialer) Dial(ctx context.Context, source v2rayNet.Address, destination v2rayNet.Destination, sockopt *internet.SocketConfig) (conn net.Conn, err error) {
var ips []net.IP
if destination.Address.Family().IsDomain() {
ips, err = dialer.resolver(destination.Address.Domain())
if err == nil && len(ips) == 0 {
err = dns.ErrEmptyResponse
}
if err != nil {
return nil, err
}
} else {
ips = append(ips, destination.Address.IP())
}
for i, ip := range ips {
if i > 0 {
if err == nil {
break
} else {
logrus.Warn("dial system failed: ", err)
time.Sleep(time.Millisecond * 200)
}
logrus.Debug("trying next address: ", ip.String())
}
destination.Address = v2rayNet.IPAddress(ip)
conn, err = dialer.dial(ctx, source, destination, sockopt)
}
return conn, err
}
func (dialer protectedDialer) dial(ctx context.Context, source v2rayNet.Address, destination v2rayNet.Destination, sockopt *internet.SocketConfig) (conn net.Conn, err error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
destIp := destination.Address.IP()
ipv6 := len(destIp) != net.IPv4len
fd, err := getFd(destination.Network, ipv6)
if err != nil {
return nil, err
}
if !fdProtector.Protect(int32(fd)) {
return nil, errors.New("protect failed")
}
if sockopt != nil {
internet.ApplySockopt(sockopt, destination, uintptr(fd), ctx)
}
var sockaddr unix.Sockaddr
if !ipv6 {
socketAddress := &unix.SockaddrInet4{
Port: int(destination.Port),
}
copy(socketAddress.Addr[:], destIp)
sockaddr = socketAddress
} else {
socketAddress := &unix.SockaddrInet6{
Port: int(destination.Port),
}
copy(socketAddress.Addr[:], destIp)
sockaddr = socketAddress
}
err = unix.Connect(fd, sockaddr)
if err != nil {
return nil, err
}
file := os.NewFile(uintptr(fd), "socket")
if file == nil {
return nil, errors.New("failed to connect to fd")
}
switch destination.Network {
case v2rayNet.Network_UDP:
pc, err := net.FilePacketConn(file)
if err == nil {
destAddr, err := net.ResolveUDPAddr("udp", destination.NetAddr())
if err != nil {
return nil, err
}
conn = &internet.PacketConnWrapper{
Conn: pc,
Dest: destAddr,
}
}
default:
conn, err = net.FileConn(file)
}
if err != nil {
return nil, err
}
closeIgnore(file)
return conn, nil
}
func getFd(network v2rayNet.Network, ipv6 bool) (fd int, err error) {
var af int
if !ipv6 {
af = unix.AF_INET
} else {
af = unix.AF_INET6
}
switch network {
case v2rayNet.Network_TCP:
fd, err = unix.Socket(af, unix.SOCK_STREAM, unix.IPPROTO_TCP)
case v2rayNet.Network_UDP:
fd, err = unix.Socket(af, unix.SOCK_DGRAM, unix.IPPROTO_UDP)
case v2rayNet.Network_UNIX:
fd, err = unix.Socket(af, unix.SOCK_STREAM, 0)
default:
err = fmt.Errorf("unknow network")
}
return
}