-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.go
126 lines (106 loc) · 3.05 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2015 Dominique Feyer <[email protected]>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"github.com/dfeyer/flow-debugproxy/config"
"github.com/dfeyer/flow-debugproxy/errorhandler"
"github.com/dfeyer/flow-debugproxy/logger"
"github.com/dfeyer/flow-debugproxy/pathmapperfactory"
"github.com/dfeyer/flow-debugproxy/pathmapping"
"github.com/dfeyer/flow-debugproxy/xdebugproxy"
// Register available path mapper
_ "github.com/dfeyer/flow-debugproxy/dummypathmapper"
_ "github.com/dfeyer/flow-debugproxy/flowpathmapper"
"github.com/urfave/cli"
"net"
"os"
"strings"
)
func main() {
app := cli.NewApp()
app.Name = "flow-debugproxy"
app.Usage = "Flow Framework xDebug proxy"
app.Version = "1.0.1"
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "xdebug, l",
Value: "127.0.0.1:9000",
Usage: "Listen address IP and port number",
},
&cli.StringFlag{
Name: "ide, I",
Value: "127.0.0.1:9010",
Usage: "Bind address IP and port number",
},
&cli.StringFlag{
Name: "context, c",
Value: "Development",
Usage: "The context to run as",
},
&cli.StringFlag{
Name: "localroot, r",
Value: "",
Usage: "Local project root for remote debugging",
},
&cli.StringFlag{
Name: "framework",
Value: "flow",
Usage: "Framework support, currently on Flow framework (flow) or Dummy (dummy) is supported",
},
&cli.BoolFlag{
Name: "verbose",
Usage: "Verbose",
},
&cli.BoolFlag{
Name: "vv",
Usage: "Very verbose",
},
&cli.BoolFlag{
Name: "debug",
Usage: "Show debug output",
},
}
app.Action = func(cli *cli.Context) error {
c := &config.Config{
Context: cli.String("context"),
Framework: cli.String("framework"),
LocalRoot: strings.TrimRight(cli.String("localroot"), "/"),
Verbose: cli.Bool("verbose") || cli.Bool("vv"),
VeryVerbose: cli.Bool("vv"),
Debug: cli.Bool("debug"),
}
log := &logger.Logger{
Config: c,
}
laddr, raddr, listener := setupNetworkConnection(cli.String("xdebug"), cli.String("ide"), log)
log.Info("Debugger from %v\nIDE from %v\n", laddr, raddr)
pathMapping := &pathmapping.PathMapping{}
pathMapper, err := pathmapperfactory.Create(c, pathMapping, log)
errorhandler.PanicHandling(err, log)
for {
conn, err := listener.AcceptTCP()
if err != nil {
log.Warn("Failed to accept connection '%s'\n", err)
continue
}
proxy := &xdebugproxy.Proxy{
Lconn: conn,
Raddr: raddr,
PathMapper: pathMapper,
Config: c,
}
go proxy.Start()
}
}
app.Run(os.Args)
}
func setupNetworkConnection(xdebugAddr string, ideAddr string, log *logger.Logger) (*net.TCPAddr, *net.TCPAddr, *net.TCPListener) {
laddr, err := net.ResolveTCPAddr("tcp", xdebugAddr)
errorhandler.PanicHandling(err, log)
raddr, err := net.ResolveTCPAddr("tcp", ideAddr)
errorhandler.PanicHandling(err, log)
listener, err := net.ListenTCP("tcp", laddr)
errorhandler.PanicHandling(err, log)
return laddr, raddr, listener
}