compy/proxy/mitmlistener.go

48 lines
903 B
Go
Raw Normal View History

2015-03-28 22:07:40 +00:00
package proxy
import (
"crypto/tls"
"net"
)
type mitmListener struct {
c chan net.Conn
cf *certFaker
config *tls.Config
2015-03-28 22:07:40 +00:00
}
func newMitmListener(cf *certFaker, config *tls.Config) *mitmListener {
2015-03-28 22:07:40 +00:00
return &mitmListener{
c: make(chan net.Conn),
cf: cf,
config: config,
2015-03-28 22:07:40 +00:00
}
}
func (l *mitmListener) Accept() (net.Conn, error) {
return <-l.c, nil
}
func (l *mitmListener) Close() error {
return nil
}
func (l *mitmListener) Addr() net.Addr {
return nil
}
func (l *mitmListener) Serve(conn net.Conn, host string) (net.Conn, error) {
sconn, err := tls.Dial("tcp", host, l.config)
2015-03-28 22:07:40 +00:00
if err != nil {
return nil, err
}
fakeCert, err := l.cf.FakeCert(sconn.ConnectionState().PeerCertificates[0])
if err != nil {
sconn.Close()
return nil, err
}
tlsconf := &tls.Config{Certificates: []tls.Certificate{*fakeCert}}
l.c <- tls.Server(conn, tlsconf)
return sconn, nil
}