Skip to content

Commit 68ccca2

Browse files
author
Andreas Sacher
committed
TASK: listen to multiple ports and associate a context with each port listened to
1 parent 41b91ff commit 68ccca2

File tree

3 files changed

+82
-41
lines changed

3 files changed

+82
-41
lines changed

Dockerfile

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ COPY --from=build-env /app/flow-debugproxy /app/
2222

2323
ENV ADDITIONAL_ARGS ""
2424

25-
ENV XDEBUG_PORT 9010
25+
ENV XDEBUG 9003:Development
2626

27-
ENV IDE_IP 127.0.0.1
28-
ENV IDE_PORT 9000
27+
ENV IDE_IP host.docker.internal
28+
ENV IDE_PORT 9010
2929

3030
ENV FRAMEWORK "flow"
3131

32-
ENTRYPOINT ["sh", "-c", "./flow-debugproxy --xdebug 0.0.0.0:${XDEBUG_PORT} --framework ${FRAMEWORK} --ide ${IDE_IP}:${IDE_PORT} ${ADDITIONAL_ARGS}"]
32+
ENV LOCAL_ROOT ""
33+
34+
ENTRYPOINT ["sh", "-c", "./flow-debugproxy --xdebug ${XDEBUG} --framework ${FRAMEWORK} --ide ${IDE_IP}:${IDE_PORT} --localroot \"${LOCAL_ROOT}\" ${ADDITIONAL_ARGS}"]

main.go

Lines changed: 75 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package main
66

77
import (
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
}

xdebugproxy/xdebugproxy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (p *Proxy) Start() {
6060
defer close(p.pipeErrors)
6161

6262
// display both ends
63-
p.log("Opened %s >>> %s", p.Lconn.RemoteAddr().String(), p.rconn.RemoteAddr().String())
63+
p.log("Opened %s >>> %s -- context %s", p.Lconn.RemoteAddr().String(), p.rconn.RemoteAddr().String(), p.Config.Context)
6464
// bidirectional copy
6565
go p.pipe(p.Lconn, p.rconn)
6666
go p.pipe(p.rconn, p.Lconn)

0 commit comments

Comments
 (0)