2023-06-12 09:44:22 +00:00
|
|
|
package v4
|
2022-11-28 09:09:25 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2023-03-15 01:55:48 +00:00
|
|
|
"fmt"
|
2022-11-28 09:09:25 +00:00
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
|
2023-06-12 09:44:22 +00:00
|
|
|
"github.com/Dreamacro/clash/adapter/inbound"
|
2023-06-21 05:53:37 +00:00
|
|
|
"github.com/Dreamacro/clash/common/atomic"
|
2022-11-28 09:09:25 +00:00
|
|
|
N "github.com/Dreamacro/clash/common/net"
|
|
|
|
"github.com/Dreamacro/clash/common/pool"
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2022-11-28 10:18:51 +00:00
|
|
|
"github.com/Dreamacro/clash/transport/socks5"
|
2023-06-12 09:44:22 +00:00
|
|
|
"github.com/Dreamacro/clash/transport/tuic/common"
|
2023-03-05 03:00:14 +00:00
|
|
|
|
2023-04-09 07:40:17 +00:00
|
|
|
"github.com/gofrs/uuid/v5"
|
2023-03-05 03:00:14 +00:00
|
|
|
"github.com/metacubex/quic-go"
|
2022-11-28 09:09:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ServerOption struct {
|
2023-06-12 09:44:22 +00:00
|
|
|
HandleTcpFn func(conn net.Conn, addr socks5.Addr, additions ...inbound.Addition) error
|
|
|
|
HandleUdpFn func(addr socks5.Addr, packet C.UDPPacket, additions ...inbound.Addition) error
|
2022-11-28 09:09:25 +00:00
|
|
|
|
|
|
|
Tokens [][32]byte
|
|
|
|
MaxUdpRelayPacketSize int
|
|
|
|
}
|
|
|
|
|
2023-06-21 05:53:37 +00:00
|
|
|
func NewServerHandler(option *ServerOption, quicConn quic.EarlyConnection, uuid uuid.UUID) common.ServerHandler {
|
|
|
|
return &serverHandler{
|
2022-11-28 09:09:25 +00:00
|
|
|
ServerOption: option,
|
2023-06-21 05:53:37 +00:00
|
|
|
quicConn: quicConn,
|
|
|
|
uuid: uuid,
|
|
|
|
authCh: make(chan struct{}),
|
2022-11-28 09:09:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type serverHandler struct {
|
2023-06-21 05:53:37 +00:00
|
|
|
*ServerOption
|
2023-03-10 04:26:17 +00:00
|
|
|
quicConn quic.EarlyConnection
|
2022-11-28 09:09:25 +00:00
|
|
|
uuid uuid.UUID
|
|
|
|
|
|
|
|
authCh chan struct{}
|
2023-06-21 05:53:37 +00:00
|
|
|
authOk atomic.Bool
|
2022-11-28 09:09:25 +00:00
|
|
|
authOnce sync.Once
|
2022-11-28 10:18:51 +00:00
|
|
|
|
|
|
|
udpInputMap sync.Map
|
2022-11-28 09:09:25 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 05:53:37 +00:00
|
|
|
func (s *serverHandler) AuthOk() bool {
|
|
|
|
return s.authOk.Load()
|
|
|
|
}
|
2023-03-10 04:26:17 +00:00
|
|
|
|
2023-06-21 05:53:37 +00:00
|
|
|
func (s *serverHandler) HandleTimeout() {
|
|
|
|
s.authOnce.Do(func() {
|
|
|
|
_ = s.quicConn.CloseWithError(AuthenticationTimeout, "AuthenticationTimeout")
|
|
|
|
s.authOk.Store(false)
|
|
|
|
close(s.authCh)
|
2023-03-10 04:26:17 +00:00
|
|
|
})
|
2022-11-28 09:09:25 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 05:53:37 +00:00
|
|
|
func (s *serverHandler) HandleMessage(message []byte) (err error) {
|
|
|
|
buffer := bytes.NewBuffer(message)
|
|
|
|
packet, err := ReadPacket(buffer)
|
|
|
|
if err != nil {
|
|
|
|
return
|
2022-11-28 09:09:25 +00:00
|
|
|
}
|
2023-08-03 15:07:30 +00:00
|
|
|
return s.parsePacket(&packet, common.NATIVE)
|
2022-11-28 09:09:25 +00:00
|
|
|
}
|
|
|
|
|
2023-08-03 15:07:30 +00:00
|
|
|
func (s *serverHandler) parsePacket(packet *Packet, udpRelayMode common.UdpRelayMode) (err error) {
|
2022-11-28 09:09:25 +00:00
|
|
|
<-s.authCh
|
2023-06-21 05:53:37 +00:00
|
|
|
if !s.authOk.Load() {
|
2022-11-28 09:09:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
var assocId uint32
|
|
|
|
|
|
|
|
assocId = packet.ASSOC_ID
|
2022-11-28 10:18:51 +00:00
|
|
|
|
|
|
|
v, _ := s.udpInputMap.LoadOrStore(assocId, &atomic.Bool{})
|
|
|
|
writeClosed := v.(*atomic.Bool)
|
|
|
|
if writeClosed.Load() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-28 09:09:25 +00:00
|
|
|
pc := &quicStreamPacketConn{
|
|
|
|
connId: assocId,
|
|
|
|
quicConn: s.quicConn,
|
|
|
|
inputConn: nil,
|
|
|
|
udpRelayMode: udpRelayMode,
|
|
|
|
maxUdpRelayPacketSize: s.MaxUdpRelayPacketSize,
|
|
|
|
deferQuicConnFn: nil,
|
|
|
|
closeDeferFn: nil,
|
2022-11-28 10:18:51 +00:00
|
|
|
writeClosed: writeClosed,
|
2022-11-28 09:09:25 +00:00
|
|
|
}
|
|
|
|
|
2022-11-28 10:18:51 +00:00
|
|
|
return s.HandleUdpFn(packet.ADDR.SocksAddr(), &serverUDPPacket{
|
2022-11-28 09:09:25 +00:00
|
|
|
pc: pc,
|
2023-08-03 15:07:30 +00:00
|
|
|
packet: packet,
|
2023-03-15 01:55:48 +00:00
|
|
|
rAddr: N.NewCustomAddr("tuic", fmt.Sprintf("tuic-%s-%d", s.uuid, assocId), s.quicConn.RemoteAddr()), // for tunnel's handleUDPConn
|
2022-11-28 09:09:25 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-06-21 05:53:37 +00:00
|
|
|
func (s *serverHandler) HandleStream(conn *N.BufferedConn) (err error) {
|
|
|
|
connect, err := ReadConnect(conn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
<-s.authCh
|
|
|
|
if !s.authOk.Load() {
|
|
|
|
return conn.Close()
|
|
|
|
}
|
2022-11-28 09:09:25 +00:00
|
|
|
|
2023-06-21 05:53:37 +00:00
|
|
|
buf := pool.GetBuffer()
|
|
|
|
defer pool.PutBuffer(buf)
|
|
|
|
err = s.HandleTcpFn(conn, connect.ADDR.SocksAddr())
|
|
|
|
if err != nil {
|
|
|
|
err = NewResponseFailed().WriteTo(buf)
|
|
|
|
defer conn.Close()
|
|
|
|
} else {
|
|
|
|
err = NewResponseSucceed().WriteTo(buf)
|
2022-11-28 09:09:25 +00:00
|
|
|
}
|
2023-06-21 05:53:37 +00:00
|
|
|
if err != nil {
|
|
|
|
_ = conn.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = buf.WriteTo(conn)
|
|
|
|
if err != nil {
|
|
|
|
_ = conn.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2022-11-28 09:09:25 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 05:53:37 +00:00
|
|
|
func (s *serverHandler) HandleUniStream(reader *bufio.Reader) (err error) {
|
|
|
|
commandHead, err := ReadCommandHead(reader)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch commandHead.TYPE {
|
|
|
|
case AuthenticateType:
|
|
|
|
var authenticate Authenticate
|
|
|
|
authenticate, err = ReadAuthenticateWithHead(commandHead, reader)
|
2022-11-28 09:09:25 +00:00
|
|
|
if err != nil {
|
2023-06-21 05:53:37 +00:00
|
|
|
return
|
2022-11-28 09:09:25 +00:00
|
|
|
}
|
2023-06-21 05:53:37 +00:00
|
|
|
authOk := false
|
|
|
|
for _, tkn := range s.Tokens {
|
|
|
|
if authenticate.TKN == tkn {
|
|
|
|
authOk = true
|
|
|
|
break
|
2022-11-28 09:09:25 +00:00
|
|
|
}
|
2023-06-21 05:53:37 +00:00
|
|
|
}
|
|
|
|
s.authOnce.Do(func() {
|
|
|
|
if !authOk {
|
|
|
|
_ = s.quicConn.CloseWithError(AuthenticationFailed, "AuthenticationFailed")
|
2022-11-28 09:09:25 +00:00
|
|
|
}
|
2023-06-21 05:53:37 +00:00
|
|
|
s.authOk.Store(authOk)
|
|
|
|
close(s.authCh)
|
|
|
|
})
|
|
|
|
case PacketType:
|
|
|
|
var packet Packet
|
|
|
|
packet, err = ReadPacketWithHead(commandHead, reader)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-08-03 15:07:30 +00:00
|
|
|
return s.parsePacket(&packet, common.QUIC)
|
2023-06-21 05:53:37 +00:00
|
|
|
case DissociateType:
|
|
|
|
var disassociate Dissociate
|
|
|
|
disassociate, err = ReadDissociateWithHead(commandHead, reader)
|
|
|
|
if err != nil {
|
2022-11-28 09:09:25 +00:00
|
|
|
return
|
2023-06-21 05:53:37 +00:00
|
|
|
}
|
|
|
|
if v, loaded := s.udpInputMap.LoadAndDelete(disassociate.ASSOC_ID); loaded {
|
|
|
|
writeClosed := v.(*atomic.Bool)
|
|
|
|
writeClosed.Store(true)
|
|
|
|
}
|
|
|
|
case HeartbeatType:
|
|
|
|
var heartbeat Heartbeat
|
|
|
|
heartbeat, err = ReadHeartbeatWithHead(commandHead, reader)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
heartbeat.BytesLen()
|
2022-11-28 09:09:25 +00:00
|
|
|
}
|
2023-06-21 05:53:37 +00:00
|
|
|
return
|
2022-11-28 09:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type serverUDPPacket struct {
|
|
|
|
pc *quicStreamPacketConn
|
|
|
|
packet *Packet
|
|
|
|
rAddr net.Addr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *serverUDPPacket) InAddr() net.Addr {
|
|
|
|
return s.pc.LocalAddr()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *serverUDPPacket) LocalAddr() net.Addr {
|
|
|
|
return s.rAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *serverUDPPacket) Data() []byte {
|
|
|
|
return s.packet.DATA
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *serverUDPPacket) WriteBack(b []byte, addr net.Addr) (n int, err error) {
|
|
|
|
return s.pc.WriteTo(b, addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *serverUDPPacket) Drop() {
|
|
|
|
s.packet.DATA = nil
|
|
|
|
}
|
|
|
|
|
2023-04-02 14:24:46 +00:00
|
|
|
var _ C.UDPPacket = (*serverUDPPacket)(nil)
|
|
|
|
var _ C.UDPPacketInAddr = (*serverUDPPacket)(nil)
|