2018-07-26 00:04:59 +08:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2018-09-16 23:35:37 +08:00
|
|
|
"bufio"
|
2018-07-26 00:04:59 +08:00
|
|
|
"fmt"
|
2018-09-21 11:33:29 +08:00
|
|
|
"net/url"
|
2018-07-26 00:04:59 +08:00
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Dreamacro/clash/adapters/remote"
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
"github.com/Dreamacro/clash/observable"
|
|
|
|
R "github.com/Dreamacro/clash/rules"
|
|
|
|
|
2018-08-12 04:00:34 +08:00
|
|
|
log "github.com/sirupsen/logrus"
|
2018-07-26 00:04:59 +08:00
|
|
|
"gopkg.in/ini.v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
config *Config
|
|
|
|
once sync.Once
|
|
|
|
)
|
|
|
|
|
2018-08-12 02:23:46 +08:00
|
|
|
// General config
|
|
|
|
type General struct {
|
|
|
|
Port int
|
|
|
|
SocksPort int
|
2018-08-12 04:00:34 +08:00
|
|
|
RedirPort int
|
2018-08-12 02:23:46 +08:00
|
|
|
AllowLan bool
|
|
|
|
Mode Mode
|
|
|
|
LogLevel C.LogLevel
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProxyConfig is update proxy schema
|
|
|
|
type ProxyConfig struct {
|
|
|
|
Port *int
|
|
|
|
SocksPort *int
|
2018-08-12 04:00:34 +08:00
|
|
|
RedirPort *int
|
2018-08-12 02:23:46 +08:00
|
|
|
AllowLan *bool
|
|
|
|
}
|
|
|
|
|
2018-07-26 00:04:59 +08:00
|
|
|
// Config is clash config manager
|
|
|
|
type Config struct {
|
|
|
|
general *General
|
|
|
|
rules []C.Rule
|
|
|
|
proxies map[string]C.Proxy
|
|
|
|
lastUpdate time.Time
|
|
|
|
|
|
|
|
event chan<- interface{}
|
2018-08-12 02:23:46 +08:00
|
|
|
reportCh chan interface{}
|
2018-07-26 00:04:59 +08:00
|
|
|
observable *observable.Observable
|
|
|
|
}
|
|
|
|
|
|
|
|
// Event is event of clash config
|
|
|
|
type Event struct {
|
|
|
|
Type string
|
|
|
|
Payload interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Subscribe config stream
|
|
|
|
func (c *Config) Subscribe() observable.Subscription {
|
|
|
|
sub, _ := c.observable.Subscribe()
|
|
|
|
return sub
|
|
|
|
}
|
|
|
|
|
2018-08-12 02:23:46 +08:00
|
|
|
// Report return a channel for collecting report message
|
2018-07-26 00:04:59 +08:00
|
|
|
func (c *Config) Report() chan<- interface{} {
|
2018-08-12 02:23:46 +08:00
|
|
|
return c.reportCh
|
2018-07-26 00:04:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) readConfig() (*ini.File, error) {
|
|
|
|
if _, err := os.Stat(C.ConfigPath); os.IsNotExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ini.LoadSources(
|
2018-09-16 23:35:37 +08:00
|
|
|
ini.LoadOptions{
|
|
|
|
AllowBooleanKeys: true,
|
|
|
|
UnparseableSections: []string{"Rule"},
|
|
|
|
},
|
2018-07-26 00:04:59 +08:00
|
|
|
C.ConfigPath,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse config
|
|
|
|
func (c *Config) Parse() error {
|
|
|
|
cfg, err := c.readConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.parseGeneral(cfg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.parseProxies(cfg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.parseRules(cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Proxies return proxies of clash
|
|
|
|
func (c *Config) Proxies() map[string]C.Proxy {
|
|
|
|
return c.proxies
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rules return rules of clash
|
|
|
|
func (c *Config) Rules() []C.Rule {
|
|
|
|
return c.rules
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetMode change mode of clash
|
|
|
|
func (c *Config) SetMode(mode Mode) {
|
|
|
|
c.general.Mode = mode
|
|
|
|
c.event <- &Event{Type: "mode", Payload: mode}
|
|
|
|
}
|
|
|
|
|
2018-08-04 23:04:16 +08:00
|
|
|
// SetLogLevel change log level of clash
|
|
|
|
func (c *Config) SetLogLevel(level C.LogLevel) {
|
|
|
|
c.general.LogLevel = level
|
|
|
|
c.event <- &Event{Type: "log-level", Payload: level}
|
|
|
|
}
|
|
|
|
|
2018-07-26 00:04:59 +08:00
|
|
|
// General return clash general config
|
|
|
|
func (c *Config) General() General {
|
|
|
|
return *c.general
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateRules is a function for hot reload rules
|
|
|
|
func (c *Config) UpdateRules() error {
|
|
|
|
cfg, err := c.readConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.parseRules(cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) parseGeneral(cfg *ini.File) error {
|
|
|
|
general := cfg.Section("General")
|
|
|
|
|
2018-08-12 02:23:46 +08:00
|
|
|
port := general.Key("port").RangeInt(0, 1, 65535)
|
|
|
|
socksPort := general.Key("socks-port").RangeInt(0, 1, 65535)
|
2018-08-12 04:00:34 +08:00
|
|
|
redirPort := general.Key("redir-port").RangeInt(0, 1, 65535)
|
2018-07-26 00:04:59 +08:00
|
|
|
allowLan := general.Key("allow-lan").MustBool()
|
|
|
|
logLevelString := general.Key("log-level").MustString(C.INFO.String())
|
|
|
|
modeString := general.Key("mode").MustString(Rule.String())
|
|
|
|
|
|
|
|
mode, exist := ModeMapping[modeString]
|
|
|
|
if !exist {
|
|
|
|
return fmt.Errorf("General.mode value invalid")
|
|
|
|
}
|
|
|
|
|
|
|
|
logLevel, exist := C.LogLevelMapping[logLevelString]
|
|
|
|
if !exist {
|
|
|
|
return fmt.Errorf("General.log-level value invalid")
|
|
|
|
}
|
|
|
|
|
|
|
|
c.general = &General{
|
2018-08-12 02:23:46 +08:00
|
|
|
Port: port,
|
|
|
|
SocksPort: socksPort,
|
2018-08-12 04:00:34 +08:00
|
|
|
RedirPort: redirPort,
|
2018-08-12 02:23:46 +08:00
|
|
|
AllowLan: allowLan,
|
|
|
|
Mode: mode,
|
|
|
|
LogLevel: logLevel,
|
2018-07-26 00:04:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if restAddr := general.Key("external-controller").String(); restAddr != "" {
|
|
|
|
c.event <- &Event{Type: "external-controller", Payload: restAddr}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.UpdateGeneral(*c.general)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateGeneral dispatch update event
|
|
|
|
func (c *Config) UpdateGeneral(general General) {
|
2018-08-12 02:23:46 +08:00
|
|
|
c.UpdateProxy(ProxyConfig{
|
|
|
|
Port: &general.Port,
|
|
|
|
SocksPort: &general.SocksPort,
|
2018-08-12 04:00:34 +08:00
|
|
|
RedirPort: &general.RedirPort,
|
2018-08-12 02:23:46 +08:00
|
|
|
AllowLan: &general.AllowLan,
|
|
|
|
})
|
2018-07-26 00:04:59 +08:00
|
|
|
c.event <- &Event{Type: "mode", Payload: general.Mode}
|
|
|
|
c.event <- &Event{Type: "log-level", Payload: general.LogLevel}
|
|
|
|
}
|
|
|
|
|
2018-08-12 02:23:46 +08:00
|
|
|
// UpdateProxy dispatch update proxy event
|
|
|
|
func (c *Config) UpdateProxy(pc ProxyConfig) {
|
|
|
|
if pc.AllowLan != nil {
|
|
|
|
c.general.AllowLan = *pc.AllowLan
|
|
|
|
}
|
|
|
|
|
2018-08-26 22:43:38 +08:00
|
|
|
c.general.Port = *or(pc.Port, &c.general.Port)
|
|
|
|
if c.general.Port != 0 && (pc.AllowLan != nil || pc.Port != nil) {
|
|
|
|
c.event <- &Event{Type: "http-addr", Payload: genAddr(c.general.Port, c.general.AllowLan)}
|
2018-08-12 02:23:46 +08:00
|
|
|
}
|
|
|
|
|
2018-08-26 22:43:38 +08:00
|
|
|
c.general.SocksPort = *or(pc.SocksPort, &c.general.SocksPort)
|
|
|
|
if c.general.SocksPort != 0 && (pc.AllowLan != nil || pc.SocksPort != nil) {
|
|
|
|
c.event <- &Event{Type: "socks-addr", Payload: genAddr(c.general.SocksPort, c.general.AllowLan)}
|
2018-08-12 02:23:46 +08:00
|
|
|
}
|
2018-08-12 04:00:34 +08:00
|
|
|
|
2018-08-26 22:43:38 +08:00
|
|
|
c.general.RedirPort = *or(pc.RedirPort, &c.general.RedirPort)
|
|
|
|
if c.general.RedirPort != 0 && (pc.AllowLan != nil || pc.RedirPort != nil) {
|
|
|
|
c.event <- &Event{Type: "redir-addr", Payload: genAddr(c.general.RedirPort, c.general.AllowLan)}
|
2018-08-12 04:00:34 +08:00
|
|
|
}
|
2018-08-12 02:23:46 +08:00
|
|
|
}
|
|
|
|
|
2018-07-26 00:04:59 +08:00
|
|
|
func (c *Config) parseProxies(cfg *ini.File) error {
|
|
|
|
proxies := make(map[string]C.Proxy)
|
|
|
|
proxiesConfig := cfg.Section("Proxy")
|
|
|
|
groupsConfig := cfg.Section("Proxy Group")
|
|
|
|
|
|
|
|
// parse proxy
|
|
|
|
for _, key := range proxiesConfig.Keys() {
|
|
|
|
proxy := key.Strings(",")
|
|
|
|
if len(proxy) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch proxy[0] {
|
|
|
|
// ss, server, port, cipter, password
|
|
|
|
case "ss":
|
|
|
|
if len(proxy) < 5 {
|
|
|
|
continue
|
|
|
|
}
|
2018-09-21 11:33:29 +08:00
|
|
|
ssURL := url.URL{
|
|
|
|
Scheme: "ss",
|
|
|
|
User: url.UserPassword(proxy[3], proxy[4]),
|
|
|
|
Host: fmt.Sprintf("%s:%s", proxy[1], proxy[2]),
|
|
|
|
}
|
2018-09-16 23:02:32 +08:00
|
|
|
option := parseOptions(5, proxy...)
|
2018-09-21 11:33:29 +08:00
|
|
|
ss, err := adapters.NewShadowSocks(key.Name(), ssURL.String(), option)
|
2018-07-26 00:04:59 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
proxies[key.Name()] = ss
|
2018-08-12 13:50:54 +08:00
|
|
|
// socks5, server, port
|
|
|
|
case "socks5":
|
|
|
|
if len(proxy) < 3 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
addr := fmt.Sprintf("%s:%s", proxy[1], proxy[2])
|
|
|
|
socks5 := adapters.NewSocks5(key.Name(), addr)
|
|
|
|
proxies[key.Name()] = socks5
|
2018-09-06 10:53:29 +08:00
|
|
|
// vmess, server, port, uuid, alterId, security
|
|
|
|
case "vmess":
|
|
|
|
if len(proxy) < 6 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
addr := fmt.Sprintf("%s:%s", proxy[1], proxy[2])
|
|
|
|
alterID, err := strconv.Atoi(proxy[4])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-09-08 19:53:24 +08:00
|
|
|
option := parseOptions(6, proxy...)
|
|
|
|
vmess, err := adapters.NewVmess(key.Name(), addr, proxy[3], uint16(alterID), proxy[5], option)
|
2018-09-06 10:53:29 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
proxies[key.Name()] = vmess
|
2018-07-26 00:04:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse proxy group
|
|
|
|
for _, key := range groupsConfig.Keys() {
|
|
|
|
rule := strings.Split(key.Value(), ",")
|
|
|
|
rule = trimArr(rule)
|
|
|
|
switch rule[0] {
|
|
|
|
case "url-test":
|
|
|
|
if len(rule) < 4 {
|
|
|
|
return fmt.Errorf("URLTest need more than 4 param")
|
|
|
|
}
|
|
|
|
proxyNames := rule[1 : len(rule)-2]
|
|
|
|
delay, _ := strconv.Atoi(rule[len(rule)-1])
|
|
|
|
url := rule[len(rule)-2]
|
|
|
|
var ps []C.Proxy
|
|
|
|
for _, name := range proxyNames {
|
|
|
|
if p, ok := proxies[name]; ok {
|
|
|
|
ps = append(ps, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
adapter, err := adapters.NewURLTest(key.Name(), ps, url, time.Duration(delay)*time.Second)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Config error: %s", err.Error())
|
|
|
|
}
|
|
|
|
proxies[key.Name()] = adapter
|
|
|
|
case "select":
|
2018-08-12 04:22:14 +08:00
|
|
|
if len(rule) < 2 {
|
|
|
|
return fmt.Errorf("Selector need more than 2 param")
|
2018-07-26 00:04:59 +08:00
|
|
|
}
|
|
|
|
proxyNames := rule[1:]
|
|
|
|
selectProxy := make(map[string]C.Proxy)
|
|
|
|
for _, name := range proxyNames {
|
|
|
|
proxy, exist := proxies[name]
|
|
|
|
if !exist {
|
|
|
|
return fmt.Errorf("Proxy %s not exist", name)
|
|
|
|
}
|
|
|
|
selectProxy[name] = proxy
|
|
|
|
}
|
|
|
|
selector, err := adapters.NewSelector(key.Name(), selectProxy)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Selector create error: %s", err.Error())
|
|
|
|
}
|
|
|
|
proxies[key.Name()] = selector
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// init proxy
|
2018-08-04 23:04:16 +08:00
|
|
|
proxies["GLOBAL"], _ = adapters.NewSelector("GLOBAL", proxies)
|
2018-07-26 00:04:59 +08:00
|
|
|
proxies["DIRECT"] = adapters.NewDirect()
|
|
|
|
proxies["REJECT"] = adapters.NewReject()
|
|
|
|
|
|
|
|
c.proxies = proxies
|
|
|
|
c.event <- &Event{Type: "proxies", Payload: proxies}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) parseRules(cfg *ini.File) error {
|
|
|
|
rules := []C.Rule{}
|
|
|
|
|
|
|
|
rulesConfig := cfg.Section("Rule")
|
|
|
|
// parse rules
|
2018-09-16 23:35:37 +08:00
|
|
|
reader := bufio.NewReader(strings.NewReader(rulesConfig.Body()))
|
|
|
|
for {
|
|
|
|
line, _, err := reader.ReadLine()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
rule := strings.Split(string(line), ",")
|
2018-07-26 00:04:59 +08:00
|
|
|
if len(rule) < 3 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
rule = trimArr(rule)
|
|
|
|
switch rule[0] {
|
2018-09-09 15:01:46 +08:00
|
|
|
case "DOMAIN":
|
|
|
|
rules = append(rules, R.NewDomain(rule[1], rule[2]))
|
2018-07-26 00:04:59 +08:00
|
|
|
case "DOMAIN-SUFFIX":
|
|
|
|
rules = append(rules, R.NewDomainSuffix(rule[1], rule[2]))
|
|
|
|
case "DOMAIN-KEYWORD":
|
|
|
|
rules = append(rules, R.NewDomainKeyword(rule[1], rule[2]))
|
|
|
|
case "GEOIP":
|
|
|
|
rules = append(rules, R.NewGEOIP(rule[1], rule[2]))
|
|
|
|
case "IP-CIDR", "IP-CIDR6":
|
|
|
|
rules = append(rules, R.NewIPCIDR(rule[1], rule[2]))
|
|
|
|
case "FINAL":
|
|
|
|
rules = append(rules, R.NewFinal(rule[2]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.rules = rules
|
|
|
|
c.event <- &Event{Type: "rules", Payload: rules}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-12 02:23:46 +08:00
|
|
|
func (c *Config) handleResponseMessage() {
|
|
|
|
for elm := range c.reportCh {
|
|
|
|
event := elm.(*Event)
|
2018-07-26 00:04:59 +08:00
|
|
|
switch event.Type {
|
2018-08-12 02:23:46 +08:00
|
|
|
case "http-addr":
|
|
|
|
if event.Payload.(bool) == false {
|
2018-08-27 08:50:27 +08:00
|
|
|
log.Errorf("Listening HTTP proxy at %d error", c.general.Port)
|
2018-08-12 02:23:46 +08:00
|
|
|
c.general.Port = 0
|
|
|
|
}
|
|
|
|
case "socks-addr":
|
|
|
|
if event.Payload.(bool) == false {
|
2018-08-27 08:50:27 +08:00
|
|
|
log.Errorf("Listening SOCKS proxy at %d error", c.general.SocksPort)
|
2018-08-12 02:23:46 +08:00
|
|
|
c.general.SocksPort = 0
|
|
|
|
}
|
2018-08-12 04:00:34 +08:00
|
|
|
case "redir-addr":
|
|
|
|
if event.Payload.(bool) == false {
|
2018-08-27 08:50:27 +08:00
|
|
|
log.Errorf("Listening Redir proxy at %d error", c.general.RedirPort)
|
2018-08-12 04:00:34 +08:00
|
|
|
c.general.RedirPort = 0
|
|
|
|
}
|
2018-07-26 00:04:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newConfig() *Config {
|
|
|
|
event := make(chan interface{})
|
2018-08-12 02:23:46 +08:00
|
|
|
reportCh := make(chan interface{})
|
2018-07-26 00:04:59 +08:00
|
|
|
config := &Config{
|
|
|
|
general: &General{},
|
|
|
|
proxies: make(map[string]C.Proxy),
|
|
|
|
rules: []C.Rule{},
|
|
|
|
lastUpdate: time.Now(),
|
|
|
|
|
|
|
|
event: event,
|
2018-08-12 02:23:46 +08:00
|
|
|
reportCh: reportCh,
|
2018-07-26 00:04:59 +08:00
|
|
|
observable: observable.NewObservable(event),
|
|
|
|
}
|
2018-08-12 02:23:46 +08:00
|
|
|
go config.handleResponseMessage()
|
2018-07-26 00:04:59 +08:00
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2018-08-12 16:18:58 +08:00
|
|
|
// Instance return singleton instance of Config
|
2018-07-26 00:04:59 +08:00
|
|
|
func Instance() *Config {
|
|
|
|
once.Do(func() {
|
|
|
|
config = newConfig()
|
|
|
|
})
|
|
|
|
return config
|
|
|
|
}
|