Chore: improve code architecture

This commit is contained in:
Dreamacro
2018-11-21 13:47:46 +08:00
parent 91e35f2f6a
commit 01a477bd3d
37 changed files with 926 additions and 990 deletions

View File

@ -6,31 +6,25 @@ import (
"net/http"
"github.com/Dreamacro/clash/adapters/inbound"
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/log"
"github.com/Dreamacro/clash/tunnel"
log "github.com/sirupsen/logrus"
)
var (
tun = tunnel.Instance()
)
func NewHttpProxy(addr string) (*C.ProxySignal, error) {
func NewHttpProxy(addr string) (chan<- struct{}, <-chan struct{}, error) {
l, err := net.Listen("tcp", addr)
if err != nil {
return nil, err
return nil, nil, err
}
done := make(chan struct{})
closed := make(chan struct{})
signal := &C.ProxySignal{
Done: done,
Closed: closed,
}
go func() {
log.Infof("HTTP proxy listening at: %s", addr)
log.Infoln("HTTP proxy listening at: %s", addr)
for {
c, err := l.Accept()
if err != nil {
@ -45,12 +39,11 @@ func NewHttpProxy(addr string) (*C.ProxySignal, error) {
go func() {
<-done
close(done)
l.Close()
closed <- struct{}{}
}()
return signal, nil
return done, closed, nil
}
func handleConn(conn net.Conn) {