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

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open

[client] Add embeddable library #3239

wants to merge 12 commits into from

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/[email protected]
go mod edit -replace=github.com/cloudflare/circl=github.com/cunicu/[email protected]
go mod edit -replace=github.com/pion/ice/v3=github.com/netbirdio/ice/[email protected]
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.

Copy link

sonarqubecloud bot commented Feb 7, 2025

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