-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #614 from aledbf/refactor-passthrough
Refactor nginx ssl passthrough
- Loading branch information
Showing
15 changed files
with
708 additions
and
122 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net" | ||
|
||
"github.com/golang/glog" | ||
"github.com/paultag/sniff/parser" | ||
) | ||
|
||
type server struct { | ||
Hostname string | ||
IP string | ||
Port int | ||
} | ||
|
||
type proxy struct { | ||
ServerList []*server | ||
Default *server | ||
} | ||
|
||
func (p *proxy) Get(host string) *server { | ||
for _, s := range p.ServerList { | ||
if s.Hostname == host { | ||
return s | ||
} | ||
} | ||
|
||
return &server{ | ||
Hostname: "localhost", | ||
IP: "127.0.0.1", | ||
Port: 442, | ||
} | ||
} | ||
|
||
func (p *proxy) Handle(conn net.Conn) { | ||
defer conn.Close() | ||
data := make([]byte, 4096) | ||
|
||
length, err := conn.Read(data) | ||
if err != nil { | ||
glog.V(4).Infof("error reading the first 4k of the connection: %s", err) | ||
return | ||
} | ||
|
||
var proxy *server | ||
hostname, err := parser.GetHostname(data[:]) | ||
if err == nil { | ||
glog.V(3).Infof("parsed hostname from TLS Client Hello: %s", hostname) | ||
proxy = p.Get(hostname) | ||
if proxy == nil { | ||
return | ||
} | ||
} else { | ||
proxy = p.Default | ||
if proxy == nil { | ||
return | ||
} | ||
} | ||
|
||
clientConn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", proxy.IP, proxy.Port)) | ||
if err != nil { | ||
return | ||
} | ||
defer clientConn.Close() | ||
|
||
_, err = clientConn.Write(data[:length]) | ||
if err != nil { | ||
clientConn.Close() | ||
} | ||
pipe(clientConn, conn) | ||
} | ||
|
||
func pipe(client, server net.Conn) { | ||
doCopy := func(s, c net.Conn, cancel chan<- bool) { | ||
io.Copy(s, c) | ||
cancel <- true | ||
} | ||
|
||
cancel := make(chan bool, 2) | ||
|
||
go doCopy(server, client, cancel) | ||
go doCopy(client, server, cancel) | ||
|
||
select { | ||
case <-cancel: | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.