2018-07-15 22:23:20 +08:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
2018-07-26 00:04:59 +08:00
|
|
|
"github.com/Dreamacro/clash/config"
|
2018-07-15 22:23:20 +08:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
"github.com/Dreamacro/clash/proxy/http"
|
2018-08-12 04:00:34 +08:00
|
|
|
"github.com/Dreamacro/clash/proxy/redir"
|
2018-07-15 22:23:20 +08:00
|
|
|
"github.com/Dreamacro/clash/proxy/socks"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
listener *Listener
|
|
|
|
once sync.Once
|
|
|
|
)
|
|
|
|
|
|
|
|
type Listener struct {
|
|
|
|
// signal for update
|
|
|
|
httpSignal *C.ProxySignal
|
|
|
|
socksSignal *C.ProxySignal
|
2018-08-12 04:00:34 +08:00
|
|
|
redirSignal *C.ProxySignal
|
2018-07-15 22:23:20 +08:00
|
|
|
}
|
|
|
|
|
2018-08-12 02:23:46 +08:00
|
|
|
func (l *Listener) updateHTTP(addr string) error {
|
2018-07-15 22:23:20 +08:00
|
|
|
if l.httpSignal != nil {
|
|
|
|
signal := l.httpSignal
|
|
|
|
signal.Done <- struct{}{}
|
|
|
|
<-signal.Closed
|
|
|
|
l.httpSignal = nil
|
|
|
|
}
|
|
|
|
|
2018-08-12 02:23:46 +08:00
|
|
|
signal, err := http.NewHttpProxy(addr)
|
2018-07-15 22:23:20 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
l.httpSignal = signal
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-12 02:23:46 +08:00
|
|
|
func (l *Listener) updateSocks(addr string) error {
|
2018-07-15 22:23:20 +08:00
|
|
|
if l.socksSignal != nil {
|
|
|
|
signal := l.socksSignal
|
|
|
|
signal.Done <- struct{}{}
|
|
|
|
<-signal.Closed
|
|
|
|
l.socksSignal = nil
|
|
|
|
}
|
|
|
|
|
2018-08-12 02:23:46 +08:00
|
|
|
signal, err := socks.NewSocksProxy(addr)
|
2018-07-15 22:23:20 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
l.socksSignal = signal
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-12 04:00:34 +08:00
|
|
|
func (l *Listener) updateRedir(addr string) error {
|
|
|
|
if l.redirSignal != nil {
|
|
|
|
signal := l.redirSignal
|
|
|
|
signal.Done <- struct{}{}
|
|
|
|
<-signal.Closed
|
|
|
|
l.redirSignal = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
signal, err := redir.NewRedirProxy(addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
l.redirSignal = signal
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-26 00:04:59 +08:00
|
|
|
func (l *Listener) process(signal chan<- struct{}) {
|
|
|
|
sub := config.Instance().Subscribe()
|
|
|
|
signal <- struct{}{}
|
2018-08-12 02:23:46 +08:00
|
|
|
reportCH := config.Instance().Report()
|
2018-07-26 00:04:59 +08:00
|
|
|
for elm := range sub {
|
|
|
|
event := elm.(*config.Event)
|
2018-08-12 02:23:46 +08:00
|
|
|
switch event.Type {
|
|
|
|
case "http-addr":
|
|
|
|
addr := event.Payload.(string)
|
|
|
|
err := l.updateHTTP(addr)
|
|
|
|
reportCH <- &config.Event{Type: "http-addr", Payload: err == nil}
|
|
|
|
break
|
|
|
|
case "socks-addr":
|
|
|
|
addr := event.Payload.(string)
|
|
|
|
err := l.updateSocks(addr)
|
|
|
|
reportCH <- &config.Event{Type: "socks-addr", Payload: err == nil}
|
|
|
|
break
|
2018-08-12 04:00:34 +08:00
|
|
|
case "redir-addr":
|
|
|
|
addr := event.Payload.(string)
|
|
|
|
err := l.updateRedir(addr)
|
|
|
|
reportCH <- &config.Event{Type: "redir-addr", Payload: err == nil}
|
|
|
|
break
|
2018-07-26 00:04:59 +08:00
|
|
|
}
|
2018-07-15 22:23:20 +08:00
|
|
|
}
|
2018-07-26 00:04:59 +08:00
|
|
|
}
|
2018-07-15 22:23:20 +08:00
|
|
|
|
2018-07-26 00:04:59 +08:00
|
|
|
// Run ensure config monitoring
|
|
|
|
func (l *Listener) Run() {
|
|
|
|
signal := make(chan struct{})
|
|
|
|
go l.process(signal)
|
|
|
|
<-signal
|
|
|
|
}
|
2018-07-15 22:23:20 +08:00
|
|
|
|
2018-07-26 00:04:59 +08:00
|
|
|
func newListener() *Listener {
|
|
|
|
return &Listener{}
|
2018-07-15 22:23:20 +08:00
|
|
|
}
|
|
|
|
|
2018-07-26 00:04:59 +08:00
|
|
|
// Instance return singleton instance of Listener
|
2018-07-15 22:23:20 +08:00
|
|
|
func Instance() *Listener {
|
|
|
|
once.Do(func() {
|
|
|
|
listener = newListener()
|
|
|
|
})
|
|
|
|
return listener
|
|
|
|
}
|