func (self *TcpProxy) handle(clientConn net.Conn) { upstream := self.vip.GetString("tcpproxys." + self.name + ".upstream") whitelisthosts := self.vip.GetStringSlice("whitelisthosts") if self.MatchHost(whitelisthosts, upstream) == false { clientConn.Write([]byte("sorry '" + upstream + "' is not in whitelist")) return } upstreamConn, err := self.dialerpool.Dial("tcp", upstream) if err != nil { glog.Errorf("downstream '%s' dial upstream '%s' error :%s", clientConn.RemoteAddr().String(), upstream, err.Error()) clientConn.Write([]byte("upstream is wrong: " + err.Error())) return } utils.TcpPipe(upstreamConn, clientConn) }
func (self *HttpProxy) connect(w http.ResponseWriter, r *http.Request) { whitelisthosts := self.vip.GetStringSlice("whitelisthosts") if self.MatchHost(whitelisthosts, r.RequestURI) == false { w.WriteHeader(http.StatusForbidden) w.Write([]byte("sorry '" + r.RequestURI + "' is not in whitelist")) return } glog.V(0).Infof("client %s ready httpproxy to %s", r.RemoteAddr, r.RequestURI) serverConn, err := self.dialerpool.Dial("tcp", r.RequestURI) if err != nil { http.Error(w, "Error contacting backend server.", 500) glog.Errorf("Error connect backend %s: %v", r.RequestURI, err) return } defer serverConn.Close() hj, ok := w.(http.Hijacker) if !ok { http.Error(w, "sorry, i cannot be hijacker?", 500) return } clientConn, _, err := hj.Hijack() if err != nil { glog.Infoln("Hijack error: %v", err) return } defer clientConn.Close() _, err = clientConn.Write([]byte("HTTP/1.1 200 Connection Established\r\n\r\n")) if err != nil { glog.Errorln("write error ", err) return } err = utils.TcpPipe(serverConn, clientConn) if err != nil { glog.Errorln("tcp pipe error ", err) } }