import from minilb repo

This commit is contained in:
2026-03-25 16:55:28 +02:00
parent 8efe7090be
commit 3aa1e52a5d
37 changed files with 1977 additions and 424 deletions

View File

@@ -0,0 +1,51 @@
package forwarder
import (
"context"
"time"
"helmet/pkg/client"
"helmet/pkg/mlbctl"
"github.com/spf13/cobra"
)
type CreateForwarderParams struct {
Hostname string
Lport uint32
Dport uint32
Addresses []string
}
type CreateForwarderResult struct{}
func (tool *Tool) CreateForwarder(cmd *cobra.Command, args []string) {
tool.createForwarderParams.Hostname = args[0]
res, err := tool.createForwarder(&tool.createForwarderParams)
printResponse(res, err)
}
func (tool *Tool) createForwarder(params *CreateForwarderParams) (*CreateForwarderResult, error) {
var err error
res := &CreateForwarderResult{}
ref, err := client.NewReferer(params.Hostname)
if err != nil {
return res, err
}
authCred := client.NewAuthCredential(ref.Userinfo())
conn, cli, err := client.NewClient(ref.Hostinfo(), authCred)
if err != nil {
return res, err
}
defer conn.Close()
ctx, _ := context.WithTimeout(context.Background(), 1*time.Second)
opReq := &mlbctl.CreateForwarderParams{
Lport: params.Lport,
Dport: params.Dport,
Destinations: params.Addresses,
}
_, err = cli.CreateForwarder(ctx, opReq)
if err != nil {
return res, err
}
return res, err
}

View File

@@ -0,0 +1,47 @@
package forwarder
import (
"context"
"time"
"helmet/pkg/client"
"helmet/pkg/mlbctl"
"github.com/spf13/cobra"
)
type DeleteForwarderParams struct {
Hostname string
Lport uint32
}
type DeleteForwarderResult struct{}
func (tool *Tool) DeleteForwarder(cmd *cobra.Command, args []string) {
tool.deleteForwarderParams.Hostname = args[0]
res, err := tool.deleteForwarder(&tool.deleteForwarderParams)
printResponse(res, err)
}
func (tool *Tool) deleteForwarder(params *DeleteForwarderParams) (*DeleteForwarderResult, error) {
var err error
res := &DeleteForwarderResult{}
ref, err := client.NewReferer(params.Hostname)
if err != nil {
return res, err
}
authCred := client.NewAuthCredential(ref.Userinfo())
conn, cli, err := client.NewClient(ref.Hostinfo(), authCred)
if err != nil {
return res, err
}
defer conn.Close()
ctx, _ := context.WithTimeout(context.Background(), 1*time.Second)
opReq := &mlbctl.DeleteForwarderParams{
Lport: params.Lport,
}
_, err = cli.DeleteForwarder(ctx, opReq)
if err != nil {
return res, err
}
return res, err
}

View File

@@ -0,0 +1,78 @@
package forwarder
import (
"fmt"
"github.com/spf13/cobra"
"sigs.k8s.io/yaml"
)
type Tool struct {
cmd *cobra.Command
listForwardersParams ListForwardersParams
createForwarderParams CreateForwarderParams
deleteForwarderParams DeleteForwarderParams
}
func NewTool() *Tool {
tool := &Tool{}
tool.cmd = &cobra.Command{
Use: "forwarder",
Short: "Forwarder operation",
Aliases: []string{"forw"},
}
listForwardersCmd := &cobra.Command{
Use: "list",
Args: cobra.ExactArgs(1),
Short: "List forwarders",
Run: tool.ListForwarders,
}
tool.cmd.AddCommand(listForwardersCmd)
createForwarderCmd := &cobra.Command{
Use: "create",
Args: cobra.ExactArgs(1),
Short: "Create forwarder",
Run: tool.CreateForwarder,
}
createForwarderCmd.Flags().Uint32VarP(&tool.createForwarderParams.Lport, "lport", "L", 0, "Listening port")
createForwarderCmd.Flags().Uint32VarP(&tool.createForwarderParams.Dport, "dport", "D", 0, "Destination port")
createForwarderCmd.Flags().StringArrayVarP(&tool.createForwarderParams.Addresses, "dests", "A", []string{}, "Destination address list")
createForwarderCmd.MarkFlagRequired("lport")
createForwarderCmd.MarkFlagRequired("dport")
createForwarderCmd.MarkFlagRequired("dests")
tool.cmd.AddCommand(createForwarderCmd)
deleteForwarderCmd := &cobra.Command{
Use: "delete",
Args: cobra.ExactArgs(1),
Short: "Delete forwarder",
Run: tool.CreateForwarder,
}
deleteForwarderCmd.Flags().Uint32VarP(&tool.deleteForwarderParams.Lport, "lport", "L", 0, "Listening port")
deleteForwarderCmd.MarkFlagRequired("lport")
tool.cmd.AddCommand(deleteForwarderCmd)
return tool
}
func (tool *Tool) GetCmd() *cobra.Command {
return tool.cmd
}
func printResponse(res any, err error) {
type Response struct {
Error bool `json:"error" json:"error"`
Message string `json:"message,omitempty" json:"message,omitempty"`
Result any `json:"result,omitempty" json:"result,omitempty"`
}
resp := Response{}
if err != nil {
resp.Error = true
resp.Message = err.Error()
} else {
resp.Result = res
}
respBytes, _ := yaml.Marshal(resp)
fmt.Printf("---\n%s\n", string(respBytes))
}

