2018-06-14 01:00:58 +08:00
|
|
|
package http
|
2018-06-10 22:50:03 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
|
2022-11-16 10:43:16 +08:00
|
|
|
"github.com/Dreamacro/clash/adapter/inbound"
|
2019-06-27 17:04:25 +08:00
|
|
|
"github.com/Dreamacro/clash/common/cache"
|
2021-06-13 17:23:10 +08:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2018-06-10 22:50:03 +08:00
|
|
|
)
|
|
|
|
|
2021-06-13 17:23:10 +08:00
|
|
|
type Listener struct {
|
2022-12-05 00:20:50 +08:00
|
|
|
listener net.Listener
|
|
|
|
addr string
|
|
|
|
closed bool
|
2018-11-22 11:54:01 +08:00
|
|
|
}
|
|
|
|
|
2021-08-01 00:35:37 +08:00
|
|
|
// RawAddress implements C.Listener
|
|
|
|
func (l *Listener) RawAddress() string {
|
|
|
|
return l.addr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Address implements C.Listener
|
|
|
|
func (l *Listener) Address() string {
|
|
|
|
return l.listener.Addr().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements C.Listener
|
|
|
|
func (l *Listener) Close() error {
|
|
|
|
l.closed = true
|
|
|
|
return l.listener.Close()
|
|
|
|
}
|
|
|
|
|
2023-09-28 18:59:31 +08:00
|
|
|
func New(addr string, tunnel C.Tunnel, additions ...inbound.Addition) (*Listener, error) {
|
|
|
|
return NewWithAuthenticate(addr, tunnel, true, additions...)
|
2021-06-15 17:13:40 +08:00
|
|
|
}
|
|
|
|
|
2023-09-28 18:59:31 +08:00
|
|
|
func NewWithAuthenticate(addr string, tunnel C.Tunnel, authenticate bool, additions ...inbound.Addition) (*Listener, error) {
|
2022-12-05 00:20:50 +08:00
|
|
|
if len(additions) == 0 {
|
2022-12-05 10:12:53 +08:00
|
|
|
additions = []inbound.Addition{
|
|
|
|
inbound.WithInName("DEFAULT-HTTP"),
|
|
|
|
inbound.WithSpecialRules(""),
|
|
|
|
}
|
2022-12-05 00:20:50 +08:00
|
|
|
}
|
2022-11-16 10:43:16 +08:00
|
|
|
l, err := inbound.Listen("tcp", addr)
|
2022-07-22 15:16:09 +08:00
|
|
|
|
2018-07-15 22:23:20 +08:00
|
|
|
if err != nil {
|
2018-11-22 11:54:01 +08:00
|
|
|
return nil, err
|
2018-07-15 22:23:20 +08:00
|
|
|
}
|
2021-06-15 17:13:40 +08:00
|
|
|
|
2022-11-12 20:43:48 +08:00
|
|
|
var c *cache.LruCache[string, bool]
|
2021-06-15 17:13:40 +08:00
|
|
|
if authenticate {
|
2022-11-12 20:43:48 +08:00
|
|
|
c = cache.New[string, bool](cache.WithAge[string, bool](30))
|
2021-06-15 17:13:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
hl := &Listener{
|
2022-12-05 00:20:50 +08:00
|
|
|
listener: l,
|
|
|
|
addr: addr,
|
2021-06-15 17:13:40 +08:00
|
|
|
}
|
2018-07-15 22:23:20 +08:00
|
|
|
go func() {
|
2018-08-11 22:51:30 +08:00
|
|
|
for {
|
2021-06-15 17:13:40 +08:00
|
|
|
conn, err := hl.listener.Accept()
|
2018-08-11 22:51:30 +08:00
|
|
|
if err != nil {
|
2018-11-22 11:54:01 +08:00
|
|
|
if hl.closed {
|
2018-08-11 22:51:30 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2023-10-23 16:50:43 +08:00
|
|
|
if t, ok := conn.(*net.TCPConn); ok {
|
|
|
|
t.SetKeepAlive(false)
|
|
|
|
}
|
2023-09-28 18:59:31 +08:00
|
|
|
go HandleConn(conn, tunnel, c, additions...)
|
2018-08-11 22:51:30 +08:00
|
|
|
}
|
2018-07-15 22:23:20 +08:00
|
|
|
}()
|
|
|
|
|
2018-11-22 11:54:01 +08:00
|
|
|
return hl, nil
|
|
|
|
}
|