working commit
This commit is contained in:
+38
-38
@@ -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
|
||||
}
|
||||
|
||||
+38
-40
@@ -4,56 +4,54 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"io"
|
||||
"context"
|
||||
"time"
|
||||
"time"
|
||||
|
||||
"mproxy/app/router"
|
||||
)
|
||||
|
||||
func (hand *Handler) PlainCall(rctx *router.Context) {
|
||||
hostaddr := rctx.GetHost()
|
||||
hand.logg.Debugf("Hostaddr: [%s]", hostaddr)
|
||||
|
||||
hand.logg.Debugf("Hostaddr: [%s]", hostaddr)
|
||||
|
||||
ctx := rctx.GetContext()
|
||||
ctx, _ = context.WithTimeout(ctx, 5*time.Second)
|
||||
reqMethod := rctx.Request.Method
|
||||
reqUrl := rctx.URL().String()
|
||||
reqBody := rctx.Request.Body
|
||||
ctx, _ = context.WithTimeout(ctx, 5*time.Second)
|
||||
reqMethod := rctx.Request.Method
|
||||
reqUrl := rctx.URL().String()
|
||||
reqBody := rctx.Request.Body
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, reqMethod, reqUrl, reqBody)
|
||||
if err != nil {
|
||||
hand.logg.Errorf("Create request error: %v", err)
|
||||
rctx.SetStatus(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
for key := range rctx.Request.Header {
|
||||
val := rctx.Request.Header.Get(key)
|
||||
req.Header.Add(key, val)
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, reqMethod, reqUrl, reqBody)
|
||||
if err != nil {
|
||||
hand.logg.Errorf("Create request error: %v", err)
|
||||
rctx.SetStatus(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
for key := range rctx.Request.Header {
|
||||
val := rctx.Request.Header.Get(key)
|
||||
req.Header.Add(key, val)
|
||||
}
|
||||
|
||||
httpClient := &http.Client{}
|
||||
resp, err := httpClient.Do(req)
|
||||
if err != nil {
|
||||
hand.logg.Errorf("Call request error: %v", err)
|
||||
rctx.SetStatus(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
// Copy headers from remote side
|
||||
for key := range resp.Header {
|
||||
val := resp.Header.Get(key)
|
||||
rctx.Writer.Header().Set(key, val)
|
||||
}
|
||||
// Copy status code
|
||||
rctx.SetStatus(resp.StatusCode)
|
||||
// Copy body
|
||||
_, err = io.Copy(rctx.Writer, resp.Body)
|
||||
if err != nil {
|
||||
hand.logg.Errorf("Copy resp body error: %v", err)
|
||||
return
|
||||
}
|
||||
httpClient := &http.Client{}
|
||||
resp, err := httpClient.Do(req)
|
||||
if err != nil {
|
||||
hand.logg.Errorf("Call request error: %v", err)
|
||||
rctx.SetStatus(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
// Copy headers from remote side
|
||||
for key := range resp.Header {
|
||||
val := resp.Header.Get(key)
|
||||
rctx.Writer.Header().Set(key, val)
|
||||
}
|
||||
// Copy status code
|
||||
rctx.SetStatus(resp.StatusCode)
|
||||
// Copy body
|
||||
_, err = io.Copy(rctx.Writer, resp.Body)
|
||||
if err != nil {
|
||||
hand.logg.Errorf("Copy resp body error: %v", err)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -8,8 +8,8 @@ import (
|
||||
"mproxy/app/servoper"
|
||||
)
|
||||
|
||||
func (hand *Handler) SendHello(rctx *router.Context) {
|
||||
params := &servoper.SendHelloParams{}
|
||||
res, _ := hand.seop.SendHello(params)
|
||||
func (hand *Handler) GetHello(rctx *router.Context) {
|
||||
params := &servoper.GetHelloParams{}
|
||||
res, _ := hand.seop.GetHello(params)
|
||||
hand.SendResult(rctx, res)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user