Chore: make the code more semantic

This commit is contained in:
Dreamacro committed 2018-09-30 12:25:52 +08:00
1 parent 220e4f0608
commit 2fd59cb31c
40 files changed
+102 -102

No files matched your search

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