feat: store a wglink endpoint string

Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
HeshamTB 2024-03-18 00:58:17 +03:00
parent c9b5e56f14
commit 78b61aede4
Signed by: Hesham
GPG Key ID: 74876157D199B09E
3 changed files with 14 additions and 4 deletions

View File

@ -138,6 +138,13 @@ func createCliApp() *cli.App {
}
app.Flags = append(app.Flags, &wgInterfaceName)
wgEndpoint := cli.StringFlag{
Name: "endpoint",
Usage: "Wireguard endpoint domain or address without the port",
Value: "domain.name.notset",
}
app.Flags = append(app.Flags, &wgEndpoint)
wgPort := cli.IntFlag{
Name: "port",
Usage: "UDP Port for wireguard device",
@ -171,7 +178,7 @@ func createCliApp() *cli.App {
app.Action = func(ctx *cli.Context) error {
err := setup()
err := setup(ctx)
if err != nil {
return err
}
@ -182,7 +189,7 @@ func createCliApp() *cli.App {
return app
}
func setup() error {
func setup(ctx *cli.Context) error {
slog.Debug("Starting setup()")
uid := os.Getuid()
if uid == -1 {
@ -225,6 +232,7 @@ func setup() error {
InterfaceName,
&privateKey,
WgPort,
ctx.String("endpoint"),
)
if err != nil {
slog.Warn("Error while initlizing Wireguard netlink and device!")

View File

@ -31,6 +31,7 @@ func HandleGetNodeInfo(wgLink *WGLink) http.HandlerFunc {
proto.NodePublicInfo{
PublicKey: dev.PublicKey.String(),
UDPPort: dev.ListenPort,
Endpoint: wgLink.Endpoint,
// TODO: Send endpoint for clients to connect
},
)

View File

@ -21,14 +21,15 @@ type WGLink struct {
*netlink.LinkAttrs
*wgctrl.Client
IPPool
Endpoint string // Endpoint for clients to use when connecting to this link. Has no effect.
lock *sync.Mutex
}
// Retruns an existing or create a WGLink
func InitWGLink(ifName string, privateKey *wgtypes.Key, port int) (*WGLink, error){
func InitWGLink(ifName string, privateKey *wgtypes.Key, port int, endpoint string) (*WGLink, error){
attrs := netlink.NewLinkAttrs()
attrs.Name = ifName
wg := WGLink{LinkAttrs: &attrs}
wg := WGLink{LinkAttrs: &attrs, Endpoint: endpoint}
link, err := netlink.LinkByName(ifName)
if err != nil {
switch err.(type) {