Пример #1
0
func (s Server) ServeHTTP(out http.ResponseWriter, r *http.Request) {
	if addr, ok := s[r.Host]; ok {
		if c, e := net.Dial("tcp",addr); e == nil {
			if oc, _,e := out.(http.Hijacker).Hijack(); e == nil {
				go func() {
					io.Copy(oc, c)
					oc.Close()
				}()
				go func() {
					r.Header.Set("X-Forwarded-For",r.RemoteAddr)
					r.Write(c)
					io.Copy(c, oc)
					c.Close()
				}()
				if file,e := os.OpenFile("access.log", os.O_RDWR|os.O_APPEND|os.O_CREATE,0666); e == nil {
					log.SetOutput(io.MultiWriter(file,os.Stdout))
					log.Println(r.RemoteAddr,r.Host,r.Method,r.Header["Referer"],r.Proto,r.Header["User-Agent"])
					file.Close()
				}		
				return
			} else c.Close()
		} else os.Stderr.WriteString(os.Args[0] + ": " + e.String() + "\n")
	}
	log.Println("Service Unavailable")
	out.WriteHeader(503)
	out.Write([]byte("Service Unavailable"))
}
Пример #2
0
func (s Server) ServeHTTP(out http.ResponseWriter, r *http.Request) {
	if addr, ok := s[r.Host]; ok {
		if c, e := net.Dial("tcp", "", addr); e == nil {
			if oc, _, e := out.Hijack(); e == nil {
				go func() {
					io.Copy(oc, c)
					oc.Close()
				}()
				go func() {
					r.Header["X-Forwarded-For"] = out.RemoteAddr()
					r.Write(c)
					io.Copy(c, oc)
					c.Close()
				}()
				return
			} else c.Close()
		} else os.Stderr.WriteString(os.Args[0] + ": " + e.String() + "\n")
	}
	out.WriteHeader(503)
	out.Write([]byte("Service Unavailable"))
}