Skip to content

Commit

Permalink
ignore udp bind address error
Browse files Browse the repository at this point in the history
  • Loading branch information
txthinking committed Aug 1, 2020
1 parent 0424ecb commit 7655702
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 6 deletions.
7 changes: 6 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"io"
"log"
"net"
"strings"
"time"

"github.com/txthinking/brook/limits"
Expand Down Expand Up @@ -272,6 +273,10 @@ func (x *Client) UDPHandle(s *socks5.Server, addr *net.UDPAddr, d *socks5.Datagr
}
rc, err := Dial.DialUDP("udp", laddr, raddr)
if err != nil {
if strings.Contains(err.Error(), "address already in use") {
// we dont choose lock, so ignore this error
return nil
}
return err
}
if laddr == nil {
Expand All @@ -288,8 +293,8 @@ func (x *Client) UDPHandle(s *socks5.Server, addr *net.UDPAddr, d *socks5.Datagr
s.UDPExchanges.Set(src+dst, ue, -1)
go func(ue *socks5.UDPExchange, dst string) {
defer func() {
s.UDPExchanges.Delete(ue.ClientAddr.String() + dst)
ue.RemoteConn.Close()
s.UDPExchanges.Delete(ue.ClientAddr.String() + dst)
}()
var b [65535]byte
for {
Expand Down
7 changes: 6 additions & 1 deletion relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package brook
import (
"log"
"net"
"strings"
"time"

cache "github.com/patrickmn/go-cache"
Expand Down Expand Up @@ -250,6 +251,10 @@ func (s *Relay) UDPHandle(addr *net.UDPAddr, b []byte) error {
}
rc, err := Dial.DialUDP("udp", laddr, s.RemoteUDPAddr)
if err != nil {
if strings.Contains(err.Error(), "address already in use") {
// we dont choose lock, so ignore this error
return nil
}
return err
}
if laddr == nil {
Expand All @@ -266,8 +271,8 @@ func (s *Relay) UDPHandle(addr *net.UDPAddr, b []byte) error {
s.UDPExchanges.Set(src+dst, ue, -1)
go func(ue *socks5.UDPExchange, dst string) {
defer func() {
s.UDPExchanges.Delete(ue.ClientAddr.String() + dst)
ue.RemoteConn.Close()
s.UDPExchanges.Delete(ue.ClientAddr.String() + dst)
}()
var b [65535]byte
for {
Expand Down
7 changes: 6 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"io"
"log"
"net"
"strings"
"time"

cache "github.com/patrickmn/go-cache"
Expand Down Expand Up @@ -334,6 +335,10 @@ func (s *Server) UDPHandle(addr *net.UDPAddr, b []byte) error {
}
rc, err := Dial.DialUDP("udp", laddr, raddr)
if err != nil {
if strings.Contains(err.Error(), "address already in use") {
// we dont choose lock, so ignore this error
return nil
}
return err
}
if laddr == nil {
Expand All @@ -354,8 +359,8 @@ func (s *Server) UDPHandle(addr *net.UDPAddr, b []byte) error {
s.UDPExchanges.Set(src+dst, ue, cache.DefaultExpiration)
go func(ue *ServerUDPExchange, dst string) {
defer func() {
s.UDPExchanges.Delete(ue.ClientAddr.String() + dst)
ue.RemoteConn.Close()
s.UDPExchanges.Delete(ue.ClientAddr.String() + dst)
if ue.Internet != nil {
ue.Internet.Close()
}
Expand Down
6 changes: 5 additions & 1 deletion tproxy_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,10 @@ func (s *Tproxy) UDPHandle(addr, daddr *net.UDPAddr, b []byte) error {
}
rc, err := Dial.DialUDP("udp", laddr, s.RemoteUDPAddr)
if err != nil {
if strings.Contains(err.Error(), "address already in use") {
// we dont choose lock, so ignore this error
return nil
}
return err
}
if !ok {
Expand All @@ -477,9 +481,9 @@ func (s *Tproxy) UDPHandle(addr, daddr *net.UDPAddr, b []byte) error {
s.UDPExchanges.Set(src+dst, ue, -1)
go func(ue *TproxyUDPExchange, dst string) {
defer func() {
s.UDPExchanges.Delete(ue.LocalConn.RemoteAddr().String() + dst)
ue.RemoteConn.Close()
ue.LocalConn.Close()
s.UDPExchanges.Delete(ue.LocalConn.RemoteAddr().String() + dst)
}()
var b [65535]byte
for {
Expand Down
7 changes: 6 additions & 1 deletion tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"io"
"log"
"net"
"strings"
"time"

cache "github.com/patrickmn/go-cache"
Expand Down Expand Up @@ -302,6 +303,10 @@ func (s *Tunnel) UDPHandle(addr *net.UDPAddr, b []byte) error {
}
rc, err := Dial.DialUDP("udp", laddr, s.RemoteUDPAddr)
if err != nil {
if strings.Contains(err.Error(), "address already in use") {
// we dont choose lock, so ignore this error
return nil
}
return err
}
if laddr == nil {
Expand All @@ -318,8 +323,8 @@ func (s *Tunnel) UDPHandle(addr *net.UDPAddr, b []byte) error {
s.UDPExchanges.Set(src+dst, ue, -1)
go func(ue *socks5.UDPExchange, dst string) {
defer func() {
s.UDPExchanges.Delete(ue.ClientAddr.String() + dst)
ue.RemoteConn.Close()
s.UDPExchanges.Delete(ue.ClientAddr.String() + dst)
}()
var b [65535]byte
for {
Expand Down
7 changes: 6 additions & 1 deletion wsclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"log"
"net"
"net/url"
"strings"
"time"

cache "github.com/patrickmn/go-cache"
Expand Down Expand Up @@ -406,6 +407,10 @@ func (x *WSClient) UDPHandle(s *socks5.Server, addr *net.UDPAddr, d *socks5.Data
}
rc, err := x.DialWebsocket(lsrc)
if err != nil {
if strings.Contains(err.Error(), "address already in use") {
// we dont choose lock, so ignore this error
return nil
}
return err
}
if laddr == nil {
Expand Down Expand Up @@ -443,8 +448,8 @@ func (x *WSClient) UDPHandle(s *socks5.Server, addr *net.UDPAddr, d *socks5.Data
x.UDPExchanges.Set(src+dst, ue, -1)
go func(ue *WSClientUDPExchange, dst string) {
defer func() {
x.UDPExchanges.Delete(ue.ClientAddr.String() + dst)
ue.RemoteConn.Close()
x.UDPExchanges.Delete(ue.ClientAddr.String() + dst)
}()
for {
select {
Expand Down

0 comments on commit 7655702

Please sign in to comment.