This repository has been archived by the owner on Apr 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
udp.go
145 lines (113 loc) · 2.87 KB
/
udp.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
package main
import (
"fmt"
"log"
"net"
"sync"
"time"
"github.com/NetchX/shadowsocks-multiuser/socks"
)
const udpBufferSize = 64 * 1024
func udpRemote(instance *Instance, cipher func(net.PacketConn) net.PacketConn) {
socket, err := net.ListenPacket("udp", fmt.Sprintf(":%d", instance.Port))
if err != nil {
log.Printf("Failed to listen UDP on %d: %v", instance.Port, err)
return
}
defer socket.Close()
socket = cipher(socket)
nat := newNAT(1 * time.Minute)
buffer := make([]byte, udpBufferSize)
instance.UDPSocket = socket
for instance.Started {
size, remoteAddress, err := socket.ReadFrom(buffer)
if err != nil {
continue
}
targetAddress := socks.SplitAddr(buffer[:size])
if targetAddress == nil {
continue
}
targetUDPAddress, err := net.ResolveUDPAddr("udp", targetAddress.String())
if err != nil {
continue
}
data := buffer[len(targetAddress):size]
conn := nat.Get(remoteAddress.String())
if conn == nil {
conn, err = net.ListenPacket("udp", "")
if err != nil {
continue
}
nat.Add(instance, conn, socket, remoteAddress)
}
size, err = conn.WriteTo(data, targetUDPAddress)
if err != nil {
continue
}
instance.Bandwidth.IncreaseUpload(uint64(size))
}
}
// NAT struct
type NAT struct {
sync.RWMutex
Map map[string]net.PacketConn
Timeout time.Duration
}
// Get PacketConn
func (nat *NAT) Get(id string) net.PacketConn {
nat.RLock()
defer nat.RUnlock()
return nat.Map[id]
}
// Set PacketConn
func (nat *NAT) Set(id string, conn net.PacketConn) {
nat.Lock()
defer nat.Unlock()
nat.Map[id] = conn
}
// Add NAT
func (nat *NAT) Add(instance *Instance, src, dst net.PacketConn, peer net.Addr) {
nat.Set(peer.String(), src)
go func() {
udpRelay(instance, src, dst, peer, nat.Timeout)
if conn := nat.Delete(peer.String()); conn != nil {
conn.Close()
}
}()
}
// Delete NAT
func (nat *NAT) Delete(id string) net.PacketConn {
nat.Lock()
defer nat.Unlock()
conn, ok := nat.Map[id]
if ok {
delete(nat.Map, id)
return conn
}
return nil
}
func newNAT(timeout time.Duration) *NAT {
nat := NAT{}
nat.Map = make(map[string]net.PacketConn)
nat.Timeout = timeout
return &nat
}
func udpRelay(instance *Instance, src, dst net.PacketConn, target net.Addr, timeout time.Duration) error {
buffer := make([]byte, udpBufferSize)
for {
src.SetReadDeadline(time.Now().Add(timeout))
size, remoteAddress, err := src.ReadFrom(buffer)
if err != nil {
return err
}
sourceAddress := socks.ParseAddr(remoteAddress.String())
copy(buffer[len(sourceAddress):], buffer[:size])
copy(buffer, sourceAddress)
size, err = dst.WriteTo(buffer[:len(sourceAddress)+size], target)
if err != nil {
return err
}
instance.Bandwidth.IncreaseDownload(uint64(size))
}
}