View File

@@ -0,0 +1,47 @@
package forwarder
import (
"context"
"time"
"helmet/pkg/client"
"helmet/pkg/mlbctl"
"github.com/spf13/cobra"
)
type ListForwardersParams struct {
Hostname string
}
type ListForwardersResult struct {
Forwarders []*mlbctl.Forwarder `json:"forwarders,omitempty"`
}
func (tool *Tool) ListForwarders(cmd *cobra.Command, args []string) {
tool.listForwardersParams.Hostname = args[0]
res, err := tool.listForwarders(&tool.listForwardersParams)
printResponse(res, err)
}
func (tool *Tool) listForwarders(params *ListForwardersParams) (*ListForwardersResult, error) {
var err error
res := &ListForwardersResult{}
ref, err := client.NewReferer(params.Hostname)
if err != nil {
return res, err
}
authCred := client.NewAuthCredential(ref.Userinfo())
conn, cli, err := client.NewClient(ref.Hostinfo(), authCred)
if err != nil {
return res, err
}
defer conn.Close()
ctx, _ := context.WithTimeout(context.Background(), 1*time.Second)
opReq := &mlbctl.ListForwardersParams{}
opRes, err := cli.ListForwarders(ctx, opReq)
if err != nil {
return res, err
}
res.Forwarders = opRes.Forwarders
return res, err
}

22
cmd/minilbctl/main.go Normal file
View File

@@ -0,0 +1,22 @@
/*
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
*
* This work is published and licensed under a Creative Commons
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
*
* Distribution of this work is permitted, but commercial use and
* modifications are strictly prohibited.
*/
package main
import (
"os"
)
func main() {
tool := NewTool()
err := tool.Exec(os.Args[1:])
if err != nil {
os.Exit(1)
}
}

View File

@@ -0,0 +1,91 @@
package service
import (
"context"
"fmt"
"time"
"helmet/pkg/client"
"helmet/pkg/mlbctl"
"github.com/spf13/cobra"
"sigs.k8s.io/yaml"
)
type Tool struct {
cmd *cobra.Command
serviceHelloParams ServiceHelloParams
}
func NewTool() *Tool {
tool := &Tool{}
tool.cmd = &cobra.Command{
Use: "service",
Short: "Service operation",
}
serviceHelloCmd := &cobra.Command{
Use: "hello",
Args: cobra.ExactArgs(1),
Short: "Send and receive hello",
Run: tool.ServiceHello,
}
tool.cmd.AddCommand(serviceHelloCmd)
return tool
}
func (tool *Tool) GetCmd() *cobra.Command {
return tool.cmd
}
type ServiceHelloParams struct {
Hostname string
}
type ServiceHelloResult struct {
Message string `json:"message,omitempty"`
}
func (tool *Tool) ServiceHello(cmd *cobra.Command, args []string) {
tool.serviceHelloParams.Hostname = args[0]
res, err := tool.serviceHello(&tool.serviceHelloParams)
printResponse(res, err)
}
func (tool *Tool) serviceHello(params *ServiceHelloParams) (*ServiceHelloResult, error) {
var err error
res := &ServiceHelloResult{}
ref, err := client.NewReferer(params.Hostname)
if err != nil {
return res, err
}
authCred := client.NewAuthCredential(ref.Userinfo())
conn, cli, err := client.NewClient(ref.Hostinfo(), authCred)
if err != nil {
return res, err
}
defer conn.Close()
ctx, _ := context.WithTimeout(context.Background(), 1*time.Second)
opReq := &mlbctl.GetHelloParams{}
opRes, err := cli.GetHello(ctx, opReq)
if err != nil {
return res, err
}
res.Message = opRes.Message
return res, err
}
func printResponse(res any, err error) {
type Response struct {
Error bool `json:"error" json:"error"`
Message string `json:"message,omitempty" json:"message,omitempty"`
Result any `json:"result,omitempty" json:"result,omitempty"`
}
resp := Response{}
if err != nil {
resp.Error = true
resp.Message = err.Error()
} else {
resp.Result = res
}
respBytes, _ := yaml.Marshal(resp)
fmt.Printf("---\n%s\n", string(respBytes))
}

49
cmd/minilbctl/tool.go Normal file
View File

@@ -0,0 +1,49 @@
/*
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
*
* This work is published and licensed under a Creative Commons
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
*
* Distribution of this work is permitted, but commercial use and
* modifications are strictly prohibited.
*/
package main
import (
"os"
"path/filepath"
"helmet/cmd/minilbctl/forwarder"
"helmet/cmd/minilbctl/service"
"github.com/spf13/cobra"
)
type Tool struct {
cmd *cobra.Command
forwarderTool *forwarder.Tool
serviceTool *service.Tool
}
func NewTool() *Tool {
execName := filepath.Base(os.Args[0])
tool := &Tool{}
tool.cmd = &cobra.Command{
Use: execName,
Short: "\nService tools",
SilenceUsage: true,
}
tool.cmd.CompletionOptions.DisableDefaultCmd = true
tool.serviceTool = service.NewTool()
tool.cmd.AddCommand(tool.serviceTool.GetCmd())
tool.forwarderTool = forwarder.NewTool()
tool.cmd.AddCommand(tool.forwarderTool.GetCmd())
return tool
}
func (tool *Tool) Exec(args []string) error {
tool.cmd.SetArgs(args)
return tool.cmd.Execute()
}