Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Kubectl proxy plus token verification

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Kubectl proxy plus token verification

Proxy does not support token plus verification, so you can only encapsulate another layer of proxies for additional verification.

Open the proxy kubectl proxy-- port=8089-- address=127.0.0.1-- accept-hosts=' ^ * $'# after this can be removed Do not allow all code mkdir go-proxycd go-proxygo mod init go-proxyvim main.gopackage mainimport ("bytes"fmt"github.com/ouqiang/goproxy"io/ioutil"log"net"net/http"net/url"strings"time") type EventHandler struct {} func (e * EventHandler) Connect (ctx * goproxy.Context, rw http.ResponseWriter) {} func (e * EventHandler) Auth (ctx * goproxy.Context) Rw http.ResponseWriter) {} func (e * EventHandler) BeforeRequest (ctx * goproxy.Context) {if clientIP, _, err: = net.SplitHostPort (ctx.Req.RemoteAddr) Err = = nil {if prior, ok: = ctx.Req.Header ["X-Forwarded-For"] Ok {clientIP = strings.Join (prior, ",") + "," + clientIP} ctx.Req.Header.Set ("X-Forwarded-For", clientIP)} / / read Body body Err: = ioutil.ReadAll (ctx.Req.Body) if err! = nil {/ / error handling return} if ctx.Req.Header.Get ("token")! = "1234" {fmt.Println ("No permission") Disable login ") ctx.Abort () return} / / Request.Body can only be read once, and must be put back after reading / / Response.Body ctx.Req.Body = ioutil.NopCloser (bytes.NewReader (body))} func (e * EventHandler) BeforeResponse (ctx * goproxy.Context, resp * http.Response) Err error) {if err! = nil {return} / / modify response} / / set the superior agent func (e * EventHandler) ParentProxy (req * http.Request) (* url.URL, error) {return url.Parse ("http://127.0.0.1:8089")}func (e * EventHandler) Finish (ctx * goproxy.Context) {log.Printf (" request to end URL:%s\ n " Ctx.Req.URL)} / / record error log func (e * EventHandler) ErrorLog (err error) {log.Println (err)} func main () {proxy: = goproxy.New (goproxy.WithDelegate (& EventHandler {})) server: = & http.Server {Addr: ": 8000", Handler: proxy, ReadTimeout: 1 * time.Minute, WriteTimeout: 1 * time.Minute } err: = server.ListenAndServe () if err! = nil {panic (err)}} go run main.go attention

There are two major changes.

If ctx.Req.Header.Get ("token")! = "1234" {here is the input of your token password return url.Parse ("http://127.0.0.1:8089")" here is your local proxy API request

Request header to add token: 1234

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report