package http import ( "net" "time" "github.com/Dreamacro/clash/common/cache" C "github.com/Dreamacro/clash/constant" ) type Listener struct { listener net.Listener address string closed bool } func New(addr string, in chan<- C.ConnContext) (*Listener, error) { return NewWithAuthenticate(addr, in, true) } func NewWithAuthenticate(addr string, in chan<- C.ConnContext, authenticate bool) (*Listener, error) { l, err := net.Listen("tcp", addr) if err != nil { return nil, err } var c *cache.Cache if authenticate { c = cache.New(time.Second * 30) } hl := &Listener{ listener: l, address: addr, } go func() { for { conn, err := hl.listener.Accept() if err != nil { if hl.closed { break } continue } go HandleConn(conn, in, c) } }() return hl, nil } func (l *Listener) Close() { l.closed = true l.listener.Close() } func (l *Listener) Address() string { return l.address }