2023-06-12 17:44:22 +08:00
|
|
|
package v5
|
2022-11-28 17:09:25 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2023-03-15 09:55:48 +08:00
|
|
|
"fmt"
|
2022-11-28 17:09:25 +08:00
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/adapter/inbound"
|
|
|
|
"github.com/metacubex/mihomo/common/atomic"
|
|
|
|
N "github.com/metacubex/mihomo/common/net"
|
|
|
|
C "github.com/metacubex/mihomo/constant"
|
|
|
|
"github.com/metacubex/mihomo/transport/socks5"
|
|
|
|
"github.com/metacubex/mihomo/transport/tuic/common"
|
2023-03-05 11:00:14 +08:00
|
|
|
|
2023-04-09 15:40:17 +08:00
|
|
|
"github.com/gofrs/uuid/v5"
|
2023-03-05 11:00:14 +08:00
|
|
|
"github.com/metacubex/quic-go"
|
2023-11-23 10:24:01 +08:00
|
|
|
"github.com/puzpuzpuz/xsync/v3"
|
2022-11-28 17:09:25 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type ServerOption struct {
|
2023-06-12 17:44:22 +08: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 17:09:25 +08:00
|
|
|
|
2023-06-12 17:44:22 +08:00
|
|
|
Users map[[16]byte]string
|
2022-11-28 17:09:25 +08:00
|
|
|
MaxUdpRelayPacketSize int
|
|
|
|
}
|
|
|
|
|
2023-06-21 13:53:37 +08:00
|
|
|
func NewServerHandler(option *ServerOption, quicConn quic.EarlyConnection, uuid uuid.UUID) common.ServerHandler {
|
|
|
|
return &serverHandler{
|
2022-11-28 17:09:25 +08:00
|
|
|
ServerOption: option,
|
2023-06-21 13:53:37 +08:00
|
|
|
quicConn: quicConn,
|
|
|
|
uuid: uuid,
|
|
|
|
authCh: make(chan struct{}),
|
2023-11-23 10:24:01 +08:00
|
|
|
udpInputMap: xsync.NewMapOf[uint16, *serverUDPInput](),
|
2022-11-28 17:09:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type serverHandler struct {
|
2023-06-21 13:53:37 +08:00
|
|
|
*ServerOption
|
2023-03-10 12:26:17 +08:00
|
|
|
quicConn quic.EarlyConnection
|
2022-11-28 17:09:25 +08:00
|
|
|
uuid uuid.UUID
|
|
|
|
|
|
|
|
authCh chan struct{}
|
2023-06-21 13:53:37 +08:00
|
|
|
authOk atomic.Bool
|
|
|
|
authUUID atomic.TypedValue[string]
|
2022-11-28 17:09:25 +08:00
|
|
|
authOnce sync.Once
|
2022-11-28 18:18:51 +08:00
|
|
|
|
2023-09-02 16:54:35 +08:00
|
|
|
udpInputMap *xsync.MapOf[uint16, *serverUDPInput]
|
2022-11-28 17:09:25 +08:00
|
|
|
}
|
|
|
|
|
2023-06-21 13:53:37 +08:00
|
|
|
func (s *serverHandler) AuthOk() bool {
|
|
|
|
return s.authOk.Load()
|
|
|
|
}
|
2023-03-10 12:26:17 +08:00
|
|
|
|
2023-06-21 13:53:37 +08:00
|
|
|
func (s *serverHandler) HandleTimeout() {
|
|
|
|
s.authOnce.Do(func() {
|
|
|
|
_ = s.quicConn.CloseWithError(AuthenticationTimeout, "AuthenticationTimeout")
|
|
|
|
s.authOk.Store(false)
|
|
|
|
close(s.authCh)
|
2023-03-10 12:26:17 +08:00
|
|
|
})
|
2022-11-28 17:09:25 +08:00
|
|
|
}
|
|
|
|
|
2023-06-21 13:53:37 +08:00
|
|
|
func (s *serverHandler) HandleMessage(message []byte) (err error) {
|
|
|
|
reader := bytes.NewBuffer(message)
|
|
|
|
commandHead, err := ReadCommandHead(reader)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch commandHead.TYPE {
|
|
|
|
case PacketType:
|
|
|
|
var packet Packet
|
|
|
|
packet, err = ReadPacketWithHead(commandHead, reader)
|
2022-11-28 17:09:25 +08:00
|
|
|
if err != nil {
|
2023-06-21 13:53:37 +08:00
|
|
|
return
|
2022-11-28 17:09:25 +08:00
|
|
|
}
|
2023-08-03 23:07:30 +08:00
|
|
|
return s.parsePacket(&packet, common.NATIVE)
|
2023-06-21 13:53:37 +08:00
|
|
|
case HeartbeatType:
|
|
|
|
var heartbeat Heartbeat
|
|
|
|
heartbeat, err = ReadHeartbeatWithHead(commandHead, reader)
|
|
|
|
if err != nil {
|
2023-06-13 17:50:10 +08:00
|
|
|
return
|
2023-06-21 13:53:37 +08:00
|
|
|
}
|
|
|
|
heartbeat.BytesLen()
|
2022-11-28 17:09:25 +08:00
|
|
|
}
|
2023-06-21 13:53:37 +08:00
|
|
|
return
|
2022-11-28 17:09:25 +08:00
|
|
|
}
|
|
|
|
|
2023-08-03 23:07:30 +08:00
|
|
|
func (s *serverHandler) parsePacket(packet *Packet, udpRelayMode common.UdpRelayMode) (err error) {
|
2022-11-28 17:09:25 +08:00
|
|
|
<-s.authCh
|
2023-06-21 13:53:37 +08:00
|
|
|
if !s.authOk.Load() {
|
2022-11-28 17:09:25 +08:00
|
|
|
return
|
|
|
|
}
|
2023-06-12 17:44:22 +08:00
|
|
|
var assocId uint16
|
2022-11-28 17:09:25 +08:00
|
|
|
|
|
|
|
assocId = packet.ASSOC_ID
|
2022-11-28 18:18:51 +08:00
|
|
|
|
2023-09-02 16:54:35 +08:00
|
|
|
input, _ := s.udpInputMap.LoadOrCompute(assocId, func() *serverUDPInput { return &serverUDPInput{} })
|
2023-06-12 17:44:22 +08:00
|
|
|
if input.writeClosed.Load() {
|
2022-11-28 18:18:51 +08:00
|
|
|
return nil
|
|
|
|
}
|
2023-06-12 17:44:22 +08:00
|
|
|
packetPtr := input.Feed(packet)
|
|
|
|
if packetPtr == nil {
|
|
|
|
return
|
|
|
|
}
|
2022-11-28 18:18:51 +08:00
|
|
|
|
2022-11-28 17:09:25 +08:00
|
|
|
pc := &quicStreamPacketConn{
|
|
|
|
connId: assocId,
|
|
|
|
quicConn: s.quicConn,
|
|
|
|
inputConn: nil,
|
|
|
|
udpRelayMode: udpRelayMode,
|
|
|
|
maxUdpRelayPacketSize: s.MaxUdpRelayPacketSize,
|
|
|
|
deferQuicConnFn: nil,
|
|
|
|
closeDeferFn: nil,
|
2023-06-12 17:44:22 +08:00
|
|
|
writeClosed: &input.writeClosed,
|
2022-11-28 17:09:25 +08:00
|
|
|
}
|
|
|
|
|
2023-06-12 17:44:22 +08:00
|
|
|
return s.HandleUdpFn(packetPtr.ADDR.SocksAddr(), &serverUDPPacket{
|
2022-11-28 17:09:25 +08:00
|
|
|
pc: pc,
|
2023-06-12 17:44:22 +08:00
|
|
|
packet: packetPtr,
|
2023-03-15 09:55:48 +08:00
|
|
|
rAddr: N.NewCustomAddr("tuic", fmt.Sprintf("tuic-%s-%d", s.uuid, assocId), s.quicConn.RemoteAddr()), // for tunnel's handleUDPConn
|
2023-06-21 13:53:37 +08:00
|
|
|
}, inbound.WithInUser(s.authUUID.Load()))
|
2022-11-28 17:09:25 +08:00
|
|
|
}
|
|
|
|
|
2023-06-21 13:53:37 +08: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 17:09:25 +08:00
|
|
|
|
2023-06-21 13:53:37 +08:00
|
|
|
err = s.HandleTcpFn(conn, connect.ADDR.SocksAddr(), inbound.WithInUser(s.authUUID.Load()))
|
|
|
|
if err != nil {
|
|
|
|
_ = conn.Close()
|
|
|
|
return err
|
2022-11-28 17:09:25 +08:00
|
|
|
}
|
2023-06-21 13:53:37 +08:00
|
|
|
return
|
2022-11-28 17:09:25 +08:00
|
|
|
}
|
|
|
|
|
2023-06-21 13:53:37 +08: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 17:09:25 +08:00
|
|
|
if err != nil {
|
2023-06-21 13:53:37 +08:00
|
|
|
return
|
2022-11-28 17:09:25 +08:00
|
|
|
}
|
2023-06-21 13:53:37 +08:00
|
|
|
authOk := false
|
|
|
|
var authUUID uuid.UUID
|
|
|
|
var token [32]byte
|
|
|
|
if password, ok := s.Users[authenticate.UUID]; ok {
|
|
|
|
token, err = GenToken(s.quicConn.ConnectionState(), authenticate.UUID, password)
|
2022-11-28 17:09:25 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-06-21 13:53:37 +08:00
|
|
|
if token == authenticate.TOKEN {
|
|
|
|
authOk = true
|
|
|
|
authUUID = authenticate.UUID
|
2022-11-28 17:09:25 +08:00
|
|
|
}
|
2023-06-21 13:53:37 +08:00
|
|
|
}
|
|
|
|
s.authOnce.Do(func() {
|
|
|
|
if !authOk {
|
|
|
|
_ = s.quicConn.CloseWithError(AuthenticationFailed, "AuthenticationFailed")
|
|
|
|
}
|
|
|
|
s.authOk.Store(authOk)
|
|
|
|
s.authUUID.Store(authUUID.String())
|
|
|
|
close(s.authCh)
|
|
|
|
})
|
|
|
|
case PacketType:
|
|
|
|
var packet Packet
|
|
|
|
packet, err = ReadPacketWithHead(commandHead, reader)
|
|
|
|
if err != nil {
|
2022-11-28 17:09:25 +08:00
|
|
|
return
|
2023-06-21 13:53:37 +08:00
|
|
|
}
|
2023-08-03 23:07:30 +08:00
|
|
|
return s.parsePacket(&packet, common.QUIC)
|
2023-06-21 13:53:37 +08:00
|
|
|
case DissociateType:
|
|
|
|
var disassociate Dissociate
|
|
|
|
disassociate, err = ReadDissociateWithHead(commandHead, reader)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-09-02 16:54:35 +08:00
|
|
|
if input, loaded := s.udpInputMap.LoadAndDelete(disassociate.ASSOC_ID); loaded {
|
2023-06-21 13:53:37 +08:00
|
|
|
input.writeClosed.Store(true)
|
|
|
|
}
|
2022-11-28 17:09:25 +08:00
|
|
|
}
|
2023-06-21 13:53:37 +08:00
|
|
|
return
|
2022-11-28 17:09:25 +08:00
|
|
|
}
|
|
|
|
|
2023-06-12 17:44:22 +08:00
|
|
|
type serverUDPInput struct {
|
|
|
|
writeClosed atomic.Bool
|
|
|
|
deFragger
|
|
|
|
}
|
|
|
|
|
2022-11-28 17:09:25 +08: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 22:24:46 +08:00
|
|
|
var _ C.UDPPacket = (*serverUDPPacket)(nil)
|
|
|
|
var _ C.UDPPacketInAddr = (*serverUDPPacket)(nil)
|