package main
import (
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
. "反向代理/util"
)
type ProxyHandler struct {
}
func (*ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
defer func() {
if err := recover(); err != nil {
w.WriteHeader(500)
log.Println(err)
}
}()
if r.URL.Path == "/a" {
log.Println(r.RemoteAddr)
newreq, _ := http.NewRequest(r.Method, "http://localhost:9091", r.Body)
CLoneHead(r.Header, newreq.Header)
newreq.Header.Add("x-forwarded-for", r.RemoteAddr)
newreq.Header.Add("Authorization", "Basic c2hlbnlpOjEyMw==")
newresponse, _ := http.DefaultClient.Do(newreq)
getHeader := w.Header()
CLoneHead(newresponse.Header, getHeader)
w.WriteHeader(newresponse.StatusCode)
defer newresponse.Body.Close()
res_cont, _ := ioutil.ReadAll(newresponse.Body)
w.Write(res_cont)
return
}
w.Write([]byte("default index"))
}
func main() {
http.ListenAndServe(":8080", &ProxyHandler{})
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt)
s := <-c
log.Println(s)
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步