change the Host value in http request header

This commit is contained in:
Maodanping
2016-07-24 15:00:18 +08:00
parent 4067591a4d
commit e0f2993b70
7 changed files with 165 additions and 9 deletions

View File

@@ -34,6 +34,10 @@ type VhostMuxer struct {
vhostFunc muxFunc
registryMap map[string]*Listener
mutex sync.RWMutex
//build map between custom_domains and client_domain
domainMap map[string]string
domainMutex sync.RWMutex
}
func NewVhostMuxer(listener *conn.Listener, vhostFunc muxFunc, timeout time.Duration) (mux *VhostMuxer, err error) {
@@ -47,7 +51,7 @@ func NewVhostMuxer(listener *conn.Listener, vhostFunc muxFunc, timeout time.Dura
return mux, nil
}
func (v *VhostMuxer) Listen(name string) (l *Listener, err error) {
func (v *VhostMuxer) Listen(name, proxytype, clientIp string, clientPort, serverPort int64) (l *Listener, err error) {
v.mutex.Lock()
defer v.mutex.Unlock()
if _, exist := v.registryMap[name]; exist {
@@ -55,9 +59,13 @@ func (v *VhostMuxer) Listen(name string) (l *Listener, err error) {
}
l = &Listener{
name: name,
mux: v,
accept: make(chan *conn.Conn),
name: name,
mux: v,
accept: make(chan *conn.Conn),
proxyType: proxytype,
clientIp: clientIp,
clientPort: clientPort,
serverPort: serverPort,
}
v.registryMap[name] = l
return l, nil
@@ -111,9 +119,13 @@ func (v *VhostMuxer) handle(c *conn.Conn) {
}
type Listener struct {
name string
mux *VhostMuxer // for closing VhostMuxer
accept chan *conn.Conn
name string
mux *VhostMuxer // for closing VhostMuxer
accept chan *conn.Conn
proxyType string //suppor http host rewrite
clientIp string
clientPort int64
serverPort int64
}
func (l *Listener) Accept() (*conn.Conn, error) {
@@ -121,6 +133,20 @@ func (l *Listener) Accept() (*conn.Conn, error) {
if !ok {
return nil, fmt.Errorf("Listener closed")
}
if net.ParseIP(l.clientIp) == nil && l.proxyType == "http" {
if (l.name != l.clientIp) || (l.serverPort != l.clientPort) {
clientHost := l.clientIp
if l.clientPort != 80 {
strPort := fmt.Sprintf(":%d", l.clientPort)
clientHost += strPort
}
retConn, err := HostNameRewrite(conn, clientHost)
if err != nil {
return nil, fmt.Errorf("http host rewrite failed")
}
conn.SetTcpConn(retConn)
}
}
return conn, nil
}
@@ -166,3 +192,9 @@ func (sc *sharedConn) Read(p []byte) (n int, err error) {
sc.Unlock()
return
}
func (sc *sharedConn) WriteBuff(buffer []byte) (err error) {
sc.buff.Reset()
_, err = sc.buff.Write(buffer)
return err
}