working commit
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
package config
|
||||
|
||||
const (
|
||||
confdir = "/home/ziggi/Projects/mproxy/etc/mproxy"
|
||||
rundir = "/home/ziggi/Projects/mproxy/tmp/run"
|
||||
logdir = "/home/ziggi/Projects/mproxy/tmp/log"
|
||||
datadir = "/home/ziggi/Projects/mproxy/tmp/data"
|
||||
version = "0.0.1"
|
||||
srvname = "mproxyd"
|
||||
)
|
||||
+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)
|
||||
}
|
||||
|
||||
@@ -158,9 +158,9 @@ func (route Route) Match(req *http.Request) bool {
|
||||
if req.Method != route.Method {
|
||||
return false
|
||||
}
|
||||
if req.Method == http.MethodConnect {
|
||||
if req.Method == http.MethodConnect {
|
||||
return true
|
||||
}
|
||||
}
|
||||
match := route.Regexp.MatchString(req.URL.Path)
|
||||
if match {
|
||||
return true
|
||||
|
||||
@@ -63,7 +63,7 @@ func (svc *Service) Build() error {
|
||||
svc.rout.Use(router.NewCorsMiddleware())
|
||||
svc.rout.Use(svc.hand.AuthMiddleware)
|
||||
|
||||
svc.rout.Get(`/v3/api/service/hello`, svc.hand.SendHello)
|
||||
svc.rout.Get(`/v3/api/service/hello`, svc.hand.GetHello)
|
||||
|
||||
svc.rout.Connect(``, svc.hand.ConnectTo)
|
||||
|
||||
|
||||
@@ -3,16 +3,16 @@
|
||||
*/
|
||||
package servoper
|
||||
|
||||
type SendHelloParams struct{}
|
||||
type GetHelloParams struct{}
|
||||
|
||||
type SendHelloResult struct {
|
||||
type GetHelloResult struct {
|
||||
Message string `json:"message"`
|
||||
Alive bool `json:"alive"`
|
||||
}
|
||||
|
||||
func (oper *Operator) SendHello(param *SendHelloParams) (*SendHelloResult, error) {
|
||||
func (oper *Operator) GetHello(param *GetHelloParams) (*GetHelloResult, error) {
|
||||
var err error
|
||||
res := &SendHelloResult{
|
||||
res := &GetHelloResult{
|
||||
Alive: true,
|
||||
Message: "hello",
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user