Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[client] Add embeddable library #3239

Merged
merged 13 commits into from
Feb 20, 2025
Merged

[client] Add embeddable library #3239

merged 13 commits into from
Feb 20, 2025

Conversation

lixmal
Copy link
Contributor

@lixmal lixmal commented Jan 26, 2025

Describe your changes

  • Add embed package
  • Fix default routes for netstack
  • Fix DNS for netstack

Example listen program:

package main

import (
	"context"
	"errors"
	"fmt"
	"io"
	"log"
	"net/http"
	"os"
	"os/signal"
	"syscall"
	"time"

	netbird "github.com/netbirdio/netbird/client/embed"
)

func main() {
	// Create client with setup key and device name
	client, err := netbird.New(netbird.Options{
		DeviceName:    "http-server",
		SetupKey:      os.Getenv("NB_SETUP_KEY"),
		ManagementURL: os.Getenv("NB_MANAGEMENT_URL"),
		LogOutput:     io.Discard,
	})
	if err != nil {
		log.Fatal(err)
	}

	// Start with timeout
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()

	if err := client.Start(ctx); err != nil {
		log.Fatal(err)
	}

	// Create HTTP server
	mux := http.NewServeMux()
	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Printf("Request from %s: %s %s\n", r.RemoteAddr, r.Method, r.URL.Path)
		fmt.Fprintf(w, "Hello from netbird!")
	})

	// Listen on netbird network
	l, err := client.ListenTCP(":8080")
	if err != nil {
		log.Fatal(err)
	}

	server := &http.Server{Handler: mux}
	go func() {
		if err := server.Serve(l); !errors.Is(err, http.ErrServerClosed) {
			log.Printf("HTTP server error: %v", err)
		}
	}()

	log.Printf("HTTP server listening on netbird network port 8080")

	// Handle shutdown
	stop := make(chan os.Signal, 1)
	signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
	<-stop

	shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	if err := server.Shutdown(shutdownCtx); err != nil {
		log.Printf("HTTP shutdown error: %v", err)
	}

	if err := client.Stop(shutdownCtx); err != nil {
		log.Printf("Netbird shutdown error: %v", err)
	}
}

Run:

go mod init nbembed
go mod edit -replace=golang.zx2c4.com/wireguard=github.com/netbirdio/wireguard-go@v0.0.0-20241230120307-6a676aebaaf6
go mod edit -replace=github.com/cloudflare/circl=github.com/cunicu/circl@v0.0.0-20230801113412-fec58fc7b5f6
go mod edit -replace=github.com/pion/ice/v3=github.com/netbirdio/ice/v3@v3.0.0-20240315174635-e72a50fcb64e
go mod edit -replace=github.com/libp2p/go-netroute=github.com/netbirdio/go-netroute@v0.0.0-20240611143515-f59b0e1d3944
go get github.com/netbirdio/netbird@embed-netbird
go mod tidy

NB_SETUP_KEY=xxx  go run .

Issue ticket number and link

Checklist

  • Is it a bug fix
  • Is a typo/documentation fix
  • Is a feature enhancement
  • It is a refactor
  • Created tests that fail without the change (if possible)
  • Extended the README / documentation, if necessary

@lixmal lixmal marked this pull request as ready for review January 29, 2025 23:10
@pappz
Copy link
Contributor

pappz commented Feb 5, 2025

I recommend creating a doc file in the embed package with your example code.

pappz
pappz previously approved these changes Feb 20, 2025
@lixmal lixmal merged commit 631ef4e into main Feb 20, 2025
17 checks passed
@lixmal lixmal deleted the embed-netbird branch February 20, 2025 12:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants