Improve: cleanup code

This commit is contained in:
Dreamacro 2018-09-21 11:33:29 +08:00
parent 0caa8e05a3
commit eb778ad6e2
4 changed files with 16 additions and 9 deletions

View File

@ -7,9 +7,9 @@ import (
"net/url" "net/url"
"strconv" "strconv"
"github.com/Dreamacro/clash/common/simple-obfs"
C "github.com/Dreamacro/clash/constant" C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/common/simple-obfs"
"github.com/riobard/go-shadowsocks2/core" "github.com/riobard/go-shadowsocks2/core"
"github.com/riobard/go-shadowsocks2/socks" "github.com/riobard/go-shadowsocks2/socks"
) )
@ -63,9 +63,8 @@ func (ss *ShadowSocks) Generator(addr *C.Addr) (adapter C.ProxyAdapter, err erro
} }
func NewShadowSocks(name string, ssURL string, option map[string]string) (*ShadowSocks, error) { func NewShadowSocks(name string, ssURL string, option map[string]string) (*ShadowSocks, error) {
var key []byte
server, cipher, password, _ := parseURL(ssURL) server, cipher, password, _ := parseURL(ssURL)
ciph, err := core.PickCipher(cipher, key, password) ciph, err := core.PickCipher(cipher, nil, password)
if err != nil { if err != nil {
return nil, fmt.Errorf("ss %s initialize error: %s", server, err.Error()) return nil, fmt.Errorf("ss %s initialize error: %s", server, err.Error())
} }
@ -120,5 +119,5 @@ func serializesSocksAddr(addr *C.Addr) []byte {
host := addr.IP.To16() host := addr.IP.To16()
buf = [][]byte{{aType}, host, port} buf = [][]byte{{aType}, host, port}
} }
return bytes.Join(buf, []byte("")) return bytes.Join(buf, nil)
} }

View File

@ -45,13 +45,13 @@ func (ss *Socks5) Generator(addr *C.Addr) (adapter C.ProxyAdapter, err error) {
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.sharkHand(addr, c); err != nil { if err := ss.shakeHand(addr, c); err != nil {
return nil, err return nil, err
} }
return &Socks5Adapter{conn: c}, nil return &Socks5Adapter{conn: c}, nil
} }
func (ss *Socks5) sharkHand(addr *C.Addr, rw io.ReadWriter) error { func (ss *Socks5) shakeHand(addr *C.Addr, 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) sharkHand(addr *C.Addr, rw io.ReadWriter) error {
} }
// VER, CMD, RSV, ADDR // VER, CMD, RSV, ADDR
if _, err := rw.Write(bytes.Join([][]byte{[]byte{5, 1, 0}, serializesSocksAddr(addr)}, []byte(""))); err != nil { if _, err := rw.Write(bytes.Join([][]byte{{5, 1, 0}, serializesSocksAddr(addr)}, []byte(""))); err != nil {
return err return err
} }

View File

@ -3,6 +3,7 @@ package config
import ( import (
"bufio" "bufio"
"fmt" "fmt"
"net/url"
"os" "os"
"strconv" "strconv"
"strings" "strings"
@ -226,9 +227,13 @@ func (c *Config) parseProxies(cfg *ini.File) error {
if len(proxy) < 5 { if len(proxy) < 5 {
continue continue
} }
ssURL := fmt.Sprintf("ss://%s:%s@%s:%s", proxy[3], proxy[4], proxy[1], proxy[2]) ssURL := url.URL{
Scheme: "ss",
User: url.UserPassword(proxy[3], proxy[4]),
Host: fmt.Sprintf("%s:%s", proxy[1], proxy[2]),
}
option := parseOptions(5, proxy...) option := parseOptions(5, proxy...)
ss, err := adapters.NewShadowSocks(key.Name(), ssURL, option) ss, err := adapters.NewShadowSocks(key.Name(), ssURL.String(), option)
if err != nil { if err != nil {
return err return err
} }

View File

@ -5,6 +5,7 @@ import (
"io" "io"
"net" "net"
"net/http" "net/http"
"time"
"github.com/Dreamacro/clash/adapters/local" "github.com/Dreamacro/clash/adapters/local"
C "github.com/Dreamacro/clash/constant" C "github.com/Dreamacro/clash/constant"
@ -66,9 +67,11 @@ func relay(leftConn, rightConn net.Conn) {
go func() { go func() {
_, err := io.Copy(leftConn, rightConn) _, err := io.Copy(leftConn, rightConn)
leftConn.SetReadDeadline(time.Now())
ch <- err ch <- err
}() }()
io.Copy(rightConn, leftConn) io.Copy(rightConn, leftConn)
rightConn.SetReadDeadline(time.Now())
<-ch <-ch
} }