From 6bace44fe64810bd5f8393b2e875664ed0b8a6d4 Mon Sep 17 00:00:00 2001 From: gaoshiyao Date: Thu, 24 Oct 2019 20:06:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=B7=AF=E7=94=B1=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 7 +-- go.sum | 30 ++++++++++--- src/handler/proxyhandler.go | 35 +++++++-------- src/main.go | 26 +++++++---- src/router/middleware/header/header.go | 62 ++++++++++++++++++++++++++ src/router/router.go | 37 +++++++++++++++ 6 files changed, 160 insertions(+), 37 deletions(-) create mode 100644 src/router/middleware/header/header.go create mode 100644 src/router/router.go diff --git a/go.mod b/go.mod index f63b807..624d26e 100644 --- a/go.mod +++ b/go.mod @@ -3,12 +3,13 @@ module gitlab.wodcloud.com/apaas/apaas-meshproxy go 1.12 require ( + github.com/gin-gonic/contrib v0.0.0-20190923054218-35076c1b2bea + github.com/gin-gonic/gin v1.4.0 github.com/go-redis/redis v6.15.6+incompatible - github.com/mattn/go-colorable v0.1.4 // indirect - github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect + github.com/mattn/go-isatty v0.0.8 // indirect github.com/onsi/ginkgo v1.10.2 // indirect github.com/onsi/gomega v1.7.0 // indirect + github.com/sirupsen/logrus v1.4.2 github.com/spf13/pflag v1.0.5 - github.com/tav/golly v0.0.0-20180823113506-ad032321f11e github.com/vulcand/oxy v1.0.0 ) diff --git a/go.sum b/go.sum index a1c9b5e..621a21d 100644 --- a/go.sum +++ b/go.sum @@ -4,16 +4,26 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3 h1:t8FVkw33L+wilf2QiWkw0UV77qRpcH/JHPKGpKa2E8g= +github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s= +github.com/gin-gonic/contrib v0.0.0-20190923054218-35076c1b2bea h1:tPQfr1S0mubDv/jvdbS1xbKOJzDgvIHi7db/MYr4EKg= +github.com/gin-gonic/contrib v0.0.0-20190923054218-35076c1b2bea/go.mod h1:iqneQ2Df3omzIVTkIfn7c1acsVnMGiSLn4XF5Blh3Yg= +github.com/gin-gonic/gin v1.4.0 h1:3tMoCCfM7ppqsR0ptz/wi1impNpT7/9wQtMZ8lr1mCQ= +github.com/gin-gonic/gin v1.4.0/go.mod h1:OW2EZn3DO8Ln9oIKOvM++LBO+5UPHJJDH72/q/3rZdM= github.com/go-redis/redis v6.15.6+incompatible h1:H9evprGPLI8+ci7fxQx6WNZHJSb7be8FqJQRhdQZ5Sg= github.com/go-redis/redis v6.15.6+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA= github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg= +github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q= github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/gravitational/trace v0.0.0-20190726142706-a535a178675f/go.mod h1:RvdOUHE4SHqR3oXlFFKnGzms8a5dugHygGw1bqDstYI= github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= +github.com/json-iterator/go v1.1.6 h1:MrUvLMLTMxbqFJ9kzlvat/rYZqZnW3u4wkLzWTaFwKs= +github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= @@ -26,12 +36,13 @@ github.com/mailgun/multibuf v0.0.0-20150714184110-565402cd71fb/go.mod h1:E0vRBBI github.com/mailgun/timetools v0.0.0-20141028012446-7e6055773c51 h1:Kg/NPZLLC3aAFr1YToMs98dbCdhootQ1hZIvZU28hAQ= github.com/mailgun/timetools v0.0.0-20141028012446-7e6055773c51/go.mod h1:RYmqHbhWwIz3z9eVmQ2rx82rulEMG0t+Q1bzfc9DYN4= github.com/mailgun/ttlmap v0.0.0-20170619185759-c1c17f74874f/go.mod h1:8heskWJ5c0v5J9WH89ADhyal1DOZcayll8fSbhB+/9A= -github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA= -github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= +github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE= github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= -github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b h1:j7+1HpAFS1zy5+Q4qx1fWh90gTKwiN4QCGoY9TWyyO4= -github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI= +github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.10.2 h1:uqH7bpe+ERSiDa34FDOF7RikN6RzXgduUF8yarlZp94= github.com/onsi/ginkgo v1.10.2/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= @@ -48,14 +59,15 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/tav/golly v0.0.0-20180823113506-ad032321f11e h1:cEN2utK6ivVxtMSlpCZc8+fRkI5qe2HRLxwFmL0oSk0= -github.com/tav/golly v0.0.0-20180823113506-ad032321f11e/go.mod h1:DYKtPPxKBRsX/fJcltMPl3Mdsdl+x18y6VtHlbXFfKE= +github.com/ugorji/go v1.1.4 h1:j4s+tAvLfL3bZyefP2SEWmhBzmuIlH/eqNuPdFPgngw= +github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc= github.com/vulcand/oxy v1.0.0 h1:7vL5/pjDFzHGbtBEhmlHITUi6KLH4xXTDF33/wrdRKw= github.com/vulcand/oxy v1.0.0/go.mod h1:6EXgOAl6CRa46/2ZGcDJKf3ywJUp5WtT7vSlGSkvecI= github.com/vulcand/predicate v1.1.0/go.mod h1:mlccC5IRBoc2cIFmCB8ZM62I3VDb6p2GXESMHa3CnZg= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 h1:Ao/3l156eZf2AW5wK8a7/smtodRU+gha3+BeqJ69lRk= golang.org/x/net v0.0.0-20190724013045-ca1201d0de80/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA= @@ -72,7 +84,13 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33 gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= +gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM= +gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE= +gopkg.in/go-playground/validator.v8 v8.18.2 h1:lFB4DoMU6B626w8ny76MV7VX6W2VHct2GVOI3xgiMrQ= +gopkg.in/go-playground/validator.v8 v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/src/handler/proxyhandler.go b/src/handler/proxyhandler.go index 17714fe..04f0285 100644 --- a/src/handler/proxyhandler.go +++ b/src/handler/proxyhandler.go @@ -2,6 +2,7 @@ package handler import ( "fmt" + "github.com/gin-gonic/gin" "net/http" "strings" @@ -12,36 +13,27 @@ import ( "gitlab.wodcloud.com/apaas/apaas-meshproxy/src/config" ) -// ProxyHandler , a proxy handler -type ProxyHandler struct { - forward *forward.Forwarder -} +var fwd *forward.Forwarder -// CreateProxyHandler , a proxy handler -func CreateProxyHandler() (*ProxyHandler, error) { - handler := new(ProxyHandler) - fwd, _ := forward.New(forward.PassHostHeader(true)) - handler.forward = fwd - ch = make(chan bool, 1) - return handler, nil +func init() { + fwd, _ = forward.New(forward.PassHostHeader(true)) } -var ch chan bool - -func (handler *ProxyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { +func Proxy(c *gin.Context) { if err := count(); err != nil { fmt.Println(err.Error()) - w.Write([]byte("缓存计数失败!")) + c.String(200, "缓存计数失败!") return } - req.URL = testutils.ParseURI(handler.getProxyURL(req)) - req.RequestURI = handler.getRequestURI(req) + req := c.Request + req.URL = testutils.ParseURI(getProxyURL(req)) + req.RequestURI = getRequestURI(req) req.Host = req.URL.Host fmt.Println(req.URL, req.RequestURI, req.Host) - handler.forward.ServeHTTP(w, req) + fwd.ServeHTTP(c.Writer, req) } -func (handler *ProxyHandler) getProxyURL(req *http.Request) string { +func getProxyURL(req *http.Request) string { path := req.URL.Path path = strings.Replace(req.URL.Path, config.Prefix, config.ProxyPath, 1) rawQuery := req.URL.RawQuery @@ -51,10 +43,13 @@ func (handler *ProxyHandler) getProxyURL(req *http.Request) string { } else { result = fmt.Sprintf("http://%s%s?%s", config.ProxyHost, path, rawQuery) } + if strings.HasSuffix(result, "/") { + result = strings.TrimRight(result, "/") + } return result } -func (handler *ProxyHandler) getRequestURI(req *http.Request) string { +func getRequestURI(req *http.Request) string { path := req.URL.Path rawQuery := req.URL.RawQuery var result = "" diff --git a/src/main.go b/src/main.go index 413158d..4223580 100644 --- a/src/main.go +++ b/src/main.go @@ -3,13 +3,15 @@ package main import ( "fmt" "github.com/sirupsen/logrus" + "gitlab.wodcloud.com/apaas/apaas-meshproxy/src/router" "net/http" "os" + "time" "gitlab.wodcloud.com/apaas/apaas-meshproxy/src/config" + "github.com/gin-gonic/contrib/ginrus" "github.com/spf13/pflag" - "gitlab.wodcloud.com/apaas/apaas-meshproxy/src/handler" ) var ( @@ -23,13 +25,21 @@ func main() { pflag.Parse() initEnv() initConfig() - proxyhandler, _ := handler.CreateProxyHandler() - counthandler, _ := handler.CreateCountHandler() - http.Handle(fmt.Sprintf("%s%s", *argPrefix, "/count"), counthandler) - http.Handle(*argPrefix+"/", proxyhandler) - http.Handle(os.Getenv("PROXY_PATH")+"/", proxyhandler) - logrus.Info("Listen Server on port :", *argPort) - http.ListenAndServe(fmt.Sprintf(":%d", *argPort), nil) + //proxyhandler, _ := handler.CreateProxyHandler() + //counthandler, _ := handler.CreateCountHandler() + //http.Handle(fmt.Sprintf("%s%s", *argPrefix, "/count"), counthandler) + //http.Handle(*argPrefix, proxyhandler) + server() +} + +func server() error { + config.Prefix = *argPrefix + handler := router.Load(ginrus.Ginrus(logrus.StandardLogger(), time.RFC3339, true)) + fmt.Println(fmt.Sprintf(":%d", *argPort)) + return http.ListenAndServe( + fmt.Sprintf(":%d", *argPort), + handler, + ) } //注册环境变量 diff --git a/src/router/middleware/header/header.go b/src/router/middleware/header/header.go new file mode 100644 index 0000000..5713546 --- /dev/null +++ b/src/router/middleware/header/header.go @@ -0,0 +1,62 @@ +// Copyright 2018 Drone.IO Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package header + +import ( + "net/http" + "time" + + "github.com/gin-gonic/gin" +) + +// NoCache is a middleware function that appends headers +// to prevent the client from caching the HTTP response. +func NoCache(c *gin.Context) { + c.Header("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate, value") + c.Header("Expires", "Thu, 01 Jan 1970 00:00:00 GMT") + c.Header("Last-Modified", time.Now().UTC().Format(http.TimeFormat)) + c.Next() +} + +// Options is a middleware function that appends headers +// for options requests and aborts then exits the middleware +// chain and ends the request. +func Options(c *gin.Context) { + if c.Request.Method != "OPTIONS" { + c.Next() + } else { + c.Header("Access-Control-Allow-Origin", "*") + c.Header("Access-Control-Allow-Methods", "GET,POST,PUT,PATCH,DELETE,OPTIONS") + c.Header("Access-Control-Allow-Headers", "authorization, origin, content-type, accept") + c.Header("Allow", "HEAD,GET,POST,PUT,PATCH,DELETE,OPTIONS") + c.Header("Content-Type", "application/json") + c.AbortWithStatus(200) + } +} + +// Secure is a middleware function that appends security +// and resource access headers. +func Secure(c *gin.Context) { + c.Header("Access-Control-Allow-Origin", "*") + c.Header("X-Frame-Options", "DENY") + c.Header("X-Content-Type-Options", "nosniff") + c.Header("X-XSS-Protection", "1; mode=block") + if c.Request.TLS != nil { + c.Header("Strict-Transport-Security", "max-age=31536000") + } + + // Also consider adding Content-Security-Policy headers + // c.Header("Content-Security-Policy", "script-src 'self' https://cdnjs.cloudflare.com") +} diff --git a/src/router/router.go b/src/router/router.go new file mode 100644 index 0000000..c1cace5 --- /dev/null +++ b/src/router/router.go @@ -0,0 +1,37 @@ +// Copyright 2018 Drone.IO Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package router + +import ( + "github.com/gin-gonic/gin" + "gitlab.wodcloud.com/apaas/apaas-meshproxy/src/config" + "gitlab.wodcloud.com/apaas/apaas-meshproxy/src/handler" + "gitlab.wodcloud.com/apaas/apaas-meshproxy/src/router/middleware/header" + "net/http" +) + +// Load loads the router +func Load(middleware ...gin.HandlerFunc) http.Handler { + e := gin.Default() + e.Use(header.NoCache) + e.Use(header.Options) + e.Use(header.Secure) + e.Use(middleware...) + root := e.Group(config.Prefix) + { + root.Any("*action", handler.Proxy) + } + return e +} -- 2.26.0