2018-06-26 16:15:48 +08:00
|
|
|
package proxy
|
2017-08-23 16:35:39 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net"
|
2022-02-16 23:37:25 +08:00
|
|
|
"sort"
|
2018-06-26 16:15:48 +08:00
|
|
|
"strings"
|
2017-08-23 16:35:39 +08:00
|
|
|
)
|
|
|
|
|
2020-11-26 19:21:27 +08:00
|
|
|
var (
|
2021-07-02 01:06:11 +08:00
|
|
|
// ErrNotSupported indicates that the operation is not supported
|
2020-11-26 19:21:27 +08:00
|
|
|
ErrNotSupported = errors.New("not supported")
|
|
|
|
)
|
|
|
|
|
2019-09-18 19:40:14 +08:00
|
|
|
// Dialer is used to create connection.
|
2017-08-23 16:35:39 +08:00
|
|
|
type Dialer interface {
|
2020-04-13 12:26:21 +08:00
|
|
|
TCPDialer
|
|
|
|
UDPDialer
|
|
|
|
}
|
|
|
|
|
2020-04-14 00:17:46 +08:00
|
|
|
// TCPDialer is used to create tcp connection.
|
2020-04-13 12:26:21 +08:00
|
|
|
type TCPDialer interface {
|
2019-09-18 19:40:14 +08:00
|
|
|
// Addr is the dialer's addr
|
2017-08-23 16:35:39 +08:00
|
|
|
Addr() string
|
|
|
|
|
2019-09-18 19:40:14 +08:00
|
|
|
// Dial connects to the given address
|
|
|
|
Dial(network, addr string) (c net.Conn, err error)
|
2020-04-13 12:26:21 +08:00
|
|
|
}
|
|
|
|
|
2020-04-14 00:17:46 +08:00
|
|
|
// UDPDialer is used to create udp PacketConn.
|
2020-04-13 12:26:21 +08:00
|
|
|
type UDPDialer interface {
|
|
|
|
// Addr is the dialer's addr
|
|
|
|
Addr() string
|
2017-08-23 17:45:57 +08:00
|
|
|
|
2019-09-18 19:40:14 +08:00
|
|
|
// DialUDP connects to the given address
|
2022-03-11 19:08:07 +08:00
|
|
|
DialUDP(network, addr string) (pc net.PacketConn, err error)
|
2017-08-23 16:35:39 +08:00
|
|
|
}
|
|
|
|
|
2018-06-26 17:00:13 +08:00
|
|
|
// DialerCreator is a function to create dialers.
|
2018-06-26 16:15:48 +08:00
|
|
|
type DialerCreator func(s string, dialer Dialer) (Dialer, error)
|
|
|
|
|
|
|
|
var (
|
2020-09-23 22:14:18 +08:00
|
|
|
dialerCreators = make(map[string]DialerCreator)
|
2018-06-26 16:15:48 +08:00
|
|
|
)
|
|
|
|
|
2019-09-18 19:40:14 +08:00
|
|
|
// RegisterDialer is used to register a dialer.
|
2018-06-26 16:15:48 +08:00
|
|
|
func RegisterDialer(name string, c DialerCreator) {
|
2020-09-27 19:50:21 +08:00
|
|
|
dialerCreators[strings.ToLower(name)] = c
|
2018-06-26 16:15:48 +08:00
|
|
|
}
|
|
|
|
|
2018-06-26 17:00:13 +08:00
|
|
|
// DialerFromURL calls the registered creator to create dialers.
|
2018-08-20 22:23:00 +08:00
|
|
|
// dialer is the default upstream dialer so cannot be nil, we can use Default when calling this function.
|
2018-03-24 19:57:46 +08:00
|
|
|
func DialerFromURL(s string, dialer Dialer) (Dialer, error) {
|
2018-08-20 22:23:00 +08:00
|
|
|
if dialer == nil {
|
|
|
|
return nil, errors.New("DialerFromURL: dialer cannot be nil")
|
|
|
|
}
|
|
|
|
|
2021-04-20 15:28:52 +08:00
|
|
|
if !strings.Contains(s, "://") {
|
|
|
|
s = s + "://"
|
|
|
|
}
|
|
|
|
|
2020-10-23 22:29:12 +08:00
|
|
|
scheme := s[:strings.Index(s, ":")]
|
|
|
|
c, ok := dialerCreators[strings.ToLower(scheme)]
|
2018-06-26 16:15:48 +08:00
|
|
|
if ok {
|
|
|
|
return c(s, dialer)
|
2017-08-23 16:35:39 +08:00
|
|
|
}
|
|
|
|
|
2020-10-23 22:29:12 +08:00
|
|
|
return nil, errors.New("unknown scheme '" + scheme + "'")
|
2017-08-23 16:35:39 +08:00
|
|
|
}
|
2022-02-15 21:34:55 +08:00
|
|
|
|
|
|
|
// DialerSchemes returns the registered dialer schemes.
|
|
|
|
func DialerSchemes() string {
|
|
|
|
s := make([]string, 0, len(dialerCreators))
|
|
|
|
for name := range dialerCreators {
|
|
|
|
s = append(s, name)
|
|
|
|
}
|
2022-02-16 23:37:25 +08:00
|
|
|
sort.Strings(s)
|
|
|
|
return strings.Join(s, " ")
|
2022-02-15 21:34:55 +08:00
|
|
|
}
|