-
Notifications
You must be signed in to change notification settings - Fork 10
/
proxy_dialer.go
113 lines (88 loc) · 1.92 KB
/
proxy_dialer.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
package main
import (
"context"
"net"
"strconv"
"time"
C "github.com/doorbash/bridge/constant"
)
type ProxyDialer struct {
proxy C.Proxy
timeout time.Duration
}
func (p *ProxyDialer) Dial(network, addr string) (c net.Conn, err error) {
// log.F("Dial: network: %s, addr: %s\n", network, addr)
host, port, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
metadata := &C.Metadata{
NetWork: C.TCP,
DstPort: port,
}
ip := net.ParseIP(host)
if ip == nil {
metadata.Host = host
metadata.AddrType = C.ATypDomainName
} else {
ipv4 := ip.To4()
if ipv4 != nil {
metadata.DstIP = ipv4
metadata.AddrType = C.ATypIPv4
} else {
metadata.DstIP = ip
metadata.AddrType = C.ATypIPv6
}
}
_, err = strconv.Atoi(port)
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), p.timeout)
defer cancel()
return p.proxy.DialContext(ctx, metadata)
}
func (p *ProxyDialer) DialUDP(network, addr string) (pc net.PacketConn, writeTo net.Addr, err error) {
// log.F("DialUDP: network: %s, addr: %s\n", network, addr)
host, port, err := net.SplitHostPort(addr)
if err != nil {
return nil, nil, err
}
metadata := &C.Metadata{
NetWork: C.UDP,
DstPort: port,
}
ip := net.ParseIP(host)
if ip == nil {
metadata.Host = host
metadata.AddrType = C.ATypDomainName
} else {
ipv4 := ip.To4()
if ipv4 != nil {
metadata.DstIP = ipv4
metadata.AddrType = C.ATypIPv4
} else {
metadata.DstIP = ip
metadata.AddrType = C.ATypIPv6
}
}
prt, err := strconv.Atoi(port)
if err != nil {
return nil, nil, err
}
uaddr := &net.UDPAddr{
IP: metadata.DstIP,
Port: prt,
}
pc, e := p.proxy.DialUDP(metadata)
return pc, uaddr, e
}
func (p *ProxyDialer) Addr() string {
return p.proxy.Addr()
}
func NewProxyDialer(p C.Proxy, timeout time.Duration) (*ProxyDialer, error) {
return &ProxyDialer{
proxy: p,
timeout: timeout,
}, nil
}