working commit

This commit is contained in:
2026-05-29 19:07:59 +02:00
parent 4e2c548e97
commit c1f85e87c9
23 changed files with 543 additions and 342 deletions
+38 -38
View File
@@ -4,66 +4,66 @@
package handler
import (
"io"
"net"
"net/http"
"sync"
"io"
"net"
"sync"
"mproxy/app/router"
)
func (hand *Handler) ConnectTo(rctx *router.Context) {
hostaddr := rctx.GetHost()
hand.logg.Debugf("Hostaddr: [%s]", hostaddr)
hand.logg.Debugf("Hostaddr: [%s]", hostaddr)
destConn, err := net.Dial("tcp", hostaddr)
if err != nil {
destConn, err := net.Dial("tcp", hostaddr)
if err != nil {
hand.logg.Errorf("ConnectTo error: %v", err)
rctx.SetStatus(http.StatusInternalServerError)
return
}
}
if err != nil {
hand.logg.Errorf("ConnectTo error: %v", err)
rctx.SetStatus(http.StatusInternalServerError)
return
}
defer destConn.Close()
hijacker, hijackerOk := rctx.Writer.(http.Hijacker)
if !hijackerOk {
hand.logg.Errorf("Hijacking not OK")
defer destConn.Close()
hijacker, hijackerOk := rctx.Writer.(http.Hijacker)
if !hijackerOk {
hand.logg.Errorf("Hijacking not OK")
rctx.SetStatus(http.StatusInternalServerError)
return
}
}
rctx.SetStatus(http.StatusOK)
clientConn, _, err := hijacker.Hijack()
if err != nil {
hand.logg.Errorf("Hijacking error: %v", err)
clientConn, _, err := hijacker.Hijack()
if err != nil {
hand.logg.Errorf("Hijacking error: %v", err)
rctx.SetStatus(http.StatusInternalServerError)
return
}
var wg sync.WaitGroup
copyTo := func() {
defer wg.Done()
_, err = io.Copy(clientConn, destConn)
if err != nil {
hand.logg.Errorf("CopyTo error: %v", err)
return
}
}
copyFrom := func() {
defer wg.Done()
_, err = io.Copy(destConn, clientConn)
if err != nil {
hand.logg.Errorf("CopyFrom error: %v", err)
return
}
}
wg.Add(1)
go copyTo()
wg.Add(1)
go copyFrom()
wg.Wait()
}
var wg sync.WaitGroup
copyTo := func() {
defer wg.Done()
_, err = io.Copy(clientConn, destConn)
if err != nil {
hand.logg.Errorf("CopyTo error: %v", err)
return
}
}
copyFrom := func() {
defer wg.Done()
_, err = io.Copy(destConn, clientConn)
if err != nil {
hand.logg.Errorf("CopyFrom error: %v", err)
return
}
}
wg.Add(1)
go copyTo()
wg.Add(1)
go copyFrom()
wg.Wait()
return
}