Skip to content

Commit

Permalink
Unable to open the site, the reverse proxy past
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjia404 committed Jun 16, 2023
1 parent 86090fe commit 48255de
Showing 1 changed file with 43 additions and 6 deletions.
49 changes: 43 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"html/template"
"io/ioutil"
"net/http"
"net/http/httputil"
"net/url"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -39,6 +41,13 @@ type UserData struct {
Post []Post `json:"post"`
}

func ModifyResponse(resp *http.Response) error {

resp.Header.Set("Content-Security-Policy", "")

return nil
}

func setupRouter(db *gorm.DB) *gin.Engine {
// Disable Console Color
// gin.DisableConsoleColor()
Expand All @@ -52,6 +61,7 @@ func setupRouter(db *gorm.DB) *gin.Engine {
html.WithXHTML(),
),
)
remote, _ := url.Parse(ProxyHost)

r := gin.Default()
r.LoadHTMLGlob("templates/**/*")
Expand All @@ -62,13 +72,10 @@ func setupRouter(db *gorm.DB) *gin.Engine {
})

r.GET("/", func(c *gin.Context) {
blogs := make([]models.Blog, 0, 20)
db.Limit(20).Find(&blogs)
for _, blog := range blogs {
fmt.Printf("Address:%s %s\n", blog.Address, blog.Title)
}
blogs := make([]models.Blog, 0, 100)
db.Limit(100).Find(&blogs)
c.HTML(http.StatusOK, "index/index.tmpl", gin.H{
"title": "zeronet 2 web",
"title": "zeronet to web",
"description": "显示全部zeronet博客",
"blogs": blogs,
})
Expand All @@ -83,6 +90,21 @@ func setupRouter(db *gorm.DB) *gin.Engine {
jsonFile, err := os.Open(ZeroNetDataPath + address + "/data/data.json")
if err != nil {
fmt.Println("文件不存在,请查看该文件")

c.Request.Host = remote.Host
fmt.Println(c.Request.Host, c.Request.URL)
proxy := httputil.NewSingleHostReverseProxy(remote)
proxy.Director = func(req *http.Request) {
req.Header = c.Request.Header
req.Host = remote.Host
req.URL.Scheme = remote.Scheme
req.URL.Host = remote.Host
req.URL.Path = "/raw" + c.Request.URL.Path
fmt.Println(req.URL.Path)
}
proxy.ModifyResponse = ModifyResponse
proxy.ServeHTTP(c.Writer, c.Request)
return
}
byteValue, _ := ioutil.ReadAll(jsonFile)
var result UserData
Expand Down Expand Up @@ -156,6 +178,21 @@ func setupRouter(db *gorm.DB) *gin.Engine {
// c.String(http.StatusOK, "Hello %s %s", name, post_id)
})

r.NoRoute(func(c *gin.Context) {
c.Request.Host = remote.Host
fmt.Println(c.Request.Host, c.Request.URL)
proxy := httputil.NewSingleHostReverseProxy(remote)
proxy.Director = func(req *http.Request) {
req.Header = c.Request.Header
req.Host = remote.Host
req.URL.Scheme = remote.Scheme
req.URL.Host = remote.Host
req.URL.Path = "/raw" + c.Request.URL.Path
fmt.Println(req.URL.Path)
}
proxy.ModifyResponse = ModifyResponse
proxy.ServeHTTP(c.Writer, c.Request)
})
return r
}

Expand Down

0 comments on commit 48255de

Please sign in to comment.