166 lines
4.0 KiB
Go
166 lines
4.0 KiB
Go
package torrent
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"net/url"
|
|
"strconv"
|
|
|
|
"github.com/anacrolix/missinggo"
|
|
"github.com/anacrolix/missinggo/perf"
|
|
"github.com/pkg/errors"
|
|
"golang.org/x/net/proxy"
|
|
)
|
|
|
|
type dialer interface {
|
|
dial(_ context.Context, addr string) (net.Conn, error)
|
|
}
|
|
|
|
type socket interface {
|
|
net.Listener
|
|
dialer
|
|
}
|
|
|
|
func getProxyDialer(proxyURL string) (proxy.Dialer, error) {
|
|
fixedURL, err := url.Parse(proxyURL)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return proxy.FromURL(fixedURL, proxy.Direct)
|
|
}
|
|
|
|
func listen(n network, addr, proxyURL string, f firewallCallback) (socket, error) {
|
|
switch {
|
|
case n.Tcp:
|
|
return listenTcp(n.String(), addr, proxyURL)
|
|
case n.Udp:
|
|
return listenUtp(n.String(), addr, proxyURL, f)
|
|
default:
|
|
panic(n)
|
|
}
|
|
}
|
|
|
|
func listenTcp(network, address, proxyURL string) (s socket, err error) {
|
|
l, err := net.Listen(network, address)
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer func() {
|
|
if err != nil {
|
|
l.Close()
|
|
}
|
|
}()
|
|
|
|
// If we don't need the proxy - then we should return default net.Dialer,
|
|
// otherwise, let's try to parse the proxyURL and return proxy.Dialer
|
|
if len(proxyURL) != 0 {
|
|
// TODO: The error should be propagated, as proxy may be in use for
|
|
// security or privacy reasons. Also just pass proxy.Dialer in from
|
|
// the Config.
|
|
if dialer, err := getProxyDialer(proxyURL); err == nil {
|
|
return tcpSocket{l, func(ctx context.Context, addr string) (conn net.Conn, err error) {
|
|
defer perf.ScopeTimerErr(&err)()
|
|
return dialer.Dial(network, addr)
|
|
}}, nil
|
|
}
|
|
}
|
|
dialer := net.Dialer{}
|
|
return tcpSocket{l, func(ctx context.Context, addr string) (conn net.Conn, err error) {
|
|
defer perf.ScopeTimerErr(&err)()
|
|
return dialer.DialContext(ctx, network, addr)
|
|
}}, nil
|
|
}
|
|
|
|
type tcpSocket struct {
|
|
net.Listener
|
|
d func(ctx context.Context, addr string) (net.Conn, error)
|
|
}
|
|
|
|
func (me tcpSocket) dial(ctx context.Context, addr string) (net.Conn, error) {
|
|
return me.d(ctx, addr)
|
|
}
|
|
|
|
func listenAll(networks []network, getHost func(string) string, port int, proxyURL string, f firewallCallback) ([]socket, error) {
|
|
if len(networks) == 0 {
|
|
return nil, nil
|
|
}
|
|
var nahs []networkAndHost
|
|
for _, n := range networks {
|
|
nahs = append(nahs, networkAndHost{n, getHost(n.String())})
|
|
}
|
|
for {
|
|
ss, retry, err := listenAllRetry(nahs, port, proxyURL, f)
|
|
if !retry {
|
|
return ss, err
|
|
}
|
|
}
|
|
}
|
|
|
|
type networkAndHost struct {
|
|
Network network
|
|
Host string
|
|
}
|
|
|
|
func listenAllRetry(nahs []networkAndHost, port int, proxyURL string, f firewallCallback) (ss []socket, retry bool, err error) {
|
|
ss = make([]socket, 1, len(nahs))
|
|
portStr := strconv.FormatInt(int64(port), 10)
|
|
ss[0], err = listen(nahs[0].Network, net.JoinHostPort(nahs[0].Host, portStr), proxyURL, f)
|
|
if err != nil {
|
|
return nil, false, errors.Wrap(err, "first listen")
|
|
}
|
|
defer func() {
|
|
if err != nil || retry {
|
|
for _, s := range ss {
|
|
s.Close()
|
|
}
|
|
ss = nil
|
|
}
|
|
}()
|
|
portStr = strconv.FormatInt(int64(missinggo.AddrPort(ss[0].Addr())), 10)
|
|
for _, nah := range nahs[1:] {
|
|
s, err := listen(nah.Network, net.JoinHostPort(nah.Host, portStr), proxyURL, f)
|
|
if err != nil {
|
|
return ss,
|
|
missinggo.IsAddrInUse(err) && port == 0,
|
|
errors.Wrap(err, "subsequent listen")
|
|
}
|
|
ss = append(ss, s)
|
|
}
|
|
return
|
|
}
|
|
|
|
type firewallCallback func(net.Addr) bool
|
|
|
|
func listenUtp(network, addr, proxyURL string, fc firewallCallback) (s socket, err error) {
|
|
us, err := NewUtpSocket(network, addr, fc)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
// If we don't need the proxy - then we should return default net.Dialer,
|
|
// otherwise, let's try to parse the proxyURL and return proxy.Dialer
|
|
if len(proxyURL) != 0 {
|
|
if dialer, err := getProxyDialer(proxyURL); err == nil {
|
|
return utpSocketSocket{us, network, dialer}, nil
|
|
}
|
|
}
|
|
|
|
return utpSocketSocket{us, network, nil}, nil
|
|
}
|
|
|
|
type utpSocketSocket struct {
|
|
utpSocket
|
|
network string
|
|
d proxy.Dialer
|
|
}
|
|
|
|
func (me utpSocketSocket) dial(ctx context.Context, addr string) (conn net.Conn, err error) {
|
|
defer perf.ScopeTimerErr(&err)()
|
|
if me.d != nil {
|
|
return me.d.Dial(me.network, addr)
|
|
}
|
|
|
|
return me.utpSocket.DialContext(ctx, me.network, addr)
|
|
}
|