From b4eb561e1f8b47eaf4331175579e0066d6bd31dc Mon Sep 17 00:00:00 2001 From: 0xTux <0xtux@pm.me> Date: Sat, 19 Oct 2024 12:26:42 +0530 Subject: [PATCH] feat: initial commit with cobra setup --- .gitignore | 25 +++++++++++++++++++++++++ README.md | 1 + cmd/client.go | 30 ++++++++++++++++++++++++++++++ cmd/root.go | 24 ++++++++++++++++++++++++ cmd/server.go | 29 +++++++++++++++++++++++++++++ go.mod | 16 ++++++++++++++++ go.sum | 29 +++++++++++++++++++++++++++++ internal/client/init.go | 13 +++++++++++++ internal/server/init.go | 13 +++++++++++++ main.go | 10 ++++++++++ 10 files changed, 190 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 cmd/client.go create mode 100644 cmd/root.go create mode 100644 cmd/server.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 internal/client/init.go create mode 100644 internal/server/init.go create mode 100644 main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0df78a8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,25 @@ +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + +# Go workspace file +go.work +go.work.sum + +# env file +.env + +# Random snippets +snippets/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..8b9a7fa --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# trok diff --git a/cmd/client.go b/cmd/client.go new file mode 100644 index 0000000..bf3b3c9 --- /dev/null +++ b/cmd/client.go @@ -0,0 +1,30 @@ +/* +Copyright © 2024 tux <0xtux@pm.me> +*/ +package cmd + +import ( + "github.com/0xtux/trok/internal/client" + "github.com/spf13/cobra" +) + +// clientCmd represents the local command +var clientCmd = &cobra.Command{ + Use: "client", + Short: "Initiates a local proxy to the remote server", + Long: "Initiates a local proxy to the remote server", + Run: func(cmd *cobra.Command, args []string) { + port, err := cmd.Flags().GetUint16("port") + if err != nil { + panic(err) + } + client.Start(port) + }, +} + +func init() { + rootCmd.AddCommand(clientCmd) + + clientCmd.Flags().Uint16P("port", "p", 0, "Local port to expose") + clientCmd.MarkFlagRequired("port") +} diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 0000000..86b889e --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,24 @@ +/* +Copyright © 2024 tux <0xtux@pm.me> +*/ +package cmd + +import ( + "os" + + "github.com/spf13/cobra" +) + +var rootCmd = &cobra.Command{ + Use: "trok", + Hidden: true, + Long: "Simple TCP tunnel in Go that exposes local ports to internet, bypassing NAT firewalls.", +} + +func Execute() { + rootCmd.CompletionOptions.HiddenDefaultCmd = true + err := rootCmd.Execute() + if err != nil { + os.Exit(1) + } +} diff --git a/cmd/server.go b/cmd/server.go new file mode 100644 index 0000000..16d9d18 --- /dev/null +++ b/cmd/server.go @@ -0,0 +1,29 @@ +/* +Copyright © 2024 tux <0xtux@pm.me> +*/ +package cmd + +import ( + "github.com/0xtux/trok/internal/server" + "github.com/spf13/cobra" +) + +// serverCmd represents the server command +var serverCmd = &cobra.Command{ + Use: "server", + Short: "Initiates the remote proxy server", + Long: "Initiates the remote proxy server", + Run: func(cmd *cobra.Command, args []string) { + port, err := cmd.Flags().GetUint16("port") + if err != nil { + panic(err) + } + server.Start(port) + }, +} + +func init() { + rootCmd.AddCommand(serverCmd) + + serverCmd.Flags().Uint16P("port", "p", 1421, "Port for the server to listen on") +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..ed1d1d4 --- /dev/null +++ b/go.mod @@ -0,0 +1,16 @@ +module github.com/0xtux/trok + +go 1.22.7 + +require github.com/google/uuid v1.6.0 + +require ( + github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/matoous/go-nanoid/v2 v2.1.0 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.19 // indirect + github.com/rs/zerolog v1.33.0 // indirect + github.com/spf13/cobra v1.8.1 // indirect + github.com/spf13/pflag v1.0.5 // indirect + golang.org/x/sys v0.12.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..a21aa41 --- /dev/null +++ b/go.sum @@ -0,0 +1,29 @@ +github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= +github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/matoous/go-nanoid/v2 v2.1.0 h1:P64+dmq21hhWdtvZfEAofnvJULaRR1Yib0+PnU669bE= +github.com/matoous/go-nanoid/v2 v2.1.0/go.mod h1:KlbGNQ+FhrUNIHUxZdL63t7tl4LaPkZNpUULS8H4uVM= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= +github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= +github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8= +github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM= +github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= +golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/client/init.go b/internal/client/init.go new file mode 100644 index 0000000..1a1792c --- /dev/null +++ b/internal/client/init.go @@ -0,0 +1,13 @@ +/* +Copyright © 2024 tux <0xtux@pm.me> +*/ + +package client + +import ( + "log" +) + +func Start(port uint16) { + log.Printf("Hello from client: %d", port) +} diff --git a/internal/server/init.go b/internal/server/init.go new file mode 100644 index 0000000..794d338 --- /dev/null +++ b/internal/server/init.go @@ -0,0 +1,13 @@ +/* +Copyright © 2024 tux <0xtux@pm.me> +*/ + +package server + +import ( + "log" +) + +func Start(port uint16) { + log.Printf("Hello from server: %d", port) +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..641e81d --- /dev/null +++ b/main.go @@ -0,0 +1,10 @@ +/* +Copyright © 2024 tux <0xtux@pm.me> +*/ +package main + +import "github.com/0xtux/trok/cmd" + +func main() { + cmd.Execute() +}