Skip to content

Commit

Permalink
feat: non-transparent proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
kirinnee committed Nov 9, 2023
1 parent 9bf822c commit cdd7a56
Showing 1 changed file with 77 additions and 5 deletions.
82 changes: 77 additions & 5 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,24 @@ func server() {
endpoint := "http://" + docker_executor.DockerContainerToString(d) + ":5550/api/template/init"
fmt.Println("🌐 Upstream Endpoint:", endpoint)
fmt.Println("πŸ†• Start forwarding request...")
// Forward the request body directly without reading it first
resp, err := http.Post(endpoint, c.GetHeader("Content-Type"), c.Request.Body)

reqBody, err := io.ReadAll(c.Request.Body)
if err != nil {
// Handle error
c.JSON(http.StatusBadGateway, ProblemDetails{
Title: "Read request failed",
Status: 400,
Detail: "Failed read the initial request body",
Type: "https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400",
TraceId: nil,
Data: []string{err.Error()},
})
return
}

fmt.Println("πŸ“¦ Request Body:", string(reqBody))

resp, err := http.Post(endpoint, c.GetHeader("Content-Type"), bytes.NewBuffer(reqBody))
if err != nil {
// Handle error
c.JSON(http.StatusBadGateway, ProblemDetails{
Expand Down Expand Up @@ -415,20 +431,40 @@ func server() {
}
fmt.Println("Status Code from upstream:", resp.StatusCode)
c.Data(resp.StatusCode, "Content-Type", body)

})
r.POST("/proxy/template/:cyanId/api/template/validate", func(c *gin.Context) {

cyanId := c.Param("cyanId")
fmt.Println("πŸ“‡ Cyan ID:", cyanId)

d := docker_executor.DockerContainerReference{
CyanId: cyanId,
CyanType: "template",
SessionId: "",
}
endpoint := "http://" + docker_executor.DockerContainerToString(d) + ":5550/api/template/validate"
fmt.Println("🌐 Upstream Endpoint:", endpoint)
fmt.Println("πŸ†• Start forwarding request...")

reqBody, err := io.ReadAll(c.Request.Body)
if err != nil {
// Handle error
c.JSON(http.StatusBadGateway, ProblemDetails{
Title: "Read request failed",
Status: 400,
Detail: "Failed read the initial request body",
Type: "https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400",
TraceId: nil,
Data: []string{err.Error()},
})
return
}

fmt.Println("πŸ“¦ Request Body:", string(reqBody))

// Forward the request body directly without reading it first
resp, err := http.Post(endpoint, c.GetHeader("Content-Type"), c.Request.Body)
resp, err := http.Post(endpoint, c.GetHeader("Content-Type"), bytes.NewBuffer(reqBody))
if err != nil {
// Handle error
c.JSON(http.StatusBadGateway, ProblemDetails{
Expand Down Expand Up @@ -465,16 +501,34 @@ func server() {
r.POST("/proxy/extension/:cyanId/api/extension/init", func(c *gin.Context) {

cyanId := c.Param("cyanId")
fmt.Println("πŸ“‡ Cyan ID:", cyanId)

d := docker_executor.DockerContainerReference{
CyanId: cyanId,
CyanType: "extension",
SessionId: "",
}
endpoint := "http://" + docker_executor.DockerContainerToString(d) + ":5550/api/extension/init"
fmt.Println("🌐 Upstream Endpoint:", endpoint)
fmt.Println("πŸ†• Start forwarding request...")

reqBody, err := io.ReadAll(c.Request.Body)
if err != nil {
// Handle error
c.JSON(http.StatusBadGateway, ProblemDetails{
Title: "Read request failed",
Status: 400,
Detail: "Failed read the initial request body",
Type: "https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400",
TraceId: nil,
Data: []string{err.Error()},
})
return
}

fmt.Println("πŸ“¦ Request Body:", string(reqBody))
// Forward the request body directly without reading it first
resp, err := http.Post(endpoint, c.GetHeader("Content-Type"), c.Request.Body)
resp, err := http.Post(endpoint, c.GetHeader("Content-Type"), bytes.NewBuffer(reqBody))
if err != nil {
// Handle error
c.JSON(http.StatusBadGateway, ProblemDetails{
Expand Down Expand Up @@ -511,16 +565,34 @@ func server() {
r.POST("/proxy/extension/:cyanId/api/extension/validate", func(c *gin.Context) {

cyanId := c.Param("cyanId")
fmt.Println("πŸ“‡ Cyan ID:", cyanId)

d := docker_executor.DockerContainerReference{
CyanId: cyanId,
CyanType: "extension",
SessionId: "",
}
endpoint := "http://" + docker_executor.DockerContainerToString(d) + ":5550/api/extension/validate"
fmt.Println("🌐 Upstream Endpoint:", endpoint)
fmt.Println("πŸ†• Start forwarding request...")

reqBody, err := io.ReadAll(c.Request.Body)
if err != nil {
// Handle error
c.JSON(http.StatusBadGateway, ProblemDetails{
Title: "Read request failed",
Status: 400,
Detail: "Failed read the initial request body",
Type: "https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400",
TraceId: nil,
Data: []string{err.Error()},
})
return
}

fmt.Println("πŸ“¦ Request Body:", string(reqBody))
// Forward the request body directly without reading it first
resp, err := http.Post(endpoint, c.GetHeader("Content-Type"), c.Request.Body)
resp, err := http.Post(endpoint, c.GetHeader("Content-Type"), bytes.NewBuffer(reqBody))
if err != nil {
// Handle error
c.JSON(http.StatusBadGateway, ProblemDetails{
Expand Down

0 comments on commit cdd7a56

Please sign in to comment.