add server side support for extra data based client id

This commit is contained in:
Shelikhoo 2024-05-02 11:12:08 +01:00 committed by WofWca
parent bf165264b1
commit c7ccaa38f9

View file

@ -7,11 +7,13 @@ import (
"crypto/rand" "crypto/rand"
"crypto/sha256" "crypto/sha256"
"encoding/binary" "encoding/binary"
"encoding/hex"
"fmt" "fmt"
"io" "io"
"log" "log"
"net" "net"
"net/http" "net/http"
"strings"
"sync" "sync"
"time" "time"
@ -108,10 +110,16 @@ func (handler *httpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Pass the address of client as the remote address of incoming connection // Pass the address of client as the remote address of incoming connection
clientIPParam := r.URL.Query().Get("client_ip") clientIPParam := r.URL.Query().Get("client_ip")
addr := clientAddr(clientIPParam) addr := clientAddr(clientIPParam)
clientTransport := r.URL.Query().Get("protocol") protocol := r.URL.Query().Get("protocol")
clientTransport := "t"
if protocol != "" {
clientTransport = fmt.Sprintf("%c", protocol[0])
}
if clientTransport == "u" { if clientTransport == "u" {
err = handler.turboTunnelUDPLikeMode(conn, addr) err = handler.turboTunnelUDPLikeMode(conn, addr, protocol)
if err != nil && err != io.EOF { if err != nil && err != io.EOF {
log.Println(err) log.Println(err)
return return
@ -231,21 +239,19 @@ func (handler *httpHandler) turbotunnelMode(conn net.Conn, addr net.Addr) error
return nil return nil
} }
func (handler *httpHandler) turboTunnelUDPLikeMode(conn net.Conn, addr net.Addr) error { func (handler *httpHandler) turboTunnelUDPLikeMode(conn net.Conn, addr net.Addr, protocol string) error {
packetConnIDCon := packetConnIDConnServer{Conn: conn}
var packet [1600]byte var packet [1600]byte
n, err := packetConnIDCon.Read(packet[:])
if err != nil { clientID := turbotunnel.ClientID{}
return fmt.Errorf("reading ClientID: %v", err) compoments := strings.Split(protocol, " ")
} _, err := hex.Decode(clientID[:], []byte(compoments[1]))
clientID, err := packetConnIDCon.GetClientID()
if err != nil { if err != nil {
return fmt.Errorf("reading ClientID: %v", err) return fmt.Errorf("reading ClientID: %v", err)
} }
clientIDAddrMap.Set(clientID, addr) clientIDAddrMap.Set(clientID, addr)
pconn := handler.lookupPacketConn(clientID) pconn := handler.lookupPacketConn(clientID)
pconn.QueueIncoming(packet[:n], clientID)
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(2) wg.Add(2)
done := make(chan struct{}) done := make(chan struct{})
@ -253,7 +259,7 @@ func (handler *httpHandler) turboTunnelUDPLikeMode(conn net.Conn, addr net.Addr)
defer wg.Done() defer wg.Done()
defer close(done) // Signal the write loop to finish defer close(done) // Signal the write loop to finish
for { for {
n, err := packetConnIDCon.Read(packet[:]) n, err := conn.Read(packet[:])
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return return
@ -272,7 +278,7 @@ func (handler *httpHandler) turboTunnelUDPLikeMode(conn net.Conn, addr net.Addr)
if !ok { if !ok {
return return
} }
_, err := packetConnIDCon.Write(p) _, err := conn.Write(p)
pconn.Restore(p) pconn.Restore(p)
if err != nil { if err != nil {
log.Println(err) log.Println(err)