2018-04-12 01:41:07 +00:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
|
2021-12-12 10:32:29 +00:00
|
|
|
utp "github.com/anacrolix/go-libutp"
|
2018-06-15 12:42:05 +00:00
|
|
|
"github.com/anacrolix/missinggo/perf"
|
2021-06-23 07:24:50 +00:00
|
|
|
"github.com/anacrolix/missinggo/v2"
|
2018-11-19 09:35:21 +00:00
|
|
|
"github.com/pkg/errors"
|
2018-04-12 01:41:07 +00:00
|
|
|
)
|
|
|
|
|
2020-02-20 05:47:37 +00:00
|
|
|
type Listener interface {
|
2020-11-08 23:56:27 +00:00
|
|
|
// Accept waits for and returns the next connection to the listener.
|
|
|
|
Accept() (net.Conn, error)
|
|
|
|
|
|
|
|
// Addr returns the listener's network address.
|
|
|
|
Addr() net.Addr
|
2018-04-12 01:41:07 +00:00
|
|
|
}
|
|
|
|
|
2020-02-19 23:57:02 +00:00
|
|
|
type socket interface {
|
2020-02-20 05:47:37 +00:00
|
|
|
Listener
|
|
|
|
Dialer
|
2020-11-08 23:56:27 +00:00
|
|
|
Close() error
|
2018-06-08 10:52:36 +00:00
|
|
|
}
|
|
|
|
|
2020-02-19 23:57:02 +00:00
|
|
|
func listen(n network, addr string, f firewallCallback) (socket, error) {
|
2018-11-27 23:30:21 +00:00
|
|
|
switch {
|
|
|
|
case n.Tcp:
|
2020-02-19 23:57:02 +00:00
|
|
|
return listenTcp(n.String(), addr)
|
2018-11-27 23:30:21 +00:00
|
|
|
case n.Udp:
|
2020-02-19 23:57:02 +00:00
|
|
|
return listenUtp(n.String(), addr, f)
|
2018-11-27 23:30:21 +00:00
|
|
|
default:
|
|
|
|
panic(n)
|
2018-04-12 01:41:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-19 23:57:02 +00:00
|
|
|
func listenTcp(network, address string) (s socket, err error) {
|
2018-04-12 01:41:07 +00:00
|
|
|
l, err := net.Listen(network, address)
|
2020-02-19 23:57:02 +00:00
|
|
|
return tcpSocket{
|
|
|
|
Listener: l,
|
2021-06-21 03:29:26 +00:00
|
|
|
NetworkDialer: NetworkDialer{
|
2020-02-20 05:47:37 +00:00
|
|
|
Network: network,
|
2021-06-21 03:29:26 +00:00
|
|
|
Dialer: DefaultNetDialer,
|
2020-02-20 05:47:37 +00:00
|
|
|
},
|
2020-02-19 23:57:02 +00:00
|
|
|
}, err
|
2018-04-12 01:41:07 +00:00
|
|
|
}
|
|
|
|
|
2020-02-19 23:57:02 +00:00
|
|
|
type tcpSocket struct {
|
2019-12-22 08:19:16 +00:00
|
|
|
net.Listener
|
2021-06-21 03:29:26 +00:00
|
|
|
NetworkDialer
|
2019-12-22 08:19:16 +00:00
|
|
|
}
|
|
|
|
|
2020-02-19 23:57:02 +00:00
|
|
|
func listenAll(networks []network, getHost func(string) string, port int, f firewallCallback) ([]socket, error) {
|
2018-04-12 01:41:07 +00:00
|
|
|
if len(networks) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2018-04-12 05:06:53 +00:00
|
|
|
var nahs []networkAndHost
|
|
|
|
for _, n := range networks {
|
2018-11-27 23:30:21 +00:00
|
|
|
nahs = append(nahs, networkAndHost{n, getHost(n.String())})
|
2018-04-12 05:06:53 +00:00
|
|
|
}
|
2018-04-12 01:41:07 +00:00
|
|
|
for {
|
2020-02-19 23:57:02 +00:00
|
|
|
ss, retry, err := listenAllRetry(nahs, port, f)
|
2018-04-12 01:41:07 +00:00
|
|
|
if !retry {
|
|
|
|
return ss, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-12 05:06:53 +00:00
|
|
|
type networkAndHost struct {
|
2018-11-27 23:30:21 +00:00
|
|
|
Network network
|
2018-04-12 05:06:53 +00:00
|
|
|
Host string
|
|
|
|
}
|
|
|
|
|
2020-02-19 23:57:02 +00:00
|
|
|
func listenAllRetry(nahs []networkAndHost, port int, f firewallCallback) (ss []socket, retry bool, err error) {
|
2018-04-12 05:06:53 +00:00
|
|
|
ss = make([]socket, 1, len(nahs))
|
|
|
|
portStr := strconv.FormatInt(int64(port), 10)
|
2020-02-19 23:57:02 +00:00
|
|
|
ss[0], err = listen(nahs[0].Network, net.JoinHostPort(nahs[0].Host, portStr), f)
|
2018-04-12 01:41:07 +00:00
|
|
|
if err != nil {
|
2018-11-19 09:35:21 +00:00
|
|
|
return nil, false, errors.Wrap(err, "first listen")
|
2018-04-12 01:41:07 +00:00
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil || retry {
|
|
|
|
for _, s := range ss {
|
|
|
|
s.Close()
|
|
|
|
}
|
|
|
|
ss = nil
|
|
|
|
}
|
|
|
|
}()
|
2018-04-12 05:06:53 +00:00
|
|
|
portStr = strconv.FormatInt(int64(missinggo.AddrPort(ss[0].Addr())), 10)
|
|
|
|
for _, nah := range nahs[1:] {
|
2020-02-19 23:57:02 +00:00
|
|
|
s, err := listen(nah.Network, net.JoinHostPort(nah.Host, portStr), f)
|
2018-04-12 01:41:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return ss,
|
|
|
|
missinggo.IsAddrInUse(err) && port == 0,
|
2018-11-19 09:35:21 +00:00
|
|
|
errors.Wrap(err, "subsequent listen")
|
2018-04-12 01:41:07 +00:00
|
|
|
}
|
|
|
|
ss = append(ss, s)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-12 10:32:29 +00:00
|
|
|
type firewallCallback = utp.FirewallCallback
|
2018-07-25 07:11:09 +00:00
|
|
|
|
2020-02-19 23:57:02 +00:00
|
|
|
func listenUtp(network, addr string, fc firewallCallback) (socket, error) {
|
2018-07-25 07:11:09 +00:00
|
|
|
us, err := NewUtpSocket(network, addr, fc)
|
2020-02-19 23:57:02 +00:00
|
|
|
return utpSocketSocket{us, network}, err
|
2019-12-22 08:19:16 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 02:54:57 +00:00
|
|
|
// utpSocket wrapper, additionally wrapped for the torrent package's socket interface.
|
2018-04-12 01:41:07 +00:00
|
|
|
type utpSocketSocket struct {
|
|
|
|
utpSocket
|
|
|
|
network string
|
|
|
|
}
|
|
|
|
|
2021-06-21 02:54:57 +00:00
|
|
|
func (me utpSocketSocket) DialerNetwork() string {
|
|
|
|
return me.network
|
|
|
|
}
|
|
|
|
|
2020-02-20 05:47:37 +00:00
|
|
|
func (me utpSocketSocket) Dial(ctx context.Context, addr string) (conn net.Conn, err error) {
|
2018-06-15 12:42:05 +00:00
|
|
|
defer perf.ScopeTimerErr(&err)()
|
2018-04-12 01:41:07 +00:00
|
|
|
return me.utpSocket.DialContext(ctx, me.network, addr)
|
|
|
|
}
|