Improve: cleanup code
This commit is contained in:
parent
0caa8e05a3
commit
eb778ad6e2
4 changed files with 16 additions and 9 deletions
|
@ -7,9 +7,9 @@ import (
|
|||
"net/url"
|
||||
"strconv"
|
||||
|
||||
"github.com/Dreamacro/clash/common/simple-obfs"
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
|
||||
"github.com/Dreamacro/clash/common/simple-obfs"
|
||||
"github.com/riobard/go-shadowsocks2/core"
|
||||
"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) {
|
||||
var key []byte
|
||||
server, cipher, password, _ := parseURL(ssURL)
|
||||
ciph, err := core.PickCipher(cipher, key, password)
|
||||
ciph, err := core.PickCipher(cipher, nil, password)
|
||||
if err != nil {
|
||||
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()
|
||||
buf = [][]byte{{aType}, host, port}
|
||||
}
|
||||
return bytes.Join(buf, []byte(""))
|
||||
return bytes.Join(buf, nil)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
tcpKeepAlive(c)
|
||||
if err := ss.sharkHand(addr, c); err != nil {
|
||||
if err := ss.shakeHand(addr, c); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
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)
|
||||
|
||||
// VER, CMD, RSV
|
||||
|
@ -71,7 +71,7 @@ func (ss *Socks5) sharkHand(addr *C.Addr, rw io.ReadWriter) error {
|
|||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@ package config
|
|||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
@ -226,9 +227,13 @@ func (c *Config) parseProxies(cfg *ini.File) error {
|
|||
if len(proxy) < 5 {
|
||||
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...)
|
||||
ss, err := adapters.NewShadowSocks(key.Name(), ssURL, option)
|
||||
ss, err := adapters.NewShadowSocks(key.Name(), ssURL.String(), option)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/Dreamacro/clash/adapters/local"
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
|
@ -66,9 +67,11 @@ func relay(leftConn, rightConn net.Conn) {
|
|||
|
||||
go func() {
|
||||
_, err := io.Copy(leftConn, rightConn)
|
||||
leftConn.SetReadDeadline(time.Now())
|
||||
ch <- err
|
||||
}()
|
||||
|
||||
io.Copy(rightConn, leftConn)
|
||||
rightConn.SetReadDeadline(time.Now())
|
||||
<-ch
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue