@@ -69,7 +69,7 @@ import (
6969// The order that routes are added in matters; each is matched in the order
7070// registered.
7171type Proxy struct {
72- routes map [string ][]route // ip:port => route
72+ routes map [string ][]route // ip:port => routes
7373
7474 lns []net.Listener
7575 donec chan struct {} // closed before err
@@ -81,13 +81,8 @@ type Proxy struct {
8181 ListenFunc func (net , laddr string ) (net.Listener , error )
8282}
8383
84- type route struct {
85- matcher matcher
86- target Target
87- }
88-
89- type matcher interface {
90- match (* bufio.Reader ) bool
84+ type route interface {
85+ match (* bufio.Reader ) Target
9186}
9287
9388func (p * Proxy ) netListen () func (net , laddr string ) (net.Listener , error ) {
@@ -97,11 +92,11 @@ func (p *Proxy) netListen() func(net, laddr string) (net.Listener, error) {
9792 return net .Listen
9893}
9994
100- func (p * Proxy ) addRoute (ipPort string , matcher matcher , dest Target ) {
95+ func (p * Proxy ) addRoute (ipPort string , r route ) {
10196 if p .routes == nil {
10297 p .routes = make (map [string ][]route )
10398 }
104- p .routes [ipPort ] = append (p .routes [ipPort ], route { matcher , dest } )
99+ p .routes [ipPort ] = append (p .routes [ipPort ], r )
105100}
106101
107102// AddRoute appends an always-matching route to the ipPort listener,
@@ -112,12 +107,14 @@ func (p *Proxy) addRoute(ipPort string, matcher matcher, dest Target) {
112107//
113108// The ipPort is any valid net.Listen TCP address.
114109func (p * Proxy ) AddRoute (ipPort string , dest Target ) {
115- p .addRoute (ipPort , alwaysMatch {}, dest )
110+ p .addRoute (ipPort , alwaysMatch {dest } )
116111}
117112
118- type alwaysMatch struct {}
113+ type alwaysMatch struct {
114+ t Target
115+ }
119116
120- func (alwaysMatch ) match (* bufio.Reader ) bool { return true }
117+ func (m alwaysMatch ) match (* bufio.Reader ) Target { return m . t }
121118
122119// Run is calls Start, and then Wait.
123120//
@@ -194,15 +191,15 @@ func (p *Proxy) serveListener(ret chan<- error, ln net.Listener, routes []route)
194191func (p * Proxy ) serveConn (c net.Conn , routes []route ) bool {
195192 br := bufio .NewReader (c )
196193 for _ , route := range routes {
197- if route .matcher . match (br ) {
194+ if target := route .match (br ); target != nil {
198195 if n := br .Buffered (); n > 0 {
199196 peeked , _ := br .Peek (br .Buffered ())
200197 c = & Conn {
201198 Peeked : peeked ,
202199 Conn : c ,
203200 }
204201 }
205- route . target .HandleConn (c )
202+ target .HandleConn (c )
206203 return true
207204 }
208205 }
0 commit comments