Categorygithub.com/nathanielvarona/pritunl-api-go
modulepackage
0.3.7
Repository: https://github.com/nathanielvarona/pritunl-api-go.git
Documentation: pkg.go.dev

# README

pritunl-api-go

Pritunl API Client for Go

A Go client for the Pritunl API, allowing you to interact with Pritunl servers and perform various actions.

Quality Gate Status

Getting Started

Environment Variables

Load your Pritunl API credentials as environment variables:

export PRITUNL_BASE_URL="https://vpn.domain.tld"
export PRITUNL_API_TOKEN="<PRITUNL API TOKEN>"
export PRITUNL_API_SECRET="<PRITUNL API SECRET>"

Installation

Get the Pritunl API Client for Go package/library:

go get github.com/nathanielvarona/pritunl-api-go

Usage

Initialize an API instance and call available feature functions:

package main

import (
	"context"
	"encoding/json"
	"fmt"
	"log"

	"github.com/nathanielvarona/pritunl-api-go"
)

func main() {
	// Initialize the Pritunl API client
	client, err := pritunl.NewClient()
	// Alternatively, you can initialize the client with manual arguments
	// client, err := pritunl.NewClient(&pritunl.Client{
	// 	BaseUrl:   "<PRITUNL BASE URL>",
	// 	ApiToken:  "<PRITUNL API TOKEN>",
	// 	ApiSecret: "<PRITUNL API SECRET>",
	// })
	if err != nil {
		log.Fatal(err)
	}

	// Create a context for the request
	ctx := context.Background()

	// Retrieve the server status
	status, err := client.StatusGet(ctx)
	if err != nil {
		log.Fatal(err)
	}

	// Print server status details
	fmt.Println("Server Status:")
	for _, stat := range status {
		fmt.Println("Server Version:", stat.ServerVersion)
		fmt.Println("Local Networks:", stat.LocalNetworks)
		fmt.Println("Host Online:", stat.HostsOnline)
		fmt.Println("------")
	}

	// Marshal server status to JSON
	statusBytes, err := json.MarshalIndent(status, "", "  ")
	if err != nil {
		log.Println("Error marshalling status:", err)
	} else {
		fmt.Println("Server Status in JSON:")
		fmt.Println(string(statusBytes))
	}
}

Examples

Check the _examples folder for code examples demonstrating how to use this package/library.

Contributing

We welcome your contributions to pritunl-api-go. This guide outlines the process for contributing effectively.

Fork & Pull Requests

Workflow:

  1. Fork the repository: Visit the pritunl-api-go repository on GitHub and click "Fork". This creates your own copy.
  2. Clone your forked repository: Use git clone to clone your forked copy to your local development environment.
  3. Create a Branch: Use a descriptive branch name following the convention <type>/<descriptive-name>.
    • <type>: Choose from breaking, feature, improvement, automation, or documentation.
    • Refer to the .github/labels.yml and .github/pr-labeler.yml file for valid <type> options and label descriptions. (e.g., improvement/start-stop-a-server)
  4. Make your changes: Implement your code modifications, ensuring they adhere to Go coding conventions gofmt and consider adding unit tests ref* for new features.
  5. Commit your changes: Stage and commit your changes with clear and concise commit messages.
  6. Push your branch and Create a Pull Request: Push your local branch to your forked repository on GitHub and create a pull request with a detailed description of your changes.

Additional Tips:

  • ref* Include examples where relevant to illustrate your changes.
  • Simplify your development workflow! We recommend using a Go workspace when contributing to pritunl-api-go. Go workspaces provide a clean and efficient way to manage dependencies.

Rebasing and Squashing Commits

Before submitting your pull request, we recommend cleaning up your commit history to make it easier to review. This involves rebasing your branch on top of the main branch and combining your commits into a single, clear commit. Additionally, please ensure that your commit author name and email are consistent with your GitHub account, as this will help us keep a clear record of contributions.

We appreciate your contributions to the project!

By following these guidelines, you'll help us maintain a high-quality codebase and make it easier for others to contribute. Thank you for taking the time to contribute to pritunl-api-go!

Features

Core Pritunl API Client

Feature FunctionDescriptionStatus
StatusGetStatus of Pritunl Server:white_check_mark: Yes
KeyGetGenerate or Retrieve a Key for the User:white_check_mark: Yes
UserGetGet the Information of Existing User:white_check_mark: Yes
UserCreateCreate a New User:white_check_mark: Yes
UserUpdateUpdate an Existing User:white_check_mark: Yes
UserDeleteDelete an User:white_check_mark: Yes
OrganizationGetGet the Information of Existing Org:white_check_mark: Yes
OrganizationCreateCreate a New Org:white_check_mark: Yes
OrganizationUpdateUpdate an Existing Org:white_check_mark: Yes
OrganizationDeleteDelete an Org:white_check_mark: Yes
ServerGetGet the Information of Existing Server:white_check_mark: Yes
ServerCreateCreate a New Server:white_check_mark: Yes
ServerUpdateUpdate an existing Server:white_check_mark: Yes
ServerDeleteDelete a Server:white_check_mark: Yes
ServerStartStart an existing Server:white_check_mark: Yes
ServerStopStart an existing Server:white_check_mark: Yes
ServerRestartRestart an existing Server:white_check_mark: Yes
ServerRouteGetGet the Routes for a Server:white_check_mark: Yes
ServerRouteCreateCreate/Add a Server Route:white_check_mark: Yes
ServerRouteUpdateUpdate a Server Route:white_check_mark: Yes
ServerRouteDeleteRemove/Delete a Server Route:white_check_mark: Yes
ServerOrgAttachAttach an Organization for a Server:white_check_mark: Yes
ServerOrgDetachDetach an Organization for a Server:white_check_mark: Yes
ServerHostAttachAttach a Host for a Server:white_check_mark: Yes
ServerHostDetachDetach a Host for a Server:white_check_mark: Yes

Future Enhancements (CLI)

  1. CLI Framework: Consider using a popular framework like spf13/cobra, urfave/cli, or alecthomas/kong to simplify the command structure, argument parsing, and flag handling.
  2. Build Distribution Workflow: Implement a CI/CD workflow (e.g., using GitHub Actions) to automate building and distributing the CLI tool across various platforms (Windows, macOS, Linux) and architectures (32-bit, 64-bit). This will streamline setup for users on different systems.

Alternative API Clients

# Functions

NewClient creates a new Pritunl client instance.

# Structs

Client represents a Pritunl API client.
KeyResponse represents a key response from the Pritunl API.
OrganizationRequest represents the structure of the organization request.
OrganizationResponse represents the structure of the organization response.
Substructure of `UserResponse` struct for `Servers` field.
ServerHostRequest represents a request to attach or detach a host to a server.
ServerHostResponse represents a server host response.
ServerOrgRequest represents a request to attach or detach an organization to a server.
ServerOrgResponse represents a server organization response.
ServerRequest represents a request to create or update a server.
ServerResponse represents a server response.
ServerRouteRequest represents a request to create or update a server route.
ServerRouteResponse represents a server route response.
StatusResponse represents the structure of Pritunl's status response.
Substructure of `UserRequest` and `UserResponse` structs for `PortForwarding` field.
UserRequest represents the structure of User Get/Post/Put request.
UserResponse represents the structure of User response.