Break Change: use yml, which is easier to parse, as the config format

This commit is contained in:
Dreamacro
2018-10-02 15:26:36 +08:00
parent 16c9445459
commit 5c7fa6b18b
12 changed files with 222 additions and 186 deletions

View File

@ -21,6 +21,13 @@ type Fallback struct {
done chan struct{}
}
type FallbackOption struct {
Name string `proxy:"name"`
Proxies []string `proxy:"proxies"`
URL string `proxy:"url"`
Delay int `proxy:"delay"`
}
func (f *Fallback) Name() string {
return f.name
}
@ -98,8 +105,8 @@ func (f *Fallback) validTest() {
wg.Wait()
}
func NewFallback(name string, proxies []C.Proxy, rawURL string, delay time.Duration) (*Fallback, error) {
_, err := urlToMetadata(rawURL)
func NewFallback(option FallbackOption, proxies []C.Proxy) (*Fallback, error) {
_, err := urlToMetadata(option.URL)
if err != nil {
return nil, err
}
@ -108,6 +115,7 @@ func NewFallback(name string, proxies []C.Proxy, rawURL string, delay time.Durat
return nil, errors.New("The number of proxies cannot be 0")
}
delay := time.Duration(option.Delay) * time.Second
warpperProxies := make([]*proxy, len(proxies))
for idx := range proxies {
warpperProxies[idx] = &proxy{
@ -117,9 +125,9 @@ func NewFallback(name string, proxies []C.Proxy, rawURL string, delay time.Durat
}
Fallback := &Fallback{
name: name,
name: option.Name,
proxies: warpperProxies,
rawURL: rawURL,
rawURL: option.URL,
delay: delay,
done: make(chan struct{}),
}

View File

@ -13,6 +13,11 @@ type Selector struct {
proxies map[string]C.Proxy
}
type SelectorOption struct {
Name string `proxy:"name"`
Proxies []string `proxy:"proxies"`
}
func (s *Selector) Name() string {
return s.name
}

View File

@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"net"
"net/url"
"strconv"
"github.com/Dreamacro/clash/component/simple-obfs"
@ -36,6 +35,16 @@ type ShadowSocks struct {
cipher core.Cipher
}
type ShadowSocksOption struct {
Name string `proxy:"name"`
Server string `proxy:"server"`
Port int `proxy:"port"`
Password string `proxy:"password"`
Cipher string `proxy:"cipher"`
Obfs string `proxy:"obfs,omitempty"`
ObfsHost string `proxy:"obfs-host,omitempty"`
}
func (ss *ShadowSocks) Name() string {
return ss.name
}
@ -62,46 +71,30 @@ func (ss *ShadowSocks) Generator(metadata *C.Metadata) (adapter C.ProxyAdapter,
return &ShadowsocksAdapter{conn: c}, err
}
func NewShadowSocks(name string, ssURL string, option map[string]string) (*ShadowSocks, error) {
server, cipher, password, _ := parseURL(ssURL)
func NewShadowSocks(option ShadowSocksOption) (*ShadowSocks, error) {
server := fmt.Sprintf("%s:%d", option.Server, option.Port)
cipher := option.Cipher
password := option.Password
ciph, err := core.PickCipher(cipher, nil, password)
if err != nil {
return nil, fmt.Errorf("ss %s initialize error: %s", server, err.Error())
}
obfs := ""
obfs := option.Obfs
obfsHost := "bing.com"
if value, ok := option["obfs"]; ok {
obfs = value
}
if value, ok := option["obfs-host"]; ok {
obfsHost = value
if option.ObfsHost != "" {
obfsHost = option.ObfsHost
}
return &ShadowSocks{
server: server,
name: name,
name: option.Name,
cipher: ciph,
obfs: obfs,
obfsHost: obfsHost,
}, nil
}
func parseURL(s string) (addr, cipher, password string, err error) {
u, err := url.Parse(s)
if err != nil {
return
}
addr = u.Host
if u.User != nil {
cipher = u.User.Username()
password, _ = u.User.Password()
}
return
}
func serializesSocksAddr(metadata *C.Metadata) []byte {
var buf [][]byte
aType := uint8(metadata.AddrType)

View File

@ -31,6 +31,12 @@ type Socks5 struct {
name string
}
type Socks5Option struct {
Name string `proxy:"name"`
Server string `proxy:"server"`
Port int `proxy:"port"`
}
func (ss *Socks5) Name() string {
return ss.name
}
@ -82,9 +88,9 @@ func (ss *Socks5) shakeHand(metadata *C.Metadata, rw io.ReadWriter) error {
return nil
}
func NewSocks5(name, addr string) *Socks5 {
func NewSocks5(option Socks5Option) *Socks5 {
return &Socks5{
addr: addr,
name: name,
addr: fmt.Sprintf("%s:%d", option.Server, option.Port),
name: option.Name,
}
}

View File

@ -1,6 +1,7 @@
package adapters
import (
"errors"
"sync"
"time"
@ -16,6 +17,13 @@ type URLTest struct {
done chan struct{}
}
type URLTestOption struct {
Name string `proxy:"name"`
Proxies []string `proxy:"proxies"`
URL string `proxy:"url"`
Delay int `proxy:"delay"`
}
func (u *URLTest) Name() string {
return u.name
}
@ -83,16 +91,20 @@ func (u *URLTest) speedTest() {
}
}
func NewURLTest(name string, proxies []C.Proxy, rawURL string, delay time.Duration) (*URLTest, error) {
_, err := urlToMetadata(rawURL)
func NewURLTest(option URLTestOption, proxies []C.Proxy) (*URLTest, error) {
_, err := urlToMetadata(option.URL)
if err != nil {
return nil, err
}
if len(proxies) < 1 {
return nil, errors.New("The number of proxies cannot be 0")
}
delay := time.Duration(option.Delay) * time.Second
urlTest := &URLTest{
name: name,
name: option.Name,
proxies: proxies[:],
rawURL: rawURL,
rawURL: option.URL,
fast: proxies[0],
delay: delay,
done: make(chan struct{}),

View File

@ -34,11 +34,11 @@ func DelayTest(proxy C.Proxy, url string) (t int16, err error) {
ExpectContinueTimeout: 1 * time.Second,
}
client := http.Client{Transport: transport}
req, err := client.Get(url)
resp, err := client.Get(url)
if err != nil {
return
}
req.Body.Close()
resp.Body.Close()
t = int16(time.Since(start) / time.Millisecond)
return
}

View File

@ -30,6 +30,16 @@ type Vmess struct {
client *vmess.Client
}
type VmessOption struct {
Name string `proxy:"name"`
Server string `proxy:"server"`
Port int `proxy:"port"`
UUID string `proxy:"uuid"`
AlterID int `proxy:"alterId"`
Cipher string `proxy:"cipher"`
TLS bool `proxy:"tls,omitempty"`
}
func (ss *Vmess) Name() string {
return ss.name
}
@ -48,21 +58,21 @@ func (ss *Vmess) Generator(metadata *C.Metadata) (adapter C.ProxyAdapter, err er
return &VmessAdapter{conn: c}, err
}
func NewVmess(name string, server string, uuid string, alterID uint16, security string, option map[string]string) (*Vmess, error) {
security = strings.ToLower(security)
func NewVmess(option VmessOption) (*Vmess, error) {
security := strings.ToLower(option.Cipher)
client, err := vmess.NewClient(vmess.Config{
UUID: uuid,
AlterID: alterID,
UUID: option.UUID,
AlterID: uint16(option.AlterID),
Security: security,
TLS: option["tls"] == "true",
TLS: option.TLS,
})
if err != nil {
return nil, err
}
return &Vmess{
name: name,
server: server,
name: option.Name,
server: fmt.Sprintf("%s:%d", option.Server, option.Port),
client: client,
}, nil
}