2022-03-01 14:07:33 +01:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"net"
|
|
|
|
)
|
|
|
|
|
2023-04-13 17:00:01 +02:00
|
|
|
// NoProxy is used just to configure WireGuard without any local proxy in between.
|
|
|
|
// Used when the WireGuard interface is userspace and uses bind.ICEBind
|
2022-03-01 14:07:33 +01:00
|
|
|
type NoProxy struct {
|
|
|
|
config Config
|
|
|
|
}
|
|
|
|
|
2023-04-13 17:00:01 +02:00
|
|
|
// NewNoProxy creates a new NoProxy with a provided config
|
|
|
|
func NewNoProxy(config Config) *NoProxy {
|
|
|
|
return &NoProxy{config: config}
|
2022-03-01 14:07:33 +01:00
|
|
|
}
|
|
|
|
|
2023-04-13 17:00:01 +02:00
|
|
|
// Close removes peer from the WireGuard interface
|
2022-03-01 14:07:33 +01:00
|
|
|
func (p *NoProxy) Close() error {
|
2022-03-02 14:50:22 +01:00
|
|
|
err := p.config.WgInterface.RemovePeer(p.config.RemoteKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-01 14:07:33 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-13 17:00:01 +02:00
|
|
|
// Start just updates WireGuard peer with the remote address
|
2022-03-01 14:07:33 +01:00
|
|
|
func (p *NoProxy) Start(remoteConn net.Conn) error {
|
|
|
|
|
2023-04-13 17:00:01 +02:00
|
|
|
log.Debugf("using NoProxy to connect to peer %s at %s", p.config.RemoteKey, remoteConn.RemoteAddr().String())
|
2022-03-01 14:07:33 +01:00
|
|
|
addr, err := net.ResolveUDPAddr("udp", remoteConn.RemoteAddr().String())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-13 17:00:01 +02:00
|
|
|
return p.config.WgInterface.UpdatePeer(p.config.RemoteKey, p.config.AllowedIps, DefaultWgKeepAlive,
|
2022-03-01 14:07:33 +01:00
|
|
|
addr, p.config.PreSharedKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *NoProxy) Type() Type {
|
|
|
|
return TypeNoProxy
|
|
|
|
}
|