working commit
This commit is contained in:
+49
-7
@@ -5,23 +5,65 @@ package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"sync"
|
||||
"io"
|
||||
"net"
|
||||
|
||||
"mproxy/app/proxoper"
|
||||
"mproxy/app/router"
|
||||
)
|
||||
|
||||
func (hand *Handler) ConnectTo(rctx *router.Context) {
|
||||
hostaddr, _ := rctx.GetSubpath("hostaddr")
|
||||
params := &proxoper.ConnectToParams{
|
||||
Hostaddr: hostaddr,
|
||||
}
|
||||
ctx := rctx.GetContext()
|
||||
_, err := hand.prop.ConnectTo(ctx, params)
|
||||
hostaddr := rctx.GetHost()
|
||||
hand.logg.Debugf("Hostaddr: [%s]", hostaddr)
|
||||
|
||||
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")
|
||||
rctx.SetStatus(http.StatusInternalServerError)
|
||||
return
|
||||
|
||||
}
|
||||
rctx.SetStatus(http.StatusOK)
|
||||
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()
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user