Skip to content

Commit

Permalink
Implement simple forward proxy for HTTP
Browse files Browse the repository at this point in the history
  • Loading branch information
dvob committed Jan 23, 2023
0 parents commit 6319eda
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/go-proxy
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module go-proxy

go 1.19
32 changes: 32 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"io"
"log"
"net/http"
"os"
)

func forward(w http.ResponseWriter, r *http.Request) {
resp, err := http.DefaultTransport.RoundTrip(r)
if err != nil {
log.Print(err)
http.Error(w, http.StatusText(http.StatusServiceUnavailable), http.StatusServiceUnavailable)
return
}
for header, values := range resp.Header {
for _, value := range values {
w.Header().Add(header, value)
}
}
w.WriteHeader(resp.StatusCode)
io.Copy(w, resp.Body)
}

func main() {
err := http.ListenAndServe(":8080", http.HandlerFunc(forward))
if err != nil {
log.Print(err)
os.Exit(1)
}
}

0 comments on commit 6319eda

Please sign in to comment.