1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
|
// Copyright (c) 2020-2025 Bryan Frimin <bryan@frimin.fr>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
package main
import (
"context"
"crypto/tls"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/url"
"os"
"path"
"path/filepath"
"strings"
"github.com/spf13/cobra"
"go.gearno.de/privatebin/v2"
)
var (
version = "dev"
commit = "unknown"
date = "unknown"
userAgent = "privatebin-cli/" + version + " (source; https://go.gearno.de/privatebin)"
cfgPath string
binName string
extraHeaderFields []string
client *privatebin.Client
binCfg *BinCfg
output string
ctx = context.Background()
clientOptions = []privatebin.Option{
privatebin.WithUserAgent(userAgent),
}
expire string
openDiscussion bool
burnAfterReading bool
gzip bool
formatter string
password string
filename string
attachment bool
insecure bool
confirmBurn bool
skipTLSVerify bool
rootCmd = &cobra.Command{
Use: "privatebin",
Version: fmt.Sprintf("%s-%s (%s)", version, commit, date),
Short: "A streamlined CLI for effortlessly creating and managing PrivateBin pastes",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
switch output {
case "":
case "json":
default:
return fmt.Errorf("invalid output: %q, valid options are '', 'json'", output)
}
if cfgPath == "" {
homeDir, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("cannot get user home directory: %w", err)
}
cfgPath = path.Join(homeDir, ".config", "privatebin", "config.json")
}
cfg, err := loadCfgFile(cfgPath)
if err != nil {
return fmt.Errorf("cannot load configuration: %w", err)
}
binCfg, err = findBinCfg(cfg, binName)
if err != nil {
return fmt.Errorf("cannot find %q bin configuration: %w", binName, err)
}
clientOptions = append(
clientOptions,
privatebin.WithBasicAuth(
binCfg.Auth.Username,
binCfg.Auth.Password,
),
)
for k, v := range binCfg.ExtraHeaderFields {
clientOptions = append(
clientOptions,
privatebin.WithCustomHeaderField(k, v),
)
}
for _, value := range extraHeaderFields {
parts := strings.SplitN(value, ":", 2)
if len(parts) != 2 {
return fmt.Errorf("invalid header field format: '%s', expected 'key: value'", value)
}
clientOptions = append(
clientOptions,
privatebin.WithCustomHeaderField(
strings.TrimSpace(parts[0]),
strings.TrimSpace(parts[1]),
),
)
}
if (binCfg.SkipTLSVerify != nil && *binCfg.SkipTLSVerify) || skipTLSVerify {
tlsConfig := &tls.Config{
InsecureSkipVerify: true,
}
clientOptions = append(
clientOptions,
privatebin.WithTLSConfig(tlsConfig),
)
}
host, err := url.Parse(binCfg.Host)
if err != nil {
return fmt.Errorf("cannot parse %q bin %q host: %w", binCfg.Name, binCfg.Host, err)
}
client = privatebin.NewClient(*host, clientOptions...)
return nil
},
}
showCmd = &cobra.Command{
Use: "show",
Short: "Show a paste",
SilenceUsage: true,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
link, err := url.Parse(args[0])
if err != nil {
return fmt.Errorf("cannot parse paste url: %w", err)
}
if link.Scheme+"://"+link.Host != strings.TrimRight(binCfg.Host, "/") {
if !insecure {
return fmt.Errorf("untrusted privatebin instance use --insecure flag or add it to the configuration")
}
}
options := privatebin.ShowPasteOptions{
Password: []byte(password),
ConfirmBurn: confirmBurn,
}
result, err := client.ShowPaste(ctx, *link, options)
if err != nil {
return fmt.Errorf("cannot show paste: %w", err)
}
switch output {
case "":
fmt.Fprintf(os.Stdout, "%s\n", result.Paste.Data)
case "json":
var comments []map[string]string
for _, comment := range result.Comments {
comments = append(
comments,
map[string]string{
"comment_id": comment.CommentID,
"paste_id": comment.PasteID,
"parent_id": comment.ParentID,
"nickname": comment.Nickname,
"text": comment.Text,
},
)
}
json.NewEncoder(os.Stdout).Encode(
map[string]any{
"paste_id": result.PasteID,
"paste": map[string]string{
"attachment_name": result.Paste.AttachmentName,
"attachment": base64.StdEncoding.EncodeToString(result.Paste.Attachment),
"data": base64.StdEncoding.EncodeToString(result.Paste.Data),
},
"comment_count": result.CommentCount,
"comments": comments,
},
)
}
return nil
},
}
createCmd = &cobra.Command{
Use: "create",
Short: "Create a paste",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
if cmd.Flags().Changed("expire") {
binCfg.Expire = expire
}
if cmd.Flags().Changed("open-discussion") {
binCfg.OpenDiscussion = &openDiscussion
}
if cmd.Flags().Changed("burn-after-reading") {
binCfg.BurnAfterReading = &burnAfterReading
}
if cmd.Flags().Changed("gzip") {
binCfg.GZip = &gzip
}
if cmd.Flags().Changed("formatter") {
binCfg.Formatter = formatter
}
var (
attachementName string
data []byte
err error
)
if cmd.Flags().Changed("filename") {
file, err := os.Open(filename)
if err != nil {
return fmt.Errorf("cannot open %q file: %w", filename, err)
}
data, err = io.ReadAll(file)
if err != nil {
return fmt.Errorf("cannot read %q file: %w", filename, err)
}
if cmd.Flags().Changed("attachment") {
attachementName = filepath.Base(filename)
}
} else {
data, err = io.ReadAll(os.Stdin)
if err != nil {
return fmt.Errorf("cannot read stdin: %w", err)
}
if cmd.Flags().Changed("attachment") {
attachementName = "stdin"
}
}
options := privatebin.CreatePasteOptions{
AttachmentName: attachementName,
Formatter: binCfg.Formatter,
Expire: binCfg.Expire,
OpenDiscussion: *binCfg.OpenDiscussion,
BurnAfterReading: *binCfg.BurnAfterReading,
Password: []byte(password),
Compress: privatebin.CompressionAlgorithmNone,
}
if *binCfg.GZip {
options.Compress = privatebin.CompressionAlgorithmGZip
}
result, err := client.CreatePaste(ctx, data, options)
if err != nil {
return fmt.Errorf("cannot create the paste: %w", err)
}
switch output {
case "":
fmt.Fprintf(os.Stdout, "%s\n", result.PasteURL.String())
case "json":
json.NewEncoder(os.Stdout).Encode(
map[string]any{
"paste_id": result.PasteID,
"paste_url": result.PasteURL.String(),
"delete_token": result.DeleteToken,
},
)
}
return nil
},
}
)
func init() {
rootCmd.PersistentFlags().StringVarP(&output, "output", "o", "", "the command output format")
rootCmd.PersistentFlags().StringVarP(&cfgPath, "config", "c", "", "the config file (default is $HOME/.config/privatebin/config.json)")
rootCmd.PersistentFlags().StringVarP(&binName, "bin", "b", "", "the name of the privatebin instance to use (default \"\")")
rootCmd.PersistentFlags().StringSliceVarP(&extraHeaderFields, "header", "H", []string{}, "extra HTTP header fields to include in the request sent")
createCmd.Flags().StringVar(&expire, "expire", "", "the time to live of the paste")
createCmd.Flags().BoolVar(&openDiscussion, "open-discussion", false, "enable discussion on the paste")
createCmd.Flags().BoolVar(&burnAfterReading, "burn-after-reading", false, "delete the paste after reading")
createCmd.Flags().BoolVar(&gzip, "gzip", true, "gzip the paste data")
createCmd.Flags().StringVar(&formatter, "formatter", "", "the text formatter to use, can be plaintext, markdown or syntaxhighlighting")
createCmd.Flags().StringVar(&password, "password", "", "the paste password")
createCmd.Flags().StringVar(&filename, "filename", "", "read filepath instead of stdin")
createCmd.Flags().BoolVar(&attachment, "attachment", false, "create the paste as an attachment")
createCmd.Flags().BoolVar(&skipTLSVerify, "skip-tls-verify", false, "skip TLS certificate verification")
showCmd.Flags().BoolVar(&insecure, "insecure", false, "allow reading paste from untrusted instance")
showCmd.Flags().BoolVar(&confirmBurn, "confirm-burn", false, "confirm paste opening, it will be deleted immediately afterwards")
showCmd.Flags().StringVar(&password, "password", "", "the paste password")
showCmd.Flags().BoolVar(&skipTLSVerify, "skip-tls-verify", false, "skip TLS certificate verification")
rootCmd.AddCommand(showCmd, createCmd)
}
func main() {
if err := rootCmd.Execute(); err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}
}
|