clash/listener/http/server.go

112 lines
2.5 KiB
Go
Raw Normal View History

2018-06-13 17:00:58 +00:00
package http
2018-06-10 14:50:03 +00:00
import (
2018-08-11 14:51:30 +00:00
"bufio"
"encoding/base64"
2018-06-10 14:50:03 +00:00
"net"
"net/http"
"strings"
"time"
2018-06-10 14:50:03 +00:00
2021-06-10 06:05:56 +00:00
"github.com/Dreamacro/clash/adapter/inbound"
"github.com/Dreamacro/clash/common/cache"
"github.com/Dreamacro/clash/component/auth"
2021-06-13 09:23:10 +00:00
C "github.com/Dreamacro/clash/constant"
authStore "github.com/Dreamacro/clash/listener/auth"
2018-11-21 05:47:46 +00:00
"github.com/Dreamacro/clash/log"
2018-06-10 14:50:03 +00:00
)
2021-06-13 09:23:10 +00:00
type Listener struct {
listener net.Listener
address string
closed bool
cache *cache.Cache
}
2021-06-13 09:23:10 +00:00
func New(addr string, in chan<- C.ConnContext) (*Listener, error) {
2018-07-15 14:23:20 +00:00
l, err := net.Listen("tcp", addr)
if err != nil {
return nil, err
2018-07-15 14:23:20 +00:00
}
2021-06-13 09:23:10 +00:00
hl := &Listener{l, addr, false, cache.New(30 * time.Second)}
2018-07-15 14:23:20 +00:00
go func() {
2018-08-11 14:51:30 +00:00
for {
c, err := hl.listener.Accept()
2018-08-11 14:51:30 +00:00
if err != nil {
if hl.closed {
2018-08-11 14:51:30 +00:00
break
}
continue
}
2021-06-13 09:23:10 +00:00
go HandleConn(c, in, hl.cache)
2018-08-11 14:51:30 +00:00
}
2018-07-15 14:23:20 +00:00
}()
return hl, nil
}
2021-06-13 09:23:10 +00:00
func (l *Listener) Close() {
l.closed = true
l.listener.Close()
}
2018-07-15 14:23:20 +00:00
2021-06-13 09:23:10 +00:00
func (l *Listener) Address() string {
return l.address
2018-06-10 14:50:03 +00:00
}
func canActivate(loginStr string, authenticator auth.Authenticator, cache *cache.Cache) (ret bool) {
if result := cache.Get(loginStr); result != nil {
ret = result.(bool)
2020-08-25 14:19:59 +00:00
return
}
loginData, err := base64.StdEncoding.DecodeString(loginStr)
login := strings.Split(string(loginData), ":")
ret = err == nil && len(login) == 2 && authenticator.Verify(login[0], login[1])
cache.Put(loginStr, ret, time.Minute)
return
}
2021-06-13 09:23:10 +00:00
func HandleConn(conn net.Conn, in chan<- C.ConnContext, cache *cache.Cache) {
2018-08-11 14:51:30 +00:00
br := bufio.NewReader(conn)
keepAlive:
2018-08-26 16:06:40 +00:00
request, err := http.ReadRequest(br)
2019-02-21 08:16:49 +00:00
if err != nil || request.URL.Host == "" {
2018-08-26 16:06:40 +00:00
conn.Close()
2018-08-11 14:51:30 +00:00
return
2018-06-10 14:50:03 +00:00
}
keepAlive := strings.TrimSpace(strings.ToLower(request.Header.Get("Proxy-Connection"))) == "keep-alive"
authenticator := authStore.Authenticator()
if authenticator != nil {
if authStrings := strings.Split(request.Header.Get("Proxy-Authorization"), " "); len(authStrings) != 2 {
2020-08-25 14:19:59 +00:00
conn.Write([]byte("HTTP/1.1 407 Proxy Authentication Required\r\nProxy-Authenticate: Basic\r\n\r\n"))
if keepAlive {
goto keepAlive
}
return
} else if !canActivate(authStrings[1], authenticator, cache) {
conn.Write([]byte("HTTP/1.1 403 Forbidden\r\n\r\n"))
log.Infoln("Auth failed from %s", conn.RemoteAddr().String())
if keepAlive {
goto keepAlive
}
conn.Close()
return
}
}
2018-08-26 16:06:40 +00:00
if request.Method == http.MethodConnect {
2018-08-11 14:51:30 +00:00
_, err := conn.Write([]byte("HTTP/1.1 200 Connection established\r\n\r\n"))
if err != nil {
conn.Close()
2018-08-11 14:51:30 +00:00
return
}
2021-06-13 09:23:10 +00:00
in <- inbound.NewHTTPS(request, conn)
2018-08-26 16:06:40 +00:00
return
2018-06-10 14:50:03 +00:00
}
2018-08-11 14:51:30 +00:00
2021-06-13 09:23:10 +00:00
in <- inbound.NewHTTP(request, conn)
2018-06-10 14:50:03 +00:00
}