Chore: make the code more semantic
This commit is contained in:
parent
bbfd2f3a71
commit
f73013006a
40 changed files with 102 additions and 102 deletions
|
@ -10,9 +10,9 @@ import (
|
||||||
|
|
||||||
// HTTPAdapter is a adapter for HTTP connection
|
// HTTPAdapter is a adapter for HTTP connection
|
||||||
type HTTPAdapter struct {
|
type HTTPAdapter struct {
|
||||||
addr *C.Addr
|
metadata *C.Metadata
|
||||||
conn net.Conn
|
conn net.Conn
|
||||||
R *http.Request
|
R *http.Request
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close HTTP connection
|
// Close HTTP connection
|
||||||
|
@ -20,9 +20,9 @@ func (h *HTTPAdapter) Close() {
|
||||||
h.conn.Close()
|
h.conn.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Addr return destination address
|
// Metadata return destination metadata
|
||||||
func (h *HTTPAdapter) Addr() *C.Addr {
|
func (h *HTTPAdapter) Metadata() *C.Metadata {
|
||||||
return h.addr
|
return h.metadata
|
||||||
}
|
}
|
||||||
|
|
||||||
// Conn return raw net.Conn of HTTP
|
// Conn return raw net.Conn of HTTP
|
||||||
|
@ -33,9 +33,9 @@ func (h *HTTPAdapter) Conn() net.Conn {
|
||||||
// NewHTTP is HTTPAdapter generator
|
// NewHTTP is HTTPAdapter generator
|
||||||
func NewHTTP(request *http.Request, conn net.Conn) *HTTPAdapter {
|
func NewHTTP(request *http.Request, conn net.Conn) *HTTPAdapter {
|
||||||
return &HTTPAdapter{
|
return &HTTPAdapter{
|
||||||
addr: parseHTTPAddr(request),
|
metadata: parseHTTPAddr(request),
|
||||||
R: request,
|
R: request,
|
||||||
conn: conn,
|
conn: conn,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,7 @@ import (
|
||||||
// NewHTTPS is HTTPAdapter generator
|
// NewHTTPS is HTTPAdapter generator
|
||||||
func NewHTTPS(request *http.Request, conn net.Conn) *SocketAdapter {
|
func NewHTTPS(request *http.Request, conn net.Conn) *SocketAdapter {
|
||||||
return &SocketAdapter{
|
return &SocketAdapter{
|
||||||
addr: parseHTTPAddr(request),
|
metadata: parseHTTPAddr(request),
|
||||||
conn: conn,
|
conn: conn,
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -9,8 +9,8 @@ import (
|
||||||
|
|
||||||
// SocketAdapter is a adapter for socks and redir connection
|
// SocketAdapter is a adapter for socks and redir connection
|
||||||
type SocketAdapter struct {
|
type SocketAdapter struct {
|
||||||
conn net.Conn
|
conn net.Conn
|
||||||
addr *C.Addr
|
metadata *C.Metadata
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close socks and redir connection
|
// Close socks and redir connection
|
||||||
|
@ -18,9 +18,9 @@ func (s *SocketAdapter) Close() {
|
||||||
s.conn.Close()
|
s.conn.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Addr return destination address
|
// Metadata return destination metadata
|
||||||
func (s *SocketAdapter) Addr() *C.Addr {
|
func (s *SocketAdapter) Metadata() *C.Metadata {
|
||||||
return s.addr
|
return s.metadata
|
||||||
}
|
}
|
||||||
|
|
||||||
// Conn return raw net.Conn
|
// Conn return raw net.Conn
|
||||||
|
@ -31,7 +31,7 @@ func (s *SocketAdapter) Conn() net.Conn {
|
||||||
// NewSocket is SocketAdapter generator
|
// NewSocket is SocketAdapter generator
|
||||||
func NewSocket(target socks.Addr, conn net.Conn) *SocketAdapter {
|
func NewSocket(target socks.Addr, conn net.Conn) *SocketAdapter {
|
||||||
return &SocketAdapter{
|
return &SocketAdapter{
|
||||||
conn: conn,
|
conn: conn,
|
||||||
addr: parseSocksAddr(target),
|
metadata: parseSocksAddr(target),
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -9,7 +9,7 @@ import (
|
||||||
"github.com/Dreamacro/go-shadowsocks2/socks"
|
"github.com/Dreamacro/go-shadowsocks2/socks"
|
||||||
)
|
)
|
||||||
|
|
||||||
func parseSocksAddr(target socks.Addr) *C.Addr {
|
func parseSocksAddr(target socks.Addr) *C.Metadata {
|
||||||
var host, port string
|
var host, port string
|
||||||
var ip net.IP
|
var ip net.IP
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ func parseSocksAddr(target socks.Addr) *C.Addr {
|
||||||
port = strconv.Itoa((int(target[1+net.IPv6len]) << 8) | int(target[1+net.IPv6len+1]))
|
port = strconv.Itoa((int(target[1+net.IPv6len]) << 8) | int(target[1+net.IPv6len+1]))
|
||||||
}
|
}
|
||||||
|
|
||||||
return &C.Addr{
|
return &C.Metadata{
|
||||||
NetWork: C.TCP,
|
NetWork: C.TCP,
|
||||||
AddrType: int(target[0]),
|
AddrType: int(target[0]),
|
||||||
Host: host,
|
Host: host,
|
||||||
|
@ -38,7 +38,7 @@ func parseSocksAddr(target socks.Addr) *C.Addr {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseHTTPAddr(request *http.Request) *C.Addr {
|
func parseHTTPAddr(request *http.Request) *C.Metadata {
|
||||||
host := request.URL.Hostname()
|
host := request.URL.Hostname()
|
||||||
port := request.URL.Port()
|
port := request.URL.Port()
|
||||||
if port == "" {
|
if port == "" {
|
||||||
|
@ -61,7 +61,7 @@ func parseHTTPAddr(request *http.Request) *C.Addr {
|
||||||
addType = socks.AtypIPv4
|
addType = socks.AtypIPv4
|
||||||
}
|
}
|
||||||
|
|
||||||
return &C.Addr{
|
return &C.Metadata{
|
||||||
NetWork: C.TCP,
|
NetWork: C.TCP,
|
||||||
AddrType: addType,
|
AddrType: addType,
|
||||||
Host: host,
|
Host: host,
|
|
@ -31,8 +31,8 @@ func (d *Direct) Type() C.AdapterType {
|
||||||
return C.Direct
|
return C.Direct
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Direct) Generator(addr *C.Addr) (adapter C.ProxyAdapter, err error) {
|
func (d *Direct) Generator(metadata *C.Metadata) (adapter C.ProxyAdapter, err error) {
|
||||||
c, err := net.Dial("tcp", net.JoinHostPort(addr.String(), addr.Port))
|
c, err := net.Dial("tcp", net.JoinHostPort(metadata.String(), metadata.Port))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
|
@ -37,7 +37,7 @@ func (f *Fallback) Now() string {
|
||||||
return f.proxies[0].RawProxy.Name()
|
return f.proxies[0].RawProxy.Name()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fallback) Generator(addr *C.Addr) (adapter C.ProxyAdapter, err error) {
|
func (f *Fallback) Generator(metadata *C.Metadata) (adapter C.ProxyAdapter, err error) {
|
||||||
idx := 0
|
idx := 0
|
||||||
var proxy *proxy
|
var proxy *proxy
|
||||||
for {
|
for {
|
||||||
|
@ -45,7 +45,7 @@ func (f *Fallback) Generator(addr *C.Addr) (adapter C.ProxyAdapter, err error) {
|
||||||
if proxy == nil {
|
if proxy == nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
adapter, err = proxy.RawProxy.Generator(addr)
|
adapter, err = proxy.RawProxy.Generator(metadata)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
proxy.Valid = false
|
proxy.Valid = false
|
||||||
idx++
|
idx++
|
||||||
|
@ -99,7 +99,7 @@ func (f *Fallback) validTest() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFallback(name string, proxies []C.Proxy, rawURL string, delay time.Duration) (*Fallback, error) {
|
func NewFallback(name string, proxies []C.Proxy, rawURL string, delay time.Duration) (*Fallback, error) {
|
||||||
_, err := urlToAddr(rawURL)
|
_, err := urlToMetadata(rawURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
|
@ -32,7 +32,7 @@ func (r *Reject) Type() C.AdapterType {
|
||||||
return C.Reject
|
return C.Reject
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Reject) Generator(addr *C.Addr) (adapter C.ProxyAdapter, err error) {
|
func (r *Reject) Generator(metadata *C.Metadata) (adapter C.ProxyAdapter, err error) {
|
||||||
return &RejectAdapter{conn: &NopConn{}}, nil
|
return &RejectAdapter{conn: &NopConn{}}, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,8 +21,8 @@ func (s *Selector) Type() C.AdapterType {
|
||||||
return C.Selector
|
return C.Selector
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Selector) Generator(addr *C.Addr) (adapter C.ProxyAdapter, err error) {
|
func (s *Selector) Generator(metadata *C.Metadata) (adapter C.ProxyAdapter, err error) {
|
||||||
return s.selected.Generator(addr)
|
return s.selected.Generator(metadata)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Selector) Now() string {
|
func (s *Selector) Now() string {
|
|
@ -7,7 +7,7 @@ import (
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/common/simple-obfs"
|
"github.com/Dreamacro/clash/component/simple-obfs"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
|
||||||
"github.com/Dreamacro/go-shadowsocks2/core"
|
"github.com/Dreamacro/go-shadowsocks2/core"
|
||||||
|
@ -44,7 +44,7 @@ func (ss *ShadowSocks) Type() C.AdapterType {
|
||||||
return C.Shadowsocks
|
return C.Shadowsocks
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ss *ShadowSocks) Generator(addr *C.Addr) (adapter C.ProxyAdapter, err error) {
|
func (ss *ShadowSocks) Generator(metadata *C.Metadata) (adapter C.ProxyAdapter, err error) {
|
||||||
c, err := net.Dial("tcp", ss.server)
|
c, err := net.Dial("tcp", ss.server)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%s connect error", ss.server)
|
return nil, fmt.Errorf("%s connect error", ss.server)
|
||||||
|
@ -58,7 +58,7 @@ func (ss *ShadowSocks) Generator(addr *C.Addr) (adapter C.ProxyAdapter, err erro
|
||||||
c = obfs.NewHTTPObfs(c, ss.obfsHost, port)
|
c = obfs.NewHTTPObfs(c, ss.obfsHost, port)
|
||||||
}
|
}
|
||||||
c = ss.cipher.StreamConn(c)
|
c = ss.cipher.StreamConn(c)
|
||||||
_, err = c.Write(serializesSocksAddr(addr))
|
_, err = c.Write(serializesSocksAddr(metadata))
|
||||||
return &ShadowsocksAdapter{conn: c}, err
|
return &ShadowsocksAdapter{conn: c}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,21 +102,21 @@ func parseURL(s string) (addr, cipher, password string, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func serializesSocksAddr(addr *C.Addr) []byte {
|
func serializesSocksAddr(metadata *C.Metadata) []byte {
|
||||||
var buf [][]byte
|
var buf [][]byte
|
||||||
aType := uint8(addr.AddrType)
|
aType := uint8(metadata.AddrType)
|
||||||
p, _ := strconv.Atoi(addr.Port)
|
p, _ := strconv.Atoi(metadata.Port)
|
||||||
port := []byte{uint8(p >> 8), uint8(p & 0xff)}
|
port := []byte{uint8(p >> 8), uint8(p & 0xff)}
|
||||||
switch addr.AddrType {
|
switch metadata.AddrType {
|
||||||
case socks.AtypDomainName:
|
case socks.AtypDomainName:
|
||||||
len := uint8(len(addr.Host))
|
len := uint8(len(metadata.Host))
|
||||||
host := []byte(addr.Host)
|
host := []byte(metadata.Host)
|
||||||
buf = [][]byte{{aType, len}, host, port}
|
buf = [][]byte{{aType, len}, host, port}
|
||||||
case socks.AtypIPv4:
|
case socks.AtypIPv4:
|
||||||
host := addr.IP.To4()
|
host := metadata.IP.To4()
|
||||||
buf = [][]byte{{aType}, host, port}
|
buf = [][]byte{{aType}, host, port}
|
||||||
case socks.AtypIPv6:
|
case socks.AtypIPv6:
|
||||||
host := addr.IP.To16()
|
host := metadata.IP.To16()
|
||||||
buf = [][]byte{{aType}, host, port}
|
buf = [][]byte{{aType}, host, port}
|
||||||
}
|
}
|
||||||
return bytes.Join(buf, nil)
|
return bytes.Join(buf, nil)
|
|
@ -39,19 +39,19 @@ func (ss *Socks5) Type() C.AdapterType {
|
||||||
return C.Socks5
|
return C.Socks5
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ss *Socks5) Generator(addr *C.Addr) (adapter C.ProxyAdapter, err error) {
|
func (ss *Socks5) Generator(metadata *C.Metadata) (adapter C.ProxyAdapter, err error) {
|
||||||
c, err := net.Dial("tcp", ss.addr)
|
c, err := net.Dial("tcp", ss.addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%s connect error", ss.addr)
|
return nil, fmt.Errorf("%s connect error", ss.addr)
|
||||||
}
|
}
|
||||||
tcpKeepAlive(c)
|
tcpKeepAlive(c)
|
||||||
if err := ss.shakeHand(addr, c); err != nil {
|
if err := ss.shakeHand(metadata, c); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &Socks5Adapter{conn: c}, nil
|
return &Socks5Adapter{conn: c}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ss *Socks5) shakeHand(addr *C.Addr, rw io.ReadWriter) error {
|
func (ss *Socks5) shakeHand(metadata *C.Metadata, rw io.ReadWriter) error {
|
||||||
buf := make([]byte, socks.MaxAddrLen)
|
buf := make([]byte, socks.MaxAddrLen)
|
||||||
|
|
||||||
// VER, CMD, RSV
|
// VER, CMD, RSV
|
||||||
|
@ -71,7 +71,7 @@ func (ss *Socks5) shakeHand(addr *C.Addr, rw io.ReadWriter) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// VER, CMD, RSV, ADDR
|
// VER, CMD, RSV, ADDR
|
||||||
if _, err := rw.Write(bytes.Join([][]byte{{5, 1, 0}, serializesSocksAddr(addr)}, []byte(""))); err != nil {
|
if _, err := rw.Write(bytes.Join([][]byte{{5, 1, 0}, serializesSocksAddr(metadata)}, []byte(""))); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,8 +28,8 @@ func (u *URLTest) Now() string {
|
||||||
return u.fast.Name()
|
return u.fast.Name()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *URLTest) Generator(addr *C.Addr) (adapter C.ProxyAdapter, err error) {
|
func (u *URLTest) Generator(metadata *C.Metadata) (adapter C.ProxyAdapter, err error) {
|
||||||
return u.fast.Generator(addr)
|
return u.fast.Generator(metadata)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *URLTest) Close() {
|
func (u *URLTest) Close() {
|
||||||
|
@ -84,7 +84,7 @@ func (u *URLTest) speedTest() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewURLTest(name string, proxies []C.Proxy, rawURL string, delay time.Duration) (*URLTest, error) {
|
func NewURLTest(name string, proxies []C.Proxy, rawURL string, delay time.Duration) (*URLTest, error) {
|
||||||
_, err := urlToAddr(rawURL)
|
_, err := urlToMetadata(rawURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
|
@ -12,7 +12,7 @@ import (
|
||||||
|
|
||||||
// DelayTest get the delay for the specified URL
|
// DelayTest get the delay for the specified URL
|
||||||
func DelayTest(proxy C.Proxy, url string) (t int16, err error) {
|
func DelayTest(proxy C.Proxy, url string) (t int16, err error) {
|
||||||
addr, err := urlToAddr(url)
|
addr, err := urlToMetadata(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -43,7 +43,7 @@ func DelayTest(proxy C.Proxy, url string) (t int16, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func urlToAddr(rawURL string) (addr C.Addr, err error) {
|
func urlToMetadata(rawURL string) (addr C.Metadata, err error) {
|
||||||
u, err := url.Parse(rawURL)
|
u, err := url.Parse(rawURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
|
@ -61,7 +61,7 @@ func urlToAddr(rawURL string) (addr C.Addr, err error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
addr = C.Addr{
|
addr = C.Metadata{
|
||||||
AddrType: C.AtypDomainName,
|
AddrType: C.AtypDomainName,
|
||||||
Host: u.Hostname(),
|
Host: u.Hostname(),
|
||||||
IP: nil,
|
IP: nil,
|
|
@ -6,7 +6,7 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/common/vmess"
|
"github.com/Dreamacro/clash/component/vmess"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -38,13 +38,13 @@ func (ss *Vmess) Type() C.AdapterType {
|
||||||
return C.Vmess
|
return C.Vmess
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ss *Vmess) Generator(addr *C.Addr) (adapter C.ProxyAdapter, err error) {
|
func (ss *Vmess) Generator(metadata *C.Metadata) (adapter C.ProxyAdapter, err error) {
|
||||||
c, err := net.Dial("tcp", ss.server)
|
c, err := net.Dial("tcp", ss.server)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%s connect error", ss.server)
|
return nil, fmt.Errorf("%s connect error", ss.server)
|
||||||
}
|
}
|
||||||
tcpKeepAlive(c)
|
tcpKeepAlive(c)
|
||||||
c = ss.client.New(c, parseVmessAddr(addr))
|
c = ss.client.New(c, parseVmessAddr(metadata))
|
||||||
return &VmessAdapter{conn: c}, err
|
return &VmessAdapter{conn: c}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,26 +67,26 @@ func NewVmess(name string, server string, uuid string, alterID uint16, security
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseVmessAddr(info *C.Addr) *vmess.DstAddr {
|
func parseVmessAddr(metadata *C.Metadata) *vmess.DstAddr {
|
||||||
var addrType byte
|
var addrType byte
|
||||||
var addr []byte
|
var addr []byte
|
||||||
switch info.AddrType {
|
switch metadata.AddrType {
|
||||||
case C.AtypIPv4:
|
case C.AtypIPv4:
|
||||||
addrType = byte(vmess.AtypIPv4)
|
addrType = byte(vmess.AtypIPv4)
|
||||||
addr = make([]byte, net.IPv4len)
|
addr = make([]byte, net.IPv4len)
|
||||||
copy(addr[:], info.IP.To4())
|
copy(addr[:], metadata.IP.To4())
|
||||||
case C.AtypIPv6:
|
case C.AtypIPv6:
|
||||||
addrType = byte(vmess.AtypIPv6)
|
addrType = byte(vmess.AtypIPv6)
|
||||||
addr = make([]byte, net.IPv6len)
|
addr = make([]byte, net.IPv6len)
|
||||||
copy(addr[:], info.IP.To16())
|
copy(addr[:], metadata.IP.To16())
|
||||||
case C.AtypDomainName:
|
case C.AtypDomainName:
|
||||||
addrType = byte(vmess.AtypDomainName)
|
addrType = byte(vmess.AtypDomainName)
|
||||||
addr = make([]byte, len(info.Host)+1)
|
addr = make([]byte, len(metadata.Host)+1)
|
||||||
addr[0] = byte(len(info.Host))
|
addr[0] = byte(len(metadata.Host))
|
||||||
copy(addr[1:], []byte(info.Host))
|
copy(addr[1:], []byte(metadata.Host))
|
||||||
}
|
}
|
||||||
|
|
||||||
port, _ := strconv.Atoi(info.Port)
|
port, _ := strconv.Atoi(metadata.Port)
|
||||||
return &vmess.DstAddr{
|
return &vmess.DstAddr{
|
||||||
AddrType: addrType,
|
AddrType: addrType,
|
||||||
Addr: addr,
|
Addr: addr,
|
|
@ -10,9 +10,9 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/adapters/remote"
|
"github.com/Dreamacro/clash/adapters/outbound"
|
||||||
|
"github.com/Dreamacro/clash/common/observable"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
"github.com/Dreamacro/clash/observable"
|
|
||||||
R "github.com/Dreamacro/clash/rules"
|
R "github.com/Dreamacro/clash/rules"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
|
@ -22,14 +22,14 @@ type ProxyAdapter interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
type ServerAdapter interface {
|
type ServerAdapter interface {
|
||||||
Addr() *Addr
|
Metadata() *Metadata
|
||||||
Close()
|
Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
type Proxy interface {
|
type Proxy interface {
|
||||||
Name() string
|
Name() string
|
||||||
Type() AdapterType
|
Type() AdapterType
|
||||||
Generator(addr *Addr) (ProxyAdapter, error)
|
Generator(metadata *Metadata) (ProxyAdapter, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// AdapterType is enum of adapter type
|
// AdapterType is enum of adapter type
|
||||||
|
|
|
@ -28,8 +28,8 @@ func (n *NetWork) String() string {
|
||||||
|
|
||||||
type SourceType int
|
type SourceType int
|
||||||
|
|
||||||
// Addr is used to store connection address
|
// Metadata is used to store connection address
|
||||||
type Addr struct {
|
type Metadata struct {
|
||||||
NetWork NetWork
|
NetWork NetWork
|
||||||
Source SourceType
|
Source SourceType
|
||||||
AddrType int
|
AddrType int
|
||||||
|
@ -38,7 +38,7 @@ type Addr struct {
|
||||||
Port string
|
Port string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (addr *Addr) String() string {
|
func (addr *Metadata) String() string {
|
||||||
if addr.Host == "" {
|
if addr.Host == "" {
|
||||||
return addr.IP.String()
|
return addr.IP.String()
|
||||||
}
|
}
|
|
@ -33,7 +33,7 @@ func (rt RuleType) String() string {
|
||||||
|
|
||||||
type Rule interface {
|
type Rule interface {
|
||||||
RuleType() RuleType
|
RuleType() RuleType
|
||||||
IsMatch(addr *Addr) bool
|
IsMatch(metadata *Metadata) bool
|
||||||
Adapter() string
|
Adapter() string
|
||||||
Payload() string
|
Payload() string
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
A "github.com/Dreamacro/clash/adapters/remote"
|
A "github.com/Dreamacro/clash/adapters/outbound"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
|
||||||
"github.com/go-chi/chi"
|
"github.com/go-chi/chi"
|
||||||
|
|
|
@ -5,7 +5,7 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/adapters/local"
|
"github.com/Dreamacro/clash/adapters/inbound"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
"github.com/Dreamacro/clash/tunnel"
|
"github.com/Dreamacro/clash/tunnel"
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ package redir
|
||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/adapters/local"
|
"github.com/Dreamacro/clash/adapters/inbound"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
"github.com/Dreamacro/clash/tunnel"
|
"github.com/Dreamacro/clash/tunnel"
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ package socks
|
||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/adapters/local"
|
"github.com/Dreamacro/clash/adapters/inbound"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
"github.com/Dreamacro/clash/tunnel"
|
"github.com/Dreamacro/clash/tunnel"
|
||||||
|
|
||||||
|
|
|
@ -15,11 +15,11 @@ func (dk *DomainKeyword) RuleType() C.RuleType {
|
||||||
return C.DomainKeyword
|
return C.DomainKeyword
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dk *DomainKeyword) IsMatch(addr *C.Addr) bool {
|
func (dk *DomainKeyword) IsMatch(metadata *C.Metadata) bool {
|
||||||
if addr.AddrType != C.AtypDomainName {
|
if metadata.AddrType != C.AtypDomainName {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
domain := addr.Host
|
domain := metadata.Host
|
||||||
return strings.Contains(domain, dk.keyword)
|
return strings.Contains(domain, dk.keyword)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,11 +15,11 @@ func (ds *DomainSuffix) RuleType() C.RuleType {
|
||||||
return C.DomainSuffix
|
return C.DomainSuffix
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ds *DomainSuffix) IsMatch(addr *C.Addr) bool {
|
func (ds *DomainSuffix) IsMatch(metadata *C.Metadata) bool {
|
||||||
if addr.AddrType != C.AtypDomainName {
|
if metadata.AddrType != C.AtypDomainName {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
domain := addr.Host
|
domain := metadata.Host
|
||||||
return strings.HasSuffix(domain, "."+ds.suffix) || domain == ds.suffix
|
return strings.HasSuffix(domain, "."+ds.suffix) || domain == ds.suffix
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,11 +13,11 @@ func (d *Domain) RuleType() C.RuleType {
|
||||||
return C.Domain
|
return C.Domain
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Domain) IsMatch(addr *C.Addr) bool {
|
func (d *Domain) IsMatch(metadata *C.Metadata) bool {
|
||||||
if addr.AddrType != C.AtypDomainName {
|
if metadata.AddrType != C.AtypDomainName {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return addr.Host == d.domain
|
return metadata.Host == d.domain
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Domain) Adapter() string {
|
func (d *Domain) Adapter() string {
|
||||||
|
|
|
@ -12,7 +12,7 @@ func (f *Final) RuleType() C.RuleType {
|
||||||
return C.FINAL
|
return C.FINAL
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Final) IsMatch(addr *C.Addr) bool {
|
func (f *Final) IsMatch(metadata *C.Metadata) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -23,11 +23,11 @@ func (g *GEOIP) RuleType() C.RuleType {
|
||||||
return C.GEOIP
|
return C.GEOIP
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GEOIP) IsMatch(addr *C.Addr) bool {
|
func (g *GEOIP) IsMatch(metadata *C.Metadata) bool {
|
||||||
if addr.IP == nil {
|
if metadata.IP == nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
record, _ := mmdb.Country(*addr.IP)
|
record, _ := mmdb.Country(*metadata.IP)
|
||||||
return record.Country.IsoCode == g.country
|
return record.Country.IsoCode == g.country
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,12 +15,12 @@ func (i *IPCIDR) RuleType() C.RuleType {
|
||||||
return C.IPCIDR
|
return C.IPCIDR
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *IPCIDR) IsMatch(addr *C.Addr) bool {
|
func (i *IPCIDR) IsMatch(metadata *C.Metadata) bool {
|
||||||
if addr.IP == nil {
|
if metadata.IP == nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
return i.ipnet.Contains(*addr.IP)
|
return i.ipnet.Contains(*metadata.IP)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *IPCIDR) Adapter() string {
|
func (i *IPCIDR) Adapter() string {
|
||||||
|
|
|
@ -7,7 +7,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/adapters/local"
|
"github.com/Dreamacro/clash/adapters/inbound"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -4,10 +4,10 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
LocalAdapter "github.com/Dreamacro/clash/adapters/local"
|
InboundAdapter "github.com/Dreamacro/clash/adapters/inbound"
|
||||||
|
"github.com/Dreamacro/clash/common/observable"
|
||||||
cfg "github.com/Dreamacro/clash/config"
|
cfg "github.com/Dreamacro/clash/config"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
"github.com/Dreamacro/clash/observable"
|
|
||||||
|
|
||||||
"gopkg.in/eapache/channels.v1"
|
"gopkg.in/eapache/channels.v1"
|
||||||
)
|
)
|
||||||
|
@ -84,7 +84,7 @@ func (t *Tunnel) process() {
|
||||||
|
|
||||||
func (t *Tunnel) handleConn(localConn C.ServerAdapter) {
|
func (t *Tunnel) handleConn(localConn C.ServerAdapter) {
|
||||||
defer localConn.Close()
|
defer localConn.Close()
|
||||||
addr := localConn.Addr()
|
metadata := localConn.Metadata()
|
||||||
|
|
||||||
var proxy C.Proxy
|
var proxy C.Proxy
|
||||||
switch t.mode {
|
switch t.mode {
|
||||||
|
@ -94,9 +94,9 @@ func (t *Tunnel) handleConn(localConn C.ServerAdapter) {
|
||||||
proxy = t.proxies["GLOBAL"]
|
proxy = t.proxies["GLOBAL"]
|
||||||
// Rule
|
// Rule
|
||||||
default:
|
default:
|
||||||
proxy = t.match(addr)
|
proxy = t.match(metadata)
|
||||||
}
|
}
|
||||||
remoConn, err := proxy.Generator(addr)
|
remoConn, err := proxy.Generator(metadata)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.logCh <- newLog(C.WARNING, "Proxy connect error: %s", err.Error())
|
t.logCh <- newLog(C.WARNING, "Proxy connect error: %s", err.Error())
|
||||||
return
|
return
|
||||||
|
@ -104,28 +104,28 @@ func (t *Tunnel) handleConn(localConn C.ServerAdapter) {
|
||||||
defer remoConn.Close()
|
defer remoConn.Close()
|
||||||
|
|
||||||
switch adapter := localConn.(type) {
|
switch adapter := localConn.(type) {
|
||||||
case *LocalAdapter.HTTPAdapter:
|
case *InboundAdapter.HTTPAdapter:
|
||||||
t.handleHTTP(adapter, remoConn)
|
t.handleHTTP(adapter, remoConn)
|
||||||
case *LocalAdapter.SocketAdapter:
|
case *InboundAdapter.SocketAdapter:
|
||||||
t.handleSOCKS(adapter, remoConn)
|
t.handleSOCKS(adapter, remoConn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tunnel) match(addr *C.Addr) C.Proxy {
|
func (t *Tunnel) match(metadata *C.Metadata) C.Proxy {
|
||||||
t.configLock.RLock()
|
t.configLock.RLock()
|
||||||
defer t.configLock.RUnlock()
|
defer t.configLock.RUnlock()
|
||||||
|
|
||||||
for _, rule := range t.rules {
|
for _, rule := range t.rules {
|
||||||
if rule.IsMatch(addr) {
|
if rule.IsMatch(metadata) {
|
||||||
a, ok := t.proxies[rule.Adapter()]
|
a, ok := t.proxies[rule.Adapter()]
|
||||||
if !ok {
|
if !ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
t.logCh <- newLog(C.INFO, "%v match %s using %s", addr.String(), rule.RuleType().String(), rule.Adapter())
|
t.logCh <- newLog(C.INFO, "%v match %s using %s", metadata.String(), rule.RuleType().String(), rule.Adapter())
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
t.logCh <- newLog(C.INFO, "%v doesn't match any rule using DIRECT", addr.String())
|
t.logCh <- newLog(C.INFO, "%v doesn't match any rule using DIRECT", metadata.String())
|
||||||
return t.proxies["DIRECT"]
|
return t.proxies["DIRECT"]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue