55package main
66
77import (
8+ "fmt"
89 golog "log"
910
1011 "github.com/dfeyer/flow-debugproxy/config"
@@ -34,19 +35,14 @@ func main() {
3435 app .Flags = []cli.Flag {
3536 & cli.StringFlag {
3637 Name : "xdebug, l" ,
37- Value : "127.0.0.1:9000 " ,
38+ Value : "Development:9003 " ,
3839 Usage : "Listen address IP and port number" ,
3940 },
4041 & cli.StringFlag {
4142 Name : "ide, I" ,
4243 Value : "127.0.0.1:9010" ,
4344 Usage : "Bind address IP and port number" ,
4445 },
45- & cli.StringFlag {
46- Name : "context, c" ,
47- Value : "Development" ,
48- Usage : "The context to run as" ,
49- },
5046 & cli.StringFlag {
5147 Name : "localroot, r" ,
5248 Value : "" ,
@@ -73,7 +69,7 @@ func main() {
7369
7470 app .Action = func (cli * cli.Context ) error {
7571 c := & config.Config {
76- Context : cli . String ( "context" ) ,
72+ Context : "" ,
7773 Framework : cli .String ("framework" ),
7874 LocalRoot : strings .TrimRight (cli .String ("localroot" ), "/" ),
7975 Verbose : cli .Bool ("verbose" ) || cli .Bool ("vv" ),
@@ -85,43 +81,58 @@ func main() {
8581 Config : c ,
8682 }
8783
88- laddr , raddr , listener , err := setupNetworkConnection (cli .String ("xdebug" ), cli .String ("ide" ))
84+ listener , raddr , err := setupNetworkConnection (strings . Split ( cli .String ("xdebug" ), ", " ), cli .String ("ide" ))
8985 if err != nil {
9086 log .Warn (err .Error ())
9187 os .Exit (1 )
9288 }
9389
94- log .Info ("Debugger from %v" , laddr )
90+ for _ , listenerWithContext := range listener {
91+ log .Info ("Debugger from %v for context %v" , listenerWithContext .addr , listenerWithContext .context )
92+ }
9593 log .Info ("IDE from %v" , raddr )
9694 if c .Verbose {
97- log .Info ("Context %v" , c .Context )
9895 log .Info ("Framework %v" , c .Framework )
9996 log .Info ("Local Root %v" , c .LocalRoot )
10097 log .Info ("Verbose %v" , c .Verbose )
10198 log .Info ("Very Verbose %v" , c .VeryVerbose )
10299 log .Info ("Debug %v" , c .Debug )
103100 }
104101
105- pathMapping := & pathmapping. PathMapping {}
106- pathMapper , err := pathmapperfactory . Create ( c , pathMapping , log )
107- if err != nil {
108- log . Warn ( err . Error ())
109- os . Exit ( 1 )
110- }
102+ connections := make ( chan * xdebugproxy. Proxy )
103+ for _ , listenerWithContext := range listener {
104+ listenerWithContext := listenerWithContext
105+ originalConfig := * c
106+ proxyConfig := originalConfig // copy config
107+ proxyConfig . Context = listenerWithContext . context
111108
112- for {
113- conn , err := listener . AcceptTCP ( )
109+ pathMapping := & pathmapping. PathMapping {}
110+ pathMapper , err := pathmapperfactory . Create ( & proxyConfig , pathMapping , log )
114111 if err != nil {
115- log .Warn ("Failed to accept connection '%s' \n " , err )
116- continue
112+ log .Warn (err . Error () )
113+ os . Exit ( 1 )
117114 }
118115
119- proxy := & xdebugproxy.Proxy {
120- Lconn : conn ,
121- Raddr : raddr ,
122- PathMapper : pathMapper ,
123- Config : c ,
124- }
116+ go func () {
117+ for {
118+ conn , err := listenerWithContext .listener .AcceptTCP ()
119+ if err != nil {
120+ log .Warn ("Failed to accept connection '%s'\n " , err )
121+ continue
122+ }
123+
124+ connections <- & xdebugproxy.Proxy {
125+ Lconn : conn ,
126+ Raddr : raddr ,
127+ PathMapper : pathMapper ,
128+ Config : & proxyConfig ,
129+ }
130+ }
131+ }()
132+ }
133+
134+ for {
135+ proxy := <- connections
125136 go proxy .Start ()
126137 }
127138 }
@@ -133,21 +144,49 @@ func main() {
133144 }
134145}
135146
136- func setupNetworkConnection (xdebugAddr string , ideAddr string ) (* net.TCPAddr , * net.TCPAddr , * net.TCPListener , error ) {
137- laddr , err := net .ResolveTCPAddr ("tcp" , xdebugAddr )
138- if err != nil {
139- return nil , nil , nil , err
147+ type listenerWithContext struct {
148+ addr * net.TCPAddr
149+ listener * net.TCPListener
150+ context string
151+ }
152+
153+ func splitContextAndPort (contextWithPort string ) (string , string , error ) {
154+ contextAndPort := strings .Split (contextWithPort , ":" )
155+ if len (contextAndPort ) != 2 {
156+ return "" , "" , fmt .Errorf ("could not parse port and context information '%s'" , contextWithPort )
140157 }
158+ return contextAndPort [0 ], contextAndPort [1 ], nil
159+ }
141160
142- raddr , err := net .ResolveTCPAddr ("tcp" , ideAddr )
143- if err != nil {
144- return nil , nil , nil , err
161+ func setupNetworkConnection (xdebugAddr []string , ideAddr string ) ([]* listenerWithContext , * net.TCPAddr , error ) {
162+ listener := make ([]* listenerWithContext , 0 , len (xdebugAddr ))
163+ for _ , portWithContext := range xdebugAddr {
164+ context , portStr , err := splitContextAndPort (portWithContext )
165+ if err != nil {
166+ return nil , nil , err
167+ }
168+
169+ addr , err := net .ResolveTCPAddr ("tcp" , "0.0.0.0:" + portStr )
170+ if err != nil {
171+ return nil , nil , err
172+ }
173+
174+ l , err := net .ListenTCP ("tcp" , addr )
175+ if err != nil {
176+ return nil , nil , err
177+ }
178+
179+ listener = append (listener , & listenerWithContext {
180+ addr : addr ,
181+ listener : l ,
182+ context : context ,
183+ })
145184 }
146185
147- listener , err := net .ListenTCP ("tcp" , laddr )
186+ raddr , err := net .ResolveTCPAddr ("tcp" , ideAddr )
148187 if err != nil {
149- return nil , nil , nil , err
188+ return nil , nil , err
150189 }
151190
152- return laddr , raddr , listener , nil
191+ return listener , raddr , nil
153192}
0 commit comments