forked from ccsexyz/shadowsocks-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
udp.go
153 lines (144 loc) · 3.26 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
146
147
148
149
150
151
152
153
package main
import (
"fmt"
"math/rand"
"net"
ss "github.com/ccsexyz/shadowsocks-go/shadowsocks"
)
type relaySession struct {
conn net.Conn
live bool
from *net.UDPAddr
die chan bool
header []byte
}
func (sess *relaySession) Close() {
select {
case <-sess.die:
default:
sess.conn.Close()
close(sess.die)
}
}
func newUDPListener(address string) (conn *net.UDPConn, err error) {
laddr, err := net.ResolveUDPAddr("udp", address)
if err == nil {
conn, err = net.ListenUDP("udp", laddr)
}
return
}
func RunUDPRemoteServer(c *ss.Config) {
conn, err := newUDPListener(c.Localaddr)
if err != nil {
c.Logger.Fatal(err)
}
var pconn net.PacketConn
pconn = ss.NewUDPConn(conn, c)
handle := func(sess *udpSession, b []byte) {
if len(c.Backends) != 0 && c.Type == "ssproxy" {
sess.conn.Write(b)
return
}
_, data, err := ss.ParseAddr(b)
if err != nil {
return
}
sess.conn.Write(data)
}
create := func(b []byte, from net.Addr) (rconn net.Conn, clean func(), header []byte, err error) {
addr, data, err := ss.ParseAddr(b)
if err != nil {
err = fmt.Errorf("unexpected header")
return
}
if len(c.Backends) != 0 && c.Type == "ssproxy" {
v := c.Backends[rand.Int()%len(c.Backends)]
rconn, err = ss.DialUDP(v)
if err != nil {
return
}
rconn.Write(b)
return
}
target := net.JoinHostPort(addr.Host(), addr.Port())
rconn, err = net.Dial("udp", target)
if err != nil {
return
}
hdrlen := len(b) - len(data)
header = make([]byte, hdrlen)
copy(header, b)
rconn.Write(data)
return
}
RunUDPServer(pconn, c, nil, handle, create)
}
func RunMultiUDPRemoteServer(c *ss.Config) {
conn, err := newUDPListener(c.Localaddr)
if err != nil {
c.Logger.Fatal(err)
}
mconn := ss.NewMultiUDPConn(conn, c)
handle := func(sess *udpSession, b []byte) {
_, data, err := ss.ParseAddr(b)
if err != nil {
return
}
sess.conn.Write(data)
}
create := func(b []byte, from net.Addr) (rconn net.Conn, clean func(), header []byte, err error) {
addr, data, err := ss.ParseAddr(b)
if err != nil {
err = fmt.Errorf("unexpected header")
return
}
target := net.JoinHostPort(addr.Host(), addr.Port())
rconn, err = net.Dial("udp", target)
if err != nil {
c.LogD(err)
return
}
hdrlen := len(b) - len(data)
header = make([]byte, hdrlen)
copy(header, b)
rconn.Write(data)
clean = func() {
mconn.RemoveAddr(from)
}
return
}
RunUDPServer(mconn, c, nil, handle, create)
}
func RunUDPLocalServer(c *ss.Config) {
conn, err := newUDPListener(c.Localaddr)
if err != nil {
c.Logger.Fatal(err)
}
check := func(b []byte) bool {
if len(b) < 3 || b[2] != 0 || b[1] != 0 || b[0] != 0 {
return false
}
return true
}
var handle func(*udpSession, []byte)
var create func([]byte, net.Addr) (net.Conn, func(), []byte, error)
handle = func(sess *udpSession, b []byte) {
sess.conn.Write(b[3:])
}
create = func(b []byte, from net.Addr) (rconn net.Conn, clean func(), header []byte, err error) {
var v *ss.Config
if len(c.Backends) != 0 && c.Type == "socksproxy" {
v = c.Backends[rand.Int()%len(c.Backends)]
} else {
v = c
}
rconn, err = ss.DialUDP(v)
if err != nil {
return
}
rconn.Write(b[3:])
header = []byte{0, 0, 0}
return
}
RunUDPServer(conn, c, check, handle, create)
}