Skip to content

Commit 7bda280

Browse files
committed
feat: implemented daemonless mode
This is a simpler mode of operation when only a single proxy is required. Start and stop the whole program with a single command instead of running in multiple terminals, etc.
1 parent 117588c commit 7bda280

File tree

7 files changed

+52
-11
lines changed

7 files changed

+52
-11
lines changed

bin/vproxy/flags.go

+13
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,24 @@ vproxy connect hello.local:8888 -- vproxy hello
110110
Value: "127.0.0.1",
111111
Usage: "Server host IP",
112112
},
113+
&cli.StringFlag{
114+
Name: "listen",
115+
Aliases: []string{"l"},
116+
Value: "127.0.0.1",
117+
Usage: "IP to listen on (when in single-client mode)",
118+
Hidden: hideFlags,
119+
},
113120
&cli.IntFlag{
114121
Name: "http",
115122
Value: 80,
116123
Usage: "Server HTTP port",
117124
},
125+
&cli.IntFlag{
126+
Name: "https",
127+
Value: 443,
128+
Usage: "Server HTTPS port (when in single-client mode)",
129+
Hidden: hideFlags,
130+
},
118131
&cli.StringSliceFlag{
119132
Name: "bind",
120133
Usage: "Bind hostname to local port (e.g., app.local.com:7000)",

bin/vproxy/main.go

+22-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"path/filepath"
99
"regexp"
1010
"strings"
11+
"time"
1112

1213
"github.com/jittering/truststore"
1314
"github.com/jittering/vproxy"
@@ -38,7 +39,6 @@ func printVersion(c *cli.Context) error {
3839
}
3940

4041
func connectVhost(c *cli.Context) error {
41-
4242
// collect and validate binds
4343
args := c.Args().Slice()
4444
binds := c.StringSlice("bind")
@@ -67,7 +67,27 @@ func connectVhost(c *cli.Context) error {
6767

6868
client := createClient(c)
6969
if !client.IsDaemonRunning() {
70-
return fmt.Errorf("daemon not running on localhost")
70+
fmt.Println("[*] warning: daemon not running on localhost. running in single-client mode")
71+
72+
// start server with defaults
73+
c.Set("listen", "127.0.0.1")
74+
c.Set("https", "443")
75+
go startDaemon(c)
76+
77+
// start command, if avail
78+
client.RunCommand(args)
79+
80+
// wait for server
81+
for {
82+
if client.IsDaemonRunning() {
83+
break
84+
}
85+
time.Sleep(100 * time.Millisecond)
86+
}
87+
88+
// bind
89+
client.AddBinding(binds[0], false)
90+
return nil
7191
}
7292

7393
client.AddBindings(binds, c.Bool("detach"), args)

client.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package vproxy
33
import (
44
"bufio"
55
"fmt"
6+
"io"
67
"io/ioutil"
78
"log"
89
"net/http"
@@ -31,20 +32,20 @@ func (c *Client) AddBindings(binds []string, detach bool, args []string) {
3132
os.Exit(1)
3233
}
3334

34-
c.runCommand(args)
35+
c.RunCommand(args)
3536

3637
c.wg = &sync.WaitGroup{}
3738
for _, bind := range binds {
3839
c.wg.Add(1)
39-
go c.addBinding(bind, detach)
40+
go c.AddBinding(bind, detach)
4041
c.wg.Wait()
4142
}
4243

4344
// c.wg.Add(1)
4445
c.wg.Wait()
4546
}
4647

47-
func (c *Client) runCommand(args []string) {
48+
func (c *Client) RunCommand(args []string) {
4849
// run command, if given
4950
if len(args) == 0 {
5051
return
@@ -66,7 +67,8 @@ func (c *Client) runCommand(args []string) {
6667
}()
6768
}
6869

69-
func (c *Client) addBinding(bind string, detach bool) {
70+
// Add single binding. Blocks when detach=false
71+
func (c *Client) AddBinding(bind string, detach bool) {
7072
data := url.Values{}
7173
data.Add("binding", bind)
7274

@@ -81,13 +83,17 @@ func (c *Client) addBinding(bind string, detach bool) {
8183
stopCommand(c.cmd)
8284
log.Fatalf("error registering client: %s\n", err)
8385
}
86+
b, err := io.ReadAll(res.Body)
87+
if err == nil {
88+
fmt.Println(string(b))
89+
}
90+
res.Body.Close()
8491

8592
if detach {
8693
c.wg.Done()
8794
} else {
8895
c.Tail(s[0], true)
8996
}
90-
res.Body.Close()
9197
}
9298

9399
func (c *Client) Tail(hostname string, follow bool) {

cmd.go

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ func runCommand(args []string) *exec.Cmd {
2525
}
2626

2727
func stopCommand(cmd *exec.Cmd) {
28-
2928
if cmd == nil {
3029
return
3130
}

daemon.go

+4
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ func (d *Daemon) relayLogsUntilClose(vhost *Vhost, w http.ResponseWriter, reqCtx
213213
return
214214
}
215215

216+
// initial flush to open the stream
217+
fmt.Fprint(w, "")
218+
flusher.Flush()
219+
216220
logChan := vhost.NewLogListener()
217221

218222
// read existing logs first

logged_handler.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,15 @@ func (lh *LoggedHandler) CreateTLSConfig() *tls.Config {
7373
// Add default internal cert
7474
cert, err := tls.LoadX509KeyPair(lh.defaultCert, lh.defaultKey)
7575
if err != nil {
76-
log.Fatal("failed to load keypair:", err)
76+
log.Fatal("failed to load internal keypair:", err)
7777
}
7878
cfg.Certificates = append(cfg.Certificates, cert)
7979

8080
// add cert for each vhost
8181
for _, server := range lh.vhostMux.Servers {
8282
cert, err := tls.LoadX509KeyPair(server.Cert, server.Key)
8383
if err != nil {
84-
log.Fatal("failed to load keypair:", err)
84+
log.Fatalf("failed to load keypair (%s, %s): %s", server.Cert, server.Key, err)
8585
}
8686
cfg.Certificates = append(cfg.Certificates, cert)
8787
}

simpleproxy.go

-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ func (t *proxyTransport) RoundTrip(request *http.Request) (*http.Response, error
6060
}
6161

6262
func createProxyTransport(targetURL url.URL, vhost string) *proxyTransport {
63-
fmt.Println("creating new transport")
6463
t := &proxyTransport{errMsg: fmt.Sprintf(badGatewayMessage, targetURL.String(), vhost)}
6564
t.transport = http.DefaultTransport.(*http.Transport).Clone()
6665
t.transport.MaxConnsPerHost = 0 // unlim

0 commit comments

Comments
 (0)