mirror of
https://github.com/ilanyu/ReverseProxy.git
synced 2024-04-21 12:41:56 +00:00
update v1.0
This commit is contained in:
@@ -1,2 +1,18 @@
|
||||
# ReverseProxy
|
||||
ReverseProxy in golang
|
||||
|
||||
## Use:
|
||||
|
||||
./ReverseProxy_[OS]_[ARCH] -h
|
||||
|
||||
Usage of ReverseProxy_[OS]_[ARCH]:
|
||||
-l string
|
||||
listen on ip:port (default "0.0.0.0:8888")
|
||||
-r string
|
||||
reverse proxy addr (default "http://idea.lanyus.com:80")
|
||||
|
||||
|
||||
./ReverseProxy_windows_amd64.exe -l "0.0.0.0:8081" -r "https://www.baidu.com"
|
||||
|
||||
Listening on 0.0.0.0:8081, forwarding to https://www.baidu.com
|
||||
|
||||
|
||||
@@ -0,0 +1,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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user