2020-05-12 03:29:53 +00:00
|
|
|
package mixed
|
|
|
|
|
|
|
|
import (
|
2022-07-22 07:16:09 +00:00
|
|
|
"context"
|
|
|
|
"github.com/database64128/tfo-go"
|
2020-05-12 03:29:53 +00:00
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Dreamacro/clash/common/cache"
|
2021-06-15 09:13:40 +00:00
|
|
|
N "github.com/Dreamacro/clash/common/net"
|
2021-06-13 09:23:10 +00:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
"github.com/Dreamacro/clash/listener/http"
|
|
|
|
"github.com/Dreamacro/clash/listener/socks"
|
2021-07-18 08:09:09 +00:00
|
|
|
"github.com/Dreamacro/clash/transport/socks4"
|
2021-05-13 14:18:49 +00:00
|
|
|
"github.com/Dreamacro/clash/transport/socks5"
|
2020-05-12 03:29:53 +00:00
|
|
|
)
|
|
|
|
|
2021-06-13 09:23:10 +00:00
|
|
|
type Listener struct {
|
2021-06-13 15:05:22 +00:00
|
|
|
listener net.Listener
|
2021-07-31 16:35:37 +00:00
|
|
|
addr string
|
2022-04-05 15:29:52 +00:00
|
|
|
cache *cache.Cache[string, bool]
|
2021-07-31 16:35:37 +00:00
|
|
|
closed bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
2020-05-12 03:29:53 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 07:16:09 +00:00
|
|
|
func New(addr string, inboundTfo bool, in chan<- C.ConnContext) (*Listener, error) {
|
|
|
|
lc := tfo.ListenConfig{
|
|
|
|
DisableTFO: !inboundTfo,
|
|
|
|
}
|
|
|
|
l, err := lc.Listen(context.Background(), "tcp", addr)
|
2020-05-12 03:29:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-19 06:07:51 +00:00
|
|
|
ml := &Listener{
|
|
|
|
listener: l,
|
2021-07-31 16:35:37 +00:00
|
|
|
addr: addr,
|
2022-04-05 15:29:52 +00:00
|
|
|
cache: cache.New[string, bool](30 * time.Second),
|
2021-07-19 06:07:51 +00:00
|
|
|
}
|
2020-05-12 03:29:53 +00:00
|
|
|
go func() {
|
|
|
|
for {
|
2021-06-13 15:05:22 +00:00
|
|
|
c, err := ml.listener.Accept()
|
2020-05-12 03:29:53 +00:00
|
|
|
if err != nil {
|
|
|
|
if ml.closed {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2021-06-13 09:23:10 +00:00
|
|
|
go handleConn(c, in, ml.cache)
|
2020-05-12 03:29:53 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return ml, nil
|
|
|
|
}
|
|
|
|
|
2022-04-05 15:29:52 +00:00
|
|
|
func handleConn(conn net.Conn, in chan<- C.ConnContext, cache *cache.Cache[string, bool]) {
|
2022-02-23 03:22:46 +00:00
|
|
|
conn.(*net.TCPConn).SetKeepAlive(true)
|
|
|
|
|
2021-06-15 09:13:40 +00:00
|
|
|
bufConn := N.NewBufferedConn(conn)
|
2020-05-12 03:29:53 +00:00
|
|
|
head, err := bufConn.Peek(1)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-18 08:09:09 +00:00
|
|
|
switch head[0] {
|
|
|
|
case socks4.Version:
|
|
|
|
socks.HandleSocks4(bufConn, in)
|
|
|
|
case socks5.Version:
|
|
|
|
socks.HandleSocks5(bufConn, in)
|
|
|
|
default:
|
|
|
|
http.HandleConn(bufConn, in, cache)
|
2020-05-12 03:29:53 +00:00
|
|
|
}
|
|
|
|
}
|