-
-
Save neoreids/d4bf0977717fd54c9ca32d004e2a8203 to your computer and use it in GitHub Desktop.
Http proxy server in Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io" | |
"log" | |
"net/http" | |
) | |
type HttpConnection struct { | |
Request *http.Request | |
Response *http.Response | |
} | |
type HttpConnectionChannel chan *HttpConnection | |
var connChannel = make(HttpConnectionChannel) | |
func PrintHTTP(conn *HttpConnection) { | |
fmt.Printf("%v %v\n", conn.Request.Method, conn.Request.RequestURI) | |
for k, v := range conn.Request.Header { | |
fmt.Println(k, ":", v) | |
} | |
fmt.Println("==============================") | |
fmt.Printf("HTTP/1.1 %v\n", conn.Response.Status) | |
for k, v := range conn.Response.Header { | |
fmt.Println(k, ":", v) | |
} | |
fmt.Println(conn.Response.Body) | |
fmt.Println("==============================") | |
} | |
func HandleHTTP() { | |
for { | |
select { | |
case conn := <-connChannel: | |
PrintHTTP(conn) | |
} | |
} | |
} | |
type Proxy struct { | |
} | |
func NewProxy() *Proxy { return &Proxy{} } | |
func (p *Proxy) ServeHTTP(wr http.ResponseWriter, r *http.Request) { | |
var resp *http.Response | |
var err error | |
var req *http.Request | |
client := &http.Client{} | |
//log.Printf("%v %v", r.Method, r.RequestURI) | |
req, err = http.NewRequest(r.Method, r.RequestURI, r.Body) | |
for name, value := range r.Header { | |
req.Header.Set(name, value[0]) | |
} | |
resp, err = client.Do(req) | |
r.Body.Close() | |
// combined for GET/POST | |
if err != nil { | |
http.Error(wr, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
conn := &HttpConnection{r, resp} | |
for k, v := range resp.Header { | |
wr.Header().Set(k, v[0]) | |
} | |
wr.WriteHeader(resp.StatusCode) | |
io.Copy(wr, resp.Body) | |
resp.Body.Close() | |
PrintHTTP(conn) | |
//connChannel <- &HttpConnection{r,resp} | |
} | |
func main() { | |
//go HandleHTTP() | |
proxy := NewProxy() | |
fmt.Println("==============================") | |
err := http.ListenAndServe(":12345", proxy) | |
if err != nil { | |
log.Fatal("ListenAndServe: ", err.Error()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment