2018-04-12 09:41:07 +08:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2018-06-08 18:52:36 +08:00
|
|
|
"net/url"
|
2018-04-12 09:41:07 +08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/anacrolix/missinggo"
|
2018-06-15 20:42:05 +08:00
|
|
|
"github.com/anacrolix/missinggo/perf"
|
2018-07-07 09:36:58 +08:00
|
|
|
"golang.org/x/net/proxy"
|
2018-04-12 09:41:07 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type dialer interface {
|
|
|
|
dial(_ context.Context, addr string) (net.Conn, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type socket interface {
|
|
|
|
net.Listener
|
|
|
|
dialer
|
|
|
|
}
|
|
|
|
|
2018-06-08 18:52:36 +08:00
|
|
|
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(network, addr, proxyURL string) (socket, error) {
|
2018-04-12 09:41:07 +08:00
|
|
|
if isTcpNetwork(network) {
|
2018-06-08 18:52:36 +08:00
|
|
|
return listenTcp(network, addr, proxyURL)
|
2018-04-12 09:41:07 +08:00
|
|
|
} else if isUtpNetwork(network) {
|
2018-06-08 18:52:36 +08:00
|
|
|
return listenUtp(network, addr, proxyURL)
|
2018-04-12 09:41:07 +08:00
|
|
|
} else {
|
|
|
|
panic(fmt.Sprintf("unknown network %q", network))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func isTcpNetwork(s string) bool {
|
|
|
|
return strings.Contains(s, "tcp")
|
|
|
|
}
|
|
|
|
|
|
|
|
func isUtpNetwork(s string) bool {
|
|
|
|
return strings.Contains(s, "utp") || strings.Contains(s, "udp")
|
|
|
|
}
|
|
|
|
|
2018-06-08 18:52:36 +08:00
|
|
|
func listenTcp(network, address, proxyURL string) (s socket, err error) {
|
2018-04-12 09:41:07 +08:00
|
|
|
l, err := net.Listen(network, address)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2018-06-15 12:34:58 +08:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
l.Close()
|
|
|
|
}
|
|
|
|
}()
|
2018-06-08 18:52:36 +08:00
|
|
|
|
|
|
|
// 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 {
|
2018-06-15 12:34:58 +08:00
|
|
|
// 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.
|
2018-06-08 18:52:36 +08:00
|
|
|
if dialer, err := getProxyDialer(proxyURL); err == nil {
|
2018-06-15 12:34:58 +08:00
|
|
|
return tcpSocket{l, func(ctx context.Context, addr string) (conn net.Conn, err error) {
|
|
|
|
defer perf.ScopeTimerErr(&err)()
|
|
|
|
return dialer.Dial(network, addr)
|
|
|
|
}}, nil
|
2018-06-08 18:52:36 +08:00
|
|
|
}
|
|
|
|
}
|
2018-06-15 12:34:58 +08:00
|
|
|
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
|
2018-04-12 09:41:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type tcpSocket struct {
|
|
|
|
net.Listener
|
2018-06-15 12:34:58 +08:00
|
|
|
d func(ctx context.Context, addr string) (net.Conn, error)
|
2018-04-12 09:41:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (me tcpSocket) dial(ctx context.Context, addr string) (net.Conn, error) {
|
2018-06-15 12:34:58 +08:00
|
|
|
return me.d(ctx, addr)
|
2018-04-12 09:41:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func setPort(addr string, port int) string {
|
|
|
|
host, _, err := net.SplitHostPort(addr)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return net.JoinHostPort(host, strconv.FormatInt(int64(port), 10))
|
|
|
|
}
|
|
|
|
|
2018-06-08 18:52:36 +08:00
|
|
|
func listenAll(networks []string, getHost func(string) string, port int, proxyURL string) ([]socket, error) {
|
2018-04-12 09:41:07 +08:00
|
|
|
if len(networks) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2018-04-12 13:06:53 +08:00
|
|
|
var nahs []networkAndHost
|
|
|
|
for _, n := range networks {
|
|
|
|
nahs = append(nahs, networkAndHost{n, getHost(n)})
|
|
|
|
}
|
2018-04-12 09:41:07 +08:00
|
|
|
for {
|
2018-06-08 18:52:36 +08:00
|
|
|
ss, retry, err := listenAllRetry(nahs, port, proxyURL)
|
2018-04-12 09:41:07 +08:00
|
|
|
if !retry {
|
|
|
|
return ss, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-12 13:06:53 +08:00
|
|
|
type networkAndHost struct {
|
|
|
|
Network string
|
|
|
|
Host string
|
|
|
|
}
|
|
|
|
|
2018-06-08 18:52:36 +08:00
|
|
|
func listenAllRetry(nahs []networkAndHost, port int, proxyURL string) (ss []socket, retry bool, err error) {
|
2018-04-12 13:06:53 +08:00
|
|
|
ss = make([]socket, 1, len(nahs))
|
|
|
|
portStr := strconv.FormatInt(int64(port), 10)
|
2018-06-08 18:52:36 +08:00
|
|
|
ss[0], err = listen(nahs[0].Network, net.JoinHostPort(nahs[0].Host, portStr), proxyURL)
|
2018-04-12 09:41:07 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, fmt.Errorf("first listen: %s", err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil || retry {
|
|
|
|
for _, s := range ss {
|
|
|
|
s.Close()
|
|
|
|
}
|
|
|
|
ss = nil
|
|
|
|
}
|
|
|
|
}()
|
2018-04-12 13:06:53 +08:00
|
|
|
portStr = strconv.FormatInt(int64(missinggo.AddrPort(ss[0].Addr())), 10)
|
|
|
|
for _, nah := range nahs[1:] {
|
2018-06-08 18:52:36 +08:00
|
|
|
s, err := listen(nah.Network, net.JoinHostPort(nah.Host, portStr), proxyURL)
|
2018-04-12 09:41:07 +08:00
|
|
|
if err != nil {
|
|
|
|
return ss,
|
|
|
|
missinggo.IsAddrInUse(err) && port == 0,
|
|
|
|
fmt.Errorf("subsequent listen: %s", err)
|
|
|
|
}
|
|
|
|
ss = append(ss, s)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-08 18:52:36 +08:00
|
|
|
func listenUtp(network, addr, proxyURL string) (s socket, err error) {
|
2018-04-12 09:41:07 +08:00
|
|
|
us, err := NewUtpSocket(network, addr)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2018-06-08 18:52:36 +08:00
|
|
|
|
|
|
|
// 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
|
2018-04-12 09:41:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type utpSocketSocket struct {
|
|
|
|
utpSocket
|
|
|
|
network string
|
2018-06-08 18:52:36 +08:00
|
|
|
d proxy.Dialer
|
2018-04-12 09:41:07 +08:00
|
|
|
}
|
|
|
|
|
2018-06-15 20:42:05 +08:00
|
|
|
func (me utpSocketSocket) dial(ctx context.Context, addr string) (conn net.Conn, err error) {
|
|
|
|
defer perf.ScopeTimerErr(&err)()
|
2018-06-08 18:52:36 +08:00
|
|
|
if me.d != nil {
|
|
|
|
return me.d.Dial(me.network, addr)
|
|
|
|
}
|
|
|
|
|
2018-04-12 09:41:07 +08:00
|
|
|
return me.utpSocket.DialContext(ctx, me.network, addr)
|
|
|
|
}
|