summaryrefslogtreecommitdiff
path: root/client/proxy.go
blob: e7757cafdef0d6fb243b36834a10061a6c6e37e6 (plain)
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
package client

import (
	"encoding/json"

	"codeberg.org/eduVPN/proxyguard"

	"github.com/eduvpn/eduvpn-common/i18nerr"
	"github.com/eduvpn/eduvpn-common/internal/log"
	"github.com/eduvpn/eduvpn-common/types/cookie"
)

// ProxyLogger is defined here such that we can update the proxyguard logger
type ProxyLogger struct{}

// Logf logs a message with parameters
func (pl *ProxyLogger) Logf(msg string, params ...interface{}) {
	log.Logger.Infof("[Proxyguard] "+msg, params...)
}

// Log logs a message
func (pl *ProxyLogger) Log(msg string) {
	log.Logger.Infof("[Proxyguard] %s", msg)
}

// StartProxyguard starts proxyguard for proxied WireGuard connections
func (c *Client) StartProxyguard(ck *cookie.Cookie, listen string, tcpsp int, peer string, gotFD func(fd int, pips string), ready func()) error {
	var err error
	proxyguard.UpdateLogger(&ProxyLogger{})

	proxyc := proxyguard.Client{
		Listen:        listen,
		TCPSourcePort: tcpsp,
		SetupSocket: func(fd int, pips []string) {
			if gotFD == nil {
				return
			}
			b, err := json.Marshal(pips)
			if err != nil {
				log.Logger.Errorf("marshalling peer IPs failed: %v", err)
				return
			}
			gotFD(fd, string(b))
		},
		Ready: ready,
	}

	// we set peer IPs to nil here as proxyguard already does a DNS request for us
	err = proxyc.Tunnel(ck.Context(), peer, nil)
	if err != nil {
		return i18nerr.Wrap(err, "The VPN proxy exited")
	}
	return err
}