Skip to content

Commit 631ef4e

Browse files
authored
[client] Add embeddable library (#3239)
1 parent 39986b0 commit 631ef4e

22 files changed

+648
-51
lines changed

client/embed/doc.go

+167
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
// Package embed provides a way to embed the NetBird client directly
2+
// into Go programs without requiring a separate NetBird client installation.
3+
package embed
4+
5+
// Basic Usage:
6+
//
7+
// client, err := embed.New(embed.Options{
8+
// DeviceName: "my-service",
9+
// SetupKey: os.Getenv("NB_SETUP_KEY"),
10+
// ManagementURL: os.Getenv("NB_MANAGEMENT_URL"),
11+
// })
12+
// if err != nil {
13+
// log.Fatal(err)
14+
// }
15+
//
16+
// ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
17+
// defer cancel()
18+
// if err := client.Start(ctx); err != nil {
19+
// log.Fatal(err)
20+
// }
21+
//
22+
// Complete HTTP Server Example:
23+
//
24+
// package main
25+
//
26+
// import (
27+
// "context"
28+
// "fmt"
29+
// "log"
30+
// "net/http"
31+
// "os"
32+
// "os/signal"
33+
// "syscall"
34+
// "time"
35+
//
36+
// netbird "github.com/netbirdio/netbird/client/embed"
37+
// )
38+
//
39+
// func main() {
40+
// // Create client with setup key and device name
41+
// client, err := netbird.New(netbird.Options{
42+
// DeviceName: "http-server",
43+
// SetupKey: os.Getenv("NB_SETUP_KEY"),
44+
// ManagementURL: os.Getenv("NB_MANAGEMENT_URL"),
45+
// LogOutput: io.Discard,
46+
// })
47+
// if err != nil {
48+
// log.Fatal(err)
49+
// }
50+
//
51+
// // Start with timeout
52+
// ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
53+
// defer cancel()
54+
// if err := client.Start(ctx); err != nil {
55+
// log.Fatal(err)
56+
// }
57+
//
58+
// // Create HTTP server
59+
// mux := http.NewServeMux()
60+
// mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
61+
// fmt.Printf("Request from %s: %s %s\n", r.RemoteAddr, r.Method, r.URL.Path)
62+
// fmt.Fprintf(w, "Hello from netbird!")
63+
// })
64+
//
65+
// // Listen on netbird network
66+
// l, err := client.ListenTCP(":8080")
67+
// if err != nil {
68+
// log.Fatal(err)
69+
// }
70+
//
71+
// server := &http.Server{Handler: mux}
72+
// go func() {
73+
// if err := server.Serve(l); !errors.Is(err, http.ErrServerClosed) {
74+
// log.Printf("HTTP server error: %v", err)
75+
// }
76+
// }()
77+
//
78+
// log.Printf("HTTP server listening on netbird network port 8080")
79+
//
80+
// // Handle shutdown
81+
// stop := make(chan os.Signal, 1)
82+
// signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
83+
// <-stop
84+
//
85+
// shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
86+
// defer cancel()
87+
//
88+
// if err := server.Shutdown(shutdownCtx); err != nil {
89+
// log.Printf("HTTP shutdown error: %v", err)
90+
// }
91+
// if err := client.Stop(shutdownCtx); err != nil {
92+
// log.Printf("Netbird shutdown error: %v", err)
93+
// }
94+
// }
95+
//
96+
// Complete HTTP Client Example:
97+
//
98+
// package main
99+
//
100+
// import (
101+
// "context"
102+
// "fmt"
103+
// "io"
104+
// "log"
105+
// "os"
106+
// "time"
107+
//
108+
// netbird "github.com/netbirdio/netbird/client/embed"
109+
// )
110+
//
111+
// func main() {
112+
// // Create client with setup key and device name
113+
// client, err := netbird.New(netbird.Options{
114+
// DeviceName: "http-client",
115+
// SetupKey: os.Getenv("NB_SETUP_KEY"),
116+
// ManagementURL: os.Getenv("NB_MANAGEMENT_URL"),
117+
// LogOutput: io.Discard,
118+
// })
119+
// if err != nil {
120+
// log.Fatal(err)
121+
// }
122+
//
123+
// // Start with timeout
124+
// ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
125+
// defer cancel()
126+
//
127+
// if err := client.Start(ctx); err != nil {
128+
// log.Fatal(err)
129+
// }
130+
//
131+
// // Create HTTP client that uses netbird network
132+
// httpClient := client.NewHTTPClient()
133+
// httpClient.Timeout = 10 * time.Second
134+
//
135+
// // Make request to server in netbird network
136+
// target := os.Getenv("NB_TARGET")
137+
// resp, err := httpClient.Get(target)
138+
// if err != nil {
139+
// log.Fatal(err)
140+
// }
141+
// defer resp.Body.Close()
142+
//
143+
// // Read and print response
144+
// body, err := io.ReadAll(resp.Body)
145+
// if err != nil {
146+
// log.Fatal(err)
147+
// }
148+
//
149+
// fmt.Printf("Response from server: %s\n", string(body))
150+
//
151+
// // Clean shutdown
152+
// shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
153+
// defer cancel()
154+
//
155+
// if err := client.Stop(shutdownCtx); err != nil {
156+
// log.Printf("Netbird shutdown error: %v", err)
157+
// }
158+
// }
159+
//
160+
// The package provides several methods for network operations:
161+
// - Dial: Creates outbound connections
162+
// - ListenTCP: Creates TCP listeners
163+
// - ListenUDP: Creates UDP listeners
164+
//
165+
// By default, the embed package uses userspace networking mode, which doesn't
166+
// require root/admin privileges. For production deployments, consider setting
167+
// appropriate config and state paths for persistence.

0 commit comments

Comments
 (0)