forked from ilanyu/ReverseProxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
36 lines (32 loc) · 863 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package main
import (
"log"
"net/http"
"net/http/httputil"
"net/url"
"flag"
)
type handle struct {
reverseProxy string
}
func (this *handle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
remote, err := url.Parse(this.reverseProxy)
if err != nil {
log.Fatalln(err)
}
proxy := httputil.NewSingleHostReverseProxy(remote)
r.Host = remote.Host
proxy.ServeHTTP(w, r)
log.Println(r.RemoteAddr + " " + r.Method + " " + r.URL.String() + " " + r.Proto + " " + r.UserAgent())
}
func main() {
bind := flag.String("l", "0.0.0.0:8888", "listen on ip:port")
remote := flag.String("r", "http://idea.lanyus.com:80", "reverse proxy addr")
flag.Parse()
log.Printf("Listening on %s, forwarding to %s", *bind, *remote)
h := &handle{reverseProxy: *remote}
err := http.ListenAndServe(*bind, h)
if err != nil {
log.Fatalln("ListenAndServe: ", err)
}
}