### Connecting and Sending Data to p2p Echo Server in Go Source: https://github.com/sigcn/pg/blob/main/p2p/README.md This snippet illustrates how a p2p client (Peer2) can connect to a previously defined echo server (Peer1) using its unique peer ID. It sends a 'hello' message to the server and then waits for a response, demonstrating basic send and receive functionality. The `p2p` and `disco` packages are essential for this communication. ```Go packetConn := langs.Must(p2p.ListenPacket(disco.NewServer(serverURL, networkSecret))) defer packetConn.Close() // "uniqueString" is above echo server's address langs.Must(packetConn.WriteTo(peer.ID("uniqueString"), []byte("hello"))) packetConn.SetReadDeadline(time.Now().Add(time.Second)) buf := make([]byte, 1024) n, peerID, err := packetConn.ReadFrom(buf) if err !=nil { panic(err) } fmt.Println(peerID, ":", string(buf[:n])) // uniqueString : hello ``` -------------------------------- ### Initializing VPN Peer 2 in Go Source: https://github.com/sigcn/pg/blob/main/vpn/README.md This Go snippet configures the second VPN peer, mirroring the setup of the first. It sets up its own UDP listener, creates a virtual TUN interface, and registers the first peer's virtual and physical IP addresses. The VPN then runs, also handling OS signals for graceful shutdown. ```Go packetConn, err := net.ListenPacket("udp", "192.168.3.98:22335") if err != nil { panic(err) } tunic, err := tun.Create(nic.Config{MTU: 1428, IPv4: "10.10.10.1/24"}) if err != nil { panic(err) } vnic := nic.VirtualNIC{NIC: tunic} vnic.AddPeer("10.10.10.2", "", &net.UDPAddr{IP: net.ParseIP("192.168.3.99"), Port: 22334}) ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) defer cancel() err = vpn.New(vpn.Config{MTU: 1428}).Run(ctx, vnic, packetConn) if err != nil { panic(err) } ``` -------------------------------- ### Initializing VPN Peer 1 in Go Source: https://github.com/sigcn/pg/blob/main/vpn/README.md This Go snippet configures the first VPN peer. It establishes a UDP listener, creates a virtual TUN interface with a specified MTU and IPv4 address, and registers the second peer's virtual and physical IP addresses. The VPN then starts, gracefully handling OS signals for termination. ```Go packetConn, err := net.ListenPacket("udp", "192.168.3.99:22334") if err != nil { panic(err) } tunic, err := tun.Create(nic.Config{MTU: 1428, IPv4: "10.10.10.2/24"}) if err != nil { panic(err) } vnic := nic.VirtualNIC{NIC: tunic} vnic.AddPeer("10.10.10.1", "", &net.UDPAddr{IP: net.ParseIP("192.168.3.98"), Port: 22335}) ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) defer cancel() err = vpn.New(vpn.Config{MTU: 1428}).Run(ctx, vnic, packetConn) if err != nil { panic(err) } ``` -------------------------------- ### Running PeerGuard Map Daemon (Shell) Source: https://github.com/sigcn/pg/blob/main/README.md This command starts the PeerGuard map daemon locally, listening on `127.0.0.1:9987`. It requires a secret key for authentication and specifies multiple STUN servers for NAT traversal assistance, enabling self-hosted PeerGuard server functionality. ```Shell $ pgmap -l 127.0.0.1:9987 --secret-key 5172554832d76672d1959a5ac63c5ab9 \ --stun 111.206.174.2:3478 --stun 106.13.249.54:3478 --stun 106.12.251.52:3478 --stun 106.12.251.31:3478 ``` -------------------------------- ### Implementing an Echo Server with p2p in Go Source: https://github.com/sigcn/pg/blob/main/p2p/README.md This snippet demonstrates how to configure a Go application to act as an unreliable p2p echo server. It involves joining an OIDC network for authentication, listening for incoming packets on a specific peer ID, and echoing any received data back to the sender. Key dependencies include `network`, `oidc`, `p2p`, and `disco` packages. ```Go serverURL := "wss://openpg.in/pg" intent := langs.Must(network.JoinOIDC(oidc.ProviderGoogle, peermapURL)) fmt.Println(intent.AuthURL()) // https://openpg.in/oidc/google?state=5G68CtYnMRMdrtrRF networkSecret := langs.Must(intent.Wait(context.TODO())) // peerID is a unique string (less than 256bytes) packetConn := langs.Must(p2p.ListenPacket( disco.NewServer(serverURL, networkSecret), p2p.ListenPeerID("uniqueString"), )) // unreliability echo server buf := make([]byte, 1024) for { n, peerID, err := packetConn.ReadFrom(buf) if err != nil { panic(err) } fmt.Println("Echo packet to", peerID, string(buf[:n])) langs.Must(packetConn.WriteTo(peerID, buf[:n])) } ``` -------------------------------- ### Opening RDT Client Stream in Go Source: https://github.com/sigcn/pg/blob/main/rdt/README.md This Go code snippet illustrates how to set up a client for the Reliable Data Transfer (RDT) protocol. It listens for UDP packets on a local address, establishes an RDT listener, and then opens a stream to a specified remote RDT server address. This setup is crucial for initiating communication with an RDT server. It requires the `net` and `rdt` packages. ```Go packetConn, err := net.ListenPacket("udp", "192.168.3.98:22335") if err != nil { panic(err) } listener, err := rdt.Listen(packetConn) if err != nil { panic(err) } conn, err := listener.OpenStream(&net.UDPAddr{ IP: net.ParseIP("192.168.3.99"), Port: 22334, }) if err != nil { panic(err) } ... ``` -------------------------------- ### Running PeerGuard VPN in Rootless Mode (Shell) Source: https://github.com/sigcn/pg/blob/main/README.md This command starts the PeerGuard VPN client in rootless mode, allowing it to run without elevated privileges. It sets up a local proxy listener and forwards specific TCP and UDP traffic through the VPN tunnel, enabling selective network access. ```Shell pgvpn -s wss://openpg.in/pg -4 100.64.0.1/24 --proxy-listen 127.0.0.1:4090 --forward tcp://127.0.0.1:80 --forward udp://8.8.8.8:53 ``` -------------------------------- ### Setting Up Server Connection with connmux in Go Source: https://github.com/sigcn/pg/blob/main/connmux/README.md This snippet illustrates how to set up a TCP listener, accept an incoming connection, multiplex it using `connmux.Mux` with `SeqEven` sequencing, and then open a new stream on the multiplexed session. It requires the `net` and `connmux` packages. ```Go l, err := net.Listen("tcp", ":7676") if err != nil { panic(err) } c, err := l.Accept() if err != nil { panic(err) } session := connmux.Mux(c, connmux.SeqEven) defer session.Close() muxConn, err := session.OpenStream() if err != nil { panic(err) } // ... ``` -------------------------------- ### Initializing Client Connection with connmux in Go Source: https://github.com/sigcn/pg/blob/main/connmux/README.md This snippet demonstrates how to establish a TCP connection, multiplex it using `connmux.Mux` with `SeqOdd` sequencing, and then continuously accept new multiplexed connections to be handled concurrently. It requires the `net` and `connmux` packages. ```Go c, err := net.Dial("tcp", "192.168.3.99:7676") if err != nil { panic(err) } session := connmux.Mux(c, connmux.SeqOdd) defer session.Close() for { muxC, err := session.Accept() if err != nil { panic(err) } go handleConn(muxC) } ``` -------------------------------- ### Initializing RDT Server in Go Source: https://github.com/sigcn/pg/blob/main/rdt/README.md This Go code snippet demonstrates how to set up a server for the Reliable Data Transfer (RDT) protocol. It listens for incoming UDP packets on a specified address, establishes an RDT listener, and then continuously accepts new connections, handling each with a placeholder `handle` function. It requires the `net` and `rdt` packages. ```Go packetConn, err := net.ListenPacket("udp", "192.168.3.99:22334") if err != nil { panic(err) } listener, err := rdt.Listen(packetConn) if err != nil { panic(err) } for { conn, err := listener.Accept() if err != nil { panic(err) } handle(conn) } ``` -------------------------------- ### Downloading a File with fileshare in Go Source: https://github.com/sigcn/pg/blob/main/fileshare/README.md This snippet demonstrates how to download a file using the `fileshare` library in Go. It initializes a `Downloader` with server and UDP port configurations, defines a `read` function to handle the incoming file stream, perform a handshake, write the data to a local file, and verify the file's SHA256 checksum against the peer's. The `Request` method is then called with a context for cancellation and the file handle callback. ```Go downloader := &fileshare.Downloader{ Server: "wss://synf.in/pg", ListenUDPPort: 29999, } read := func(fh *fileshare.FileHandle) error { // handshake (can set the offset to facilitate breakpoint resuming) if err := fh.Handshake(0, nil); err != nil { return err } reader, fileSize, err := fh.File() if err != nil { return err } f, err := os.Create(fh.Filename) if err != nil { return err } if err = io.Copy(io.MultiWriter(f, sum), reader); err != nil { return err } peerSum, err := fh.Sha256() // file checksum from peer if err != nil { return err } if !bytes.Equal(sum.Sum(nil), peerSum) { // assert that local and remote are consistent return errors.New("transfer error") } return nil } ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt) defer cancel() err := downloader.Request(ctx, "pg://DJX2csRurJ3DvKeh63JebVHFDqVhnFjckdVhToAAiPYf/0/my-show.pptx", read) if err != nil { panic(err) } ``` -------------------------------- ### Creating a Shortcut for PeerGuard VPN (Shell) Source: https://github.com/sigcn/pg/blob/main/README.md This command creates a symbolic link, allowing users to invoke `pgvpn` directly instead of `pgcli vpn`. This simplifies command-line usage for VPN operations by providing a convenient alias. ```Shell ln -sf /usr/sbin/pgcli /usr/sbin/pgvpn ``` -------------------------------- ### Initializing PeerGuard VPN for Node 1 (Shell) Source: https://github.com/sigcn/pg/blob/main/README.md This command initializes the PeerGuard VPN client for the first node. It connects to the specified PeerGuard server and assigns an IPv4 address within the 100.64.0.0/24 subnet. Time synchronization between nodes is crucial for proper operation. ```Shell pgcli vpn -s wss://openpg.in/pg -4 100.64.0.1/24 ``` -------------------------------- ### Initializing PeerGuard VPN for Node 2 (Shell) Source: https://github.com/sigcn/pg/blob/main/README.md This command initializes the PeerGuard VPN client for the second node. It connects to the same PeerGuard server as node 1 and assigns a different IPv4 address within the 100.64.0.0/24 subnet, enabling direct communication between the two nodes. Time synchronization is critical. ```Shell pgcli vpn -s wss://openpg.in/pg -4 100.64.0.2/24 ``` -------------------------------- ### Wrapping PeerGuard Map with Caddy HTTPS Proxy (Shell) Source: https://github.com/sigcn/pg/blob/main/README.md This command uses Caddy to set up a reverse proxy, making the locally running PeerGuard map daemon accessible via HTTPS. It proxies requests from `https://openpg.in` to the local `pgmap` instance at `127.0.0.1:9987`, securing the communication. ```Shell $ caddy reverse-proxy --from https://openpg.in --to 127.0.0.1:9987 ``` -------------------------------- ### Initializing VPN with Pre-shared Secret File (Shell) Source: https://github.com/sigcn/pg/blob/main/README.md This command initializes the PeerGuard VPN client using a previously generated pre-shared network secret (PSNS) file (`psns.json`) for authentication. It connects to the specified PeerGuard server and assigns an IPv4 address, bypassing standard OIDC authentication. ```Shell sudo pgcli vpn -s wss://openpg.in/pg -4 100.64.0.1/24 -f psns.json ``` -------------------------------- ### Sharing a File with PeerGuard (Shell) Source: https://github.com/sigcn/pg/blob/main/README.md This command initiates a P2P file sharing operation using PeerGuard. It connects to the specified PeerGuard server and shares the local file `~/my-show.pptx`, generating a unique `ShareURL` for others to download the file. ```Shell $ pgcli share -s wss://openpg.in/pg ~/my-show.pptx ``` -------------------------------- ### Downloading a File with PeerGuard (Shell) Source: https://github.com/sigcn/pg/blob/main/README.md This command downloads a file previously shared via PeerGuard. It connects to the specified PeerGuard server and uses the provided `ShareURL` to retrieve the file from the sharing peer. ```Shell $ pgcli download -s wss://openpg.in/pg pg://DJX2csRurJ3DvKeh63JebVHFDqVhnFjckdVhToAAiPYf/0/my-show.pptx ``` -------------------------------- ### Querying Found Peers via IPC (Shell) Source: https://github.com/sigcn/pg/blob/main/README.md This command uses the `pgvpn` shortcut to query the PeerGuard client for a list of currently connected or discovered peers via Inter-Process Communication (IPC). It provides insight into the active P2P network connections. ```Shell pgvpn --peers ``` -------------------------------- ### Generating Pre-shared Secret for OIDC Auth (Shell) Source: https://github.com/sigcn/pg/blob/main/README.md This sequence of commands sets environment variables for the PeerGuard secret key and server, then uses `pgcli admin secret` to generate a pre-shared network secret (PSNS) file. This file, valid for 24 hours, can be used for authentication instead of OpenID Connect. ```Shell export PG_SECRET_KEY=5172554832d76672d1959a5ac63c5ab9 export PG_SERVER=wss://openpg.in/pg pgcli admin secret --network "" --duration 24h > psns.json ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